优化代码

This commit is contained in:
Falcon 2025-03-05 11:24:06 +08:00
parent d64b49f8a1
commit 7aaaacb309

View File

@ -32,17 +32,17 @@ namespace Falcon.SugarApi.TimedTask
/// <summary> /// <summary>
/// 执行计划的Cron串 /// 执行计划的Cron串
/// </summary> /// </summary>
public abstract string CronSchedule { get; } protected abstract string CronSchedule { get; }
/// <summary> /// <summary>
/// Timer心跳 /// Timer心跳 毫秒
/// </summary> /// </summary>
public virtual int Heartbeat { get; protected set; } = 1; public virtual int Heartbeat { get; protected set; } = 1000;
/// <summary> /// <summary>
/// 获取下次执行任务的计划 /// 获取下次执行任务的计划
/// </summary> /// </summary>
public CronExpression Schedule { get; private set; } protected CronExpression Schedule { get; private set; }
/// <summary> /// <summary>
/// 任务正在运行 /// 任务正在运行
@ -95,7 +95,7 @@ namespace Falcon.SugarApi.TimedTask
public TimedTask(IServiceProvider service) { public TimedTask(IServiceProvider service) {
this.Service = service; this.Service = service;
this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger; this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger;
_timer = new PeriodicTimer(TimeSpan.FromSeconds(this.Heartbeat)); _timer = new PeriodicTimer(TimeSpan.FromMilliseconds(this.Heartbeat));
this.Schedule = new CronExpression(this.CronSchedule); this.Schedule = new CronExpression(this.CronSchedule);
} }
@ -113,7 +113,10 @@ namespace Falcon.SugarApi.TimedTask
if(stoppingToken.IsCancellationRequested) { if(stoppingToken.IsCancellationRequested) {
break; break;
} }
if(this._isRunning || DateTime.Now < this.NextTickTime) { if(this._isRunning) {
continue;
}
if(DateTime.Now < this.NextTickTime) {
continue; continue;
} }
this._isRunning = true; this._isRunning = true;