39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using Quartz;
|
||
using System;
|
||
using System.Diagnostics;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace SA.Quartz
|
||
{
|
||
public abstract class QuartzJob : IJob
|
||
{
|
||
protected IJobExecutionContext? _context;
|
||
|
||
public QuartzJob()
|
||
{
|
||
}
|
||
|
||
public async Task Execute(IJobExecutionContext context)
|
||
{
|
||
_context = context;
|
||
if (!context.CancellationToken.IsCancellationRequested)
|
||
{
|
||
Log.Debug($"[{DateTimeOffset.Now}] {this.GetType().Name} 后台任务开始执行!");
|
||
var watch = Stopwatch.StartNew();
|
||
watch.Restart();
|
||
try
|
||
{
|
||
await ExecuteAsync();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Debug($"[{DateTimeOffset.Now}] {this.GetType().Name} 后台任务执行出错!ex:{ex.Message}");
|
||
}
|
||
Log.Debug($"[{DateTimeOffset.Now}] {this.GetType().Name} 后台任务执行完成!用时: {watch.ElapsedMilliseconds / 1000D:0.000}s");
|
||
}
|
||
}
|
||
|
||
protected abstract Task ExecuteAsync();
|
||
}
|
||
}
|