SACenter/SA.Quartz/QuartzJob.cs

39 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}