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.

339 lines
8.4 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

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> cancel, Func<bool> abnormal, Func<bool> done,
Action doAction,
Action timeoutAction, bool isTimeOutExit, 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;
}
if (abnormal())
{
Log.Info($" {name} Exception");
return InvokeStatus.Exception;
}
try
{
doAction();
}
catch (Exception e)
{
Log.Error($"{name}", e);
}
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> cancel,Func<bool> succ, 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;
}
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;
}
}
}
}
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,
ManualSucc,
}