namespace MiaoYu.Core.Quartz.Jobs;
///
/// 任务基础类
///
///
public abstract class QuartzJobBase : IJob
where TQuartzJobInfo : class, IQuartzJobInfoEntity
{
protected readonly ILogger _logger;
protected readonly Stopwatch _stopwatch;
public QuartzJobBase(ILogger logger)
{
_logger = logger;
_stopwatch = new Stopwatch();
}
public virtual async Task Execute(IJobExecutionContext context)
{
var quartzJobTask = context.MergedJobDataMap.Get(QuartzStartupConfig.JobTaskKey) as TQuartzJobInfo;
if (quartzJobTask == null)
{
_logger.LogWarning("quartzJobTask is NULL !");
return;
}
using var scope = App.CreateScope();
if (scope is null) return;
_stopwatch.Restart();
await this.BeforeExecutionAsync(scope.ServiceProvider, quartzJobTask);
object? result = null;
try
{
result = await this.RunAsync(scope.ServiceProvider, quartzJobTask);
// 执行成功
_stopwatch.Stop();
await this.OnSuccessAsync(scope.ServiceProvider, quartzJobTask, result);
}
catch (Exception ex)
{
// 执行成功
_stopwatch.Stop();
_logger.LogError(ex, $"{this.GetType().FullName}.Execute Execution failed : {ex.Message}");
result = ex.Message;
await this.OnExceptionAsync(scope.ServiceProvider, quartzJobTask, ex);
}
finally
{
await this.OnExitAsync(scope.ServiceProvider, quartzJobTask, result);
}
}
///
/// 运行逻辑
///
///
///
///
protected virtual async Task