<fix>[important]修复按时间执行的任务的定时器BUG
This commit is contained in:
parent
e86cc463e1
commit
a36b6451fb
|
|
@ -9,7 +9,7 @@ namespace CirculateTool.Entity;
|
|||
/// <summary>
|
||||
/// 按时执行的任务的定时类
|
||||
/// </summary>
|
||||
internal class TimeTask
|
||||
public class TimeTask
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行任务的时间
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public class StartCirculation
|
|||
/// <summary>
|
||||
/// 指定时间执行的方法类
|
||||
/// </summary>
|
||||
private static List<TimeTask> _timeTasks = [];
|
||||
protected static List<TimeTask> _timeTasks = [];
|
||||
|
||||
/// <summary>
|
||||
/// 触发的异常
|
||||
|
|
@ -146,24 +146,25 @@ public class StartCirculation
|
|||
{
|
||||
if(_timeTasks.Count == 0) return;
|
||||
CancellationTokenSource cts = new();
|
||||
PeriodicTimer timer = new(new TimeSpan(0, 0, 0, 10, 0));
|
||||
PeriodicTimer timer = new(new TimeSpan(0, 0, 0, 1, 0));
|
||||
while (await timer.WaitForNextTickAsync(cts.Token))
|
||||
{
|
||||
string timeStr = DateTime.Now.ToString("HH:mm");
|
||||
List<Task> taskList = [];
|
||||
taskList.Add(Task.Factory.StartNew(() =>
|
||||
{
|
||||
foreach (var task in _timeTasks)
|
||||
{
|
||||
taskList.Add(Task.Factory.StartNew(() =>
|
||||
{
|
||||
if (task.ExecuteTime != timeStr)
|
||||
{
|
||||
task.IsRun = false; // 当时刻不匹配时,重置任务状态
|
||||
return;
|
||||
}
|
||||
if (task.ExecuteTime == timeStr && !task.IsRun) // 当时间匹配且任务未执行时,执行任务
|
||||
{
|
||||
try
|
||||
{
|
||||
task.Action?.Invoke();
|
||||
task.Action!();
|
||||
task.IsRun = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -171,8 +172,8 @@ public class StartCirculation
|
|||
ExceptionHandler?.Invoke(task.Description ?? task.Action!.Method.Name, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
Task.WaitAll([.. taskList]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ public class DataClear(ClearData clearData)
|
|||
/// 定时清理无用数据, ---- 每隔10分钟清理一次
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Circulation("数据清理", 1000 * 60 * 30)]
|
||||
public bool ClearDataCirculate()
|
||||
[CirculationTime(["10:11","10:12"], "数据清理")]
|
||||
public void ClearDataCirculate()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -56,10 +56,6 @@ public class DataClear(ClearData clearData)
|
|||
{
|
||||
ConsoleLog.Exception($"【数据清理】线程发生异常,异常信息:{ex}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -22,15 +22,38 @@ public class WcsCirculation(IComponentContext componentContext) : StartCirculati
|
|||
var attributes = method.GetCustomAttributes(false);
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
if (attribute is not CirculationAttribute needDurable) continue;
|
||||
if (attribute == default) continue;
|
||||
/* 定时执行的任务 */
|
||||
if (attribute is CirculationAttribute needDurable)
|
||||
{
|
||||
string methodDescription = needDurable.MethodDescription ?? $"{type.Name}.{method.Name}";
|
||||
instance ??= CreateInstance(type);
|
||||
bool Action() => (bool)(method.Invoke(instance, []) ?? false);
|
||||
StartTask(Action, methodDescription, needDurable.CirculationTime);
|
||||
break;
|
||||
}
|
||||
/* 每天指定时间执行 */
|
||||
if (attribute is CirculationTimeAttribute timeCirculate)
|
||||
{
|
||||
string methodDescription = timeCirculate.MethodDescription ?? $"{type.Name}.{method.Name}";
|
||||
instance ??= CreateInstance(type);
|
||||
foreach (var time in timeCirculate.Times)
|
||||
{
|
||||
_timeTasks.Add(new CirculateTool.Entity.TimeTask
|
||||
{
|
||||
ExecuteTime = time,
|
||||
Action = new Action(() => method.Invoke(instance, [])),
|
||||
Description = methodDescription,
|
||||
IsRun = false
|
||||
});
|
||||
}
|
||||
}
|
||||
/* END */
|
||||
}
|
||||
}
|
||||
/* 执行按时执行的任务 ---- 方法内判断,若没有此类方法则不会执行 */
|
||||
StartTimeTask();
|
||||
}
|
||||
|
||||
|
||||
private object? CreateInstance(Type type)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user