From 2fec21a3ead262c008b1e970d3039a8f0432e8ce Mon Sep 17 00:00:00 2001
From: Falcon <12919280+falconfly@user.noreply.gitee.com>
Date: Thu, 6 Mar 2025 14:38:16 +0800
Subject: [PATCH] =?UTF-8?q?TimedTask=E4=BD=BF=E7=94=A8=E8=BF=9B=E5=87=86?=
=?UTF-8?q?=E7=9A=84=E8=AE=A1=E6=97=B6=E5=99=A8=E6=96=B9=E5=BC=8F=E8=80=8C?=
=?UTF-8?q?=E9=9D=9E=E8=BD=AE=E8=AE=AD=E6=96=B9=E5=BC=8F=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Falcon.SugarApi/TimedTask/TimedTask.cs | 78 ++++++++------------------
1 file changed, 23 insertions(+), 55 deletions(-)
diff --git a/Falcon.SugarApi/TimedTask/TimedTask.cs b/Falcon.SugarApi/TimedTask/TimedTask.cs
index f29b858..7aecd31 100644
--- a/Falcon.SugarApi/TimedTask/TimedTask.cs
+++ b/Falcon.SugarApi/TimedTask/TimedTask.cs
@@ -10,16 +10,12 @@ namespace Falcon.SugarApi.TimedTask
///
/// 由定时器驱动的后台任务
///
- public abstract class TimedTask:BackgroundService, IDisposable
+ public abstract class TimedTask:BackgroundService
{
///
/// 日志记录器
///
protected readonly ILogger? Logger;
- ///
- /// 心跳计时器
- ///
- private readonly PeriodicTimer _timer;
///
/// 运行一次任务
@@ -34,20 +30,11 @@ namespace Falcon.SugarApi.TimedTask
///
protected abstract string CronSchedule { get; }
- ///
- /// Timer心跳 毫秒
- ///
- public virtual int Heartbeat { get; protected set; } = 1000;
-
///
/// 获取下次执行任务的计划
///
protected CronExpression Schedule { get; private set; }
- ///
- /// 任务正在运行
- ///
- private bool _isRunning = false;
///
/// 下次执行时间
///
@@ -83,10 +70,6 @@ namespace Falcon.SugarApi.TimedTask
/// 系统服务
///
public IServiceProvider Service { get; set; }
- ///
- /// 资源锁
- ///
- private static object _lock = new object();
///
/// 构造Timed后台任务
@@ -95,7 +78,6 @@ namespace Falcon.SugarApi.TimedTask
public TimedTask(IServiceProvider service) {
this.Service = service;
this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger;
- _timer = new PeriodicTimer(TimeSpan.FromMilliseconds(this.Heartbeat));
this.Schedule = new CronExpression(this.CronSchedule);
}
@@ -108,49 +90,35 @@ namespace Falcon.SugarApi.TimedTask
if(!this.OnStart(this,stoppingToken)) {
return;
}
- while(await _timer.WaitForNextTickAsync(stoppingToken)) {
- lock(_lock) {
+ try {
+ while(!stoppingToken.IsCancellationRequested) {
+ DateTime nextRunTime = Schedule.GetNextOccurrence(DateTime.Now);
+ TimeSpan delay = nextRunTime - DateTime.Now;
+ if(delay > TimeSpan.Zero) {
+ await Task.Delay(delay,stoppingToken);
+ }
if(stoppingToken.IsCancellationRequested) {
break;
}
- if(this._isRunning) {
- continue;
+ // 执行任务
+ try {
+ var goOn = await this.Run(stoppingToken);
+ var comResult = this.OnCompleted(this,stoppingToken);
+ if(!goOn || !comResult) {
+ break;
+ }
}
- if(DateTime.Now < this.NextTickTime) {
- continue;
+ catch(Exception ex) {
+ if(!this.OnException(this,ex,stoppingToken)) {
+ break;
+ }
}
- this._isRunning = true;
+ //Schedule = new CronExpression(this.CronSchedule);
}
- try {
- var goOn = await this.Run(stoppingToken);
- var comResult = this.OnCompleted(this,stoppingToken);
- if(!goOn || !comResult) {
- break;
- }
- }
- catch(Exception ex) {
- if(!this.OnException(this,ex,stoppingToken)) {
- break;
- }
- }
- SetNextTick();
- this._isRunning = false;
}
- this.OnStop(this,stoppingToken);
- }
-
- private void SetNextTick(string? cronSchedule = null) {
- var s = cronSchedule == null ? this.Schedule : new CronExpression(cronSchedule);
- this.NextTickTime = s.GetNextOccurrence(DateTime.Now);
- }
-
- ///
- /// 释放对象
- ///
- public override void Dispose() {
- this._timer?.Dispose();
- GC.SuppressFinalize(this);
- base.Dispose();
+ finally {
+ this.OnStop(this,stoppingToken);
+ }
}
}
}