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.
276 lines
6.6 KiB
276 lines
6.6 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())
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
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())
|
|
{
|
|
Thread.Sleep(duration);
|
|
if (cancel())
|
|
{
|
|
Log.Info($" {name} canceled");
|
|
return InvokeStatus.Cancel;
|
|
}
|
|
|
|
try
|
|
{
|
|
doAction();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error($"{name}", e);
|
|
exceptionAction();
|
|
}
|
|
}
|
|
|
|
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())
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return InvokeStatus.Done;
|
|
}
|
|
|
|
|
|
public static InvokeStatus Invoke(string name, int duration, int times, Func<bool> cancel,Func<bool> succ, Func<bool> done,
|
|
Action doAction,
|
|
Action timeoutAction, bool isTimeOutExit, Action exceptionAction, int timeOutActionTime,
|
|
InvokeStatus timeOutException)
|
|
{
|
|
int count = 0;
|
|
while (!done())
|
|
{
|
|
Thread.Sleep(duration);
|
|
if (cancel())
|
|
{
|
|
Log.Info($" {name} canceled");
|
|
return InvokeStatus.Cancel;
|
|
}
|
|
if (succ())
|
|
{
|
|
Log.Info($" {name} ManualSucc");
|
|
return InvokeStatus.ManualSucc;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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())
|
|
{
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
|
|
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())
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
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())
|
|
{
|
|
Thread.Sleep(duration);
|
|
if (cancel())
|
|
{
|
|
Log.Info($" {name} canceled");
|
|
return InvokeStatus.Cancel;
|
|
}
|
|
|
|
try
|
|
{
|
|
doAction();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error($"{name}", e);
|
|
}
|
|
}
|
|
|
|
return InvokeStatus.Done;
|
|
}
|
|
}
|
|
|
|
public enum InvokeStatus
|
|
{
|
|
Cancel,
|
|
TimeOut,
|
|
Done,
|
|
Exception,
|
|
None,
|
|
ManualSucc,
|
|
} |