namespace CloudGaming.Core.Quartz.Jobs; /// /// 任务基础类 /// /// public abstract class QuartzJobBase : IJob where TQuartzJobInfo : class, IQuartzJobInfoEntity { protected readonly Stopwatch _stopwatch; protected readonly ILogger _logger; protected readonly IServiceScopeFactory _serviceScopeFactory; public QuartzJobBase(ILogger logger, IServiceScopeFactory serviceScopeFactory) { _stopwatch = new Stopwatch(); _logger = logger; _serviceScopeFactory = serviceScopeFactory; } 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; using var scope = _serviceScopeFactory.CreateAsyncScope(); _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}" + ex.ToString()); 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); if (method is null) { return result; } var methodResult = method.Invoke(jobTaskObject, []); if (methodResult is null) { return result; } // 异步函数处理 if (typeof(Task).IsAssignableFrom(method.ReturnType)) { // 如果是泛型 if (method.ReturnType.IsGenericType) { var task = (Task)methodResult; await task.ContinueWith(t => { if (t.IsCompletedSuccessfully) { result = t.GetType() .GetProperty(nameof(Task.Result))? .GetValue(t); } }); } 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; } }