定时任务优化

master
tq 5 months ago
parent 76a456eecd
commit 8c13a3e225

@ -16,6 +16,7 @@ public class CountDayOrderTask : ITask
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(CountDayOrderTask)); private static readonly ILog Log = LogManager.GetLogger(typeof(CountDayOrderTask));
private volatile bool _stop; private volatile bool _stop;
private Timer _timer;
public ChargeOrderService _ChargeOrderService { get; set; } public ChargeOrderService _ChargeOrderService { get; set; }
@ -29,13 +30,14 @@ public class CountDayOrderTask : ITask
public int Interval() public int Interval()
{ {
return 1000 * 86400; return 1000 * 10;
} }
public void Handle() public void Handle()
{ {
// 统计每日运行 // 统计每日运行
CountOrder(); return;
// CountOrder();
} }
@ -43,16 +45,50 @@ public class CountDayOrderTask : ITask
public bool Stoped() public bool Stoped()
{ {
return _stop; return _stop;
} }
public void Stop() public void Stop()
{ {
_stop = true; _stop = true;
_timer?.Change(Timeout.Infinite, Timeout.Infinite);
} }
public void ResetStop() public void ResetStop()
{ {
_stop = false; _stop = false;
ScheduleNextRun();
}
public CountDayOrderTask()
{
ScheduleNextRun();
}
private void ScheduleNextRun()
{
if (_stop)
{
return;
}
DateTime now = DateTime.Now;
DateTime nextRun = new DateTime(now.Year, now.Month, now.Day, 19, 21, 0);
if (now > nextRun)
{
nextRun = nextRun.AddDays(1);
}
TimeSpan timeToGo = nextRun - now;
_timer = new Timer(x =>
{
if (!_stop)
{
CountOrder();
ScheduleNextRun();
}
}, null, timeToGo, Timeout.InfiniteTimeSpan);
} }
public void CountOrder() public void CountOrder()

Loading…
Cancel
Save