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.

167 lines
4.1 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 timeoutAction, bool isTimeOutExit, Action exceptionAction)
{
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);
exceptionAction();
}
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 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> 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
}