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 RunAsync(IServiceProvider serviceProvider, TQuartzJobInfo quartzJobInfo) { object? result = null; var jobTaskInfo = App.JobTaskInfos.FirstOrDefault(w => w.Key == quartzJobInfo.JobPoint); if (jobTaskInfo == null) { _logger.LogWarning($"jobTaskInfo is NULL !"); result += "jobTaskInfo is NULL !"; return result; } var jobTaskObject = ActivatorUtilities.CreateInstance(serviceProvider, jobTaskInfo.ClassType);// di 创建服务 var method = jobTaskObject.GetType().GetMethod(jobTaskInfo.MethodInfo.Name); var methodResult = method?.Invoke(jobTaskObject, Array.Empty()); // 异步函数处理 if (methodResult is Task) { if (methodResult.GetType().IsGenericType) { //await (Task)methodResult; var _value = methodResult.GetType().GetProperty(nameof(Task.Result))?.GetValue(methodResult); result = _value?.ToString(); } else { await ((Task)methodResult).WaitAsync(CancellationToken.None); } } else { result = methodResult?.ToString(); } return result; } /// /// 执行前 /// /// /// /// protected virtual Task BeforeExecutionAsync(IServiceProvider serviceProvider, TQuartzJobInfo quartzJobInfo) { return Task.CompletedTask; } /// /// 执行后,退出 /// /// /// /// /// protected virtual Task OnExitAsync( IServiceProvider serviceProvider, TQuartzJobInfo quartzJobInfo, object? result) { return Task.CompletedTask; } /// /// 成功执行 /// /// /// /// /// protected virtual Task OnSuccessAsync(IServiceProvider serviceProvider, TQuartzJobInfo quartzJobInfo, object? result) { return Task.CompletedTask; } /// /// 异常处理 /// /// /// /// /// protected virtual Task OnExceptionAsync( IServiceProvider serviceProvider, TQuartzJobInfo quartzJobInfo, Exception exception) { return Task.CompletedTask; } }