You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
5.7 KiB

using log4net;
namespace Service.Execute;
public class Invoker
{
private static readonly ILog Log = LogManager.GetLogger(typeof(Invoker));
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done,
Action doAction,
Action timeoutAction, bool isTimeOutExit)
{
int hvPwrOffTimes = 0;
while (!done())
{
Log.Info($"begin {name}");
Thread.Sleep(duration);
if (cancel())
{
Log.Info($" {name} canceled");
return InvokeStatus.Cancel;
}
try
{
doAction();
}
catch (Exception e)
{
Log.Error($"{name}", e);
}
if (hvPwrOffTimes++ > times)
{
timeoutAction();
Log.Info($" {name} timeout");
if (isTimeOutExit)
{
return InvokeStatus.TimeOut;
}
}
}
Log.Info($" {name} done");
return InvokeStatus.Done;
}
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done,
Action doAction,
Action exceptionAction)
{
int count = 0;
while (!done())
{
Log.Info($"begin {name}");
Thread.Sleep(duration);
if (cancel())
{
Log.Info($" {name} canceled");
return InvokeStatus.Cancel;
}
try
{
doAction();
}
catch (Exception e)
{
Log.Error($"{name}", e);
exceptionAction();
}
}
Log.Info($" {name} done");
return InvokeStatus.Done;
}
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done,
Action doAction,
Action timeoutAction, bool isTimeOutExit, Action exceptionAction, int timeOutActionTime,
InvokeStatus timeOutException)
{
int count = 0;
while (!done())
{
Log.Info($"begin {name}");
Thread.Sleep(duration);
if (cancel())
{
Log.Info($" {name} canceled");
return InvokeStatus.Cancel;
}
try
{
doAction();
}
catch (Exception e)
{
Log.Error($"{name}", e);
exceptionAction();
}
if (count++ > times)
{
if (count % timeOutActionTime == 0)
{
timeoutAction();
Log.Info($" {name} timeout");
if (isTimeOutExit)
{
return timeOutException;
}
}
}
}
Log.Info($" {name} done");
return InvokeStatus.Done;
}
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> done,
Action doAction,
Action timeoutAction, bool isExcepOutExit, Action exceptionAction, int timeOutActionTime,
InvokeStatus excepOutException)
{
int count = 0;
while (!done())
{
Log.Info($"begin {name}");
Thread.Sleep(duration);
try
{
doAction();
}
catch (Exception e)
{
Log.Error($"{name}", e);
exceptionAction();
if (isExcepOutExit)
{
return excepOutException;
}
}
if (count++ > times)
{
if (count % timeOutActionTime == 0)
{
timeoutAction();
Log.Info($" {name} timeout");
}
}
}
Log.Info($" {name} done");
return InvokeStatus.Done;
}
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> done,
Action doAction,
Action timeoutAction, bool isTimeOut, Action exceptionAction,
InvokeStatus timeOutActionTime)
{
int count = 0;
while (!done())
{
Log.Info($"begin {name}");
Thread.Sleep(duration);
try
{
doAction();
}
catch (Exception e)
{
Log.Error($"{name}", e);
exceptionAction();
}
if (count++ > times)
{
Log.Info($" {name} timeout");
timeoutAction();
if (isTimeOut)
{
return timeOutActionTime;
}
}
}
Log.Info($" {name} done");
return InvokeStatus.Done;
}
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel, Func<bool> done,
Action doAction)
{
int hvPwrOffTimes = 0;
while (!done())
{
Log.Info($"begin {name}");
Thread.Sleep(duration);
if (cancel())
{
Log.Info($" {name} canceled");
return InvokeStatus.Cancel;
}
try
{
doAction();
}
catch (Exception e)
{
Log.Error($"{name}", e);
}
}
Log.Info($" {name} done");
return InvokeStatus.Done;
}
}
public enum InvokeStatus
{
Cancel,
TimeOut,
Done,
Exception,
None
}