181 lines
5.3 KiB
C#
181 lines
5.3 KiB
C#
namespace CloudGaming.Core.Quartz.Jobs;
|
|
|
|
/// <summary>
|
|
/// 任务基础类
|
|
/// </summary>
|
|
/// <typeparam name="TQuartzJobInfo"></typeparam>
|
|
public abstract class QuartzJobBase<TQuartzJobInfo> : 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 运行逻辑
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
/// <param name="quartzJobInfo"></param>
|
|
/// <returns></returns>
|
|
protected virtual async Task<object?> 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<object>.Result))?
|
|
.GetValue(t);
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
await ((Task)methodResult).WaitAsync(CancellationToken.None);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = methodResult?.ToString();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行前
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
/// <param name="quartzJobInfo"></param>
|
|
/// <returns></returns>
|
|
protected virtual Task BeforeExecutionAsync(IServiceProvider serviceProvider, TQuartzJobInfo quartzJobInfo)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行后,退出
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
/// <param name="quartzJobInfo"></param>
|
|
/// <param name="result"></param>
|
|
/// <returns></returns>
|
|
protected virtual Task OnExitAsync(
|
|
IServiceProvider serviceProvider,
|
|
TQuartzJobInfo quartzJobInfo,
|
|
object? result)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 成功执行
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
/// <param name="quartzJobInfo"></param>
|
|
/// <param name="result"></param>
|
|
/// <returns></returns>
|
|
protected virtual Task OnSuccessAsync(IServiceProvider serviceProvider,
|
|
TQuartzJobInfo quartzJobInfo,
|
|
object? result)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异常处理
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
/// <param name="quartzJobInfo"></param>
|
|
/// <param name="exception"></param>
|
|
/// <returns></returns>
|
|
protected virtual Task OnExceptionAsync(
|
|
IServiceProvider serviceProvider,
|
|
TQuartzJobInfo quartzJobInfo,
|
|
Exception exception)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
}
|