Browse Source

feat(tasks): 使作业运行时支持一次性的缓存数据.

pull/515/head
cKey 4 years ago
parent
commit
6baf5c70b8
  1. 34
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Abstractions/LINGYUN/Abp/BackgroundTasks/JobRunnableContext.cs
  2. 6
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobExecutorProvider.cs
  3. 16
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobListener.cs
  4. 4
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobSimpleAdapter.cs
  5. 13
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/Internal/InMemoryJobStore.cs
  6. 3
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/Internal/JobExecutedEvent.cs
  7. 14
      aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobStore.cs

34
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Abstractions/LINGYUN/Abp/BackgroundTasks/JobRunnableContext.cs

@ -9,18 +9,50 @@ public class JobRunnableContext
public IServiceProvider ServiceProvider { get; }
public IReadOnlyDictionary<string, object> JobData { get; }
public object Result { get; private set; }
private Func<object, object> GetCacheData { get; set; }
private Action<object, object> SetCacheData { get; set; }
public JobRunnableContext(
Type jobType,
IServiceProvider serviceProvider,
IReadOnlyDictionary<string, object> jobData)
IReadOnlyDictionary<string, object> jobData,
Func<object, object> getCache = null,
Action<object, object> setCache = null)
{
JobType = jobType;
ServiceProvider = serviceProvider;
JobData = jobData;
GetCacheData = getCache;
SetCacheData = setCache;
}
public void SetResult(object result)
{
Result = result;
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void SetCache(object key, object value)
{
SetCacheData?.Invoke(key, value);
}
#nullable enable
/// <summary>
/// 获取缓存数据
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object? GetCache(object key)
{
if (GetCacheData != null)
{
return GetCacheData(key);
}
return null;
}
#nullable disable
}

6
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobExecutorProvider.cs

@ -53,11 +53,11 @@ public class QuartzJobExecutorProvider : IQuartzJobExecutorProvider, ISingletonD
jobBuilder.UsingJobData(nameof(JobInfo.Name), job.Name);
jobBuilder.UsingJobData(nameof(JobInfo.Type), job.Type);
jobBuilder.UsingJobData(nameof(JobInfo.Group), job.Group);
// 计算增量需要
jobBuilder.UsingJobData(nameof(JobInfo.TriggerCount), job.TriggerCount);
jobBuilder.UsingJobData(nameof(JobInfo.MaxCount), job.MaxCount);
// 独占任务需要
jobBuilder.UsingJobData(nameof(JobInfo.LockTimeOut), job.LockTimeOut);
// 加入触发次数, 某些场景需要使用增量
jobBuilder.UsingJobData(nameof(JobInfo.TriggerCount), job.TriggerCount);
jobBuilder.UsingJobData(nameof(JobInfo.TryCount), job.TryCount);
// 传递的作业参数
jobBuilder.UsingJobData(new JobDataMap(job.Args));
if (job.TenantId.HasValue)

16
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobListener.cs

@ -36,7 +36,7 @@ public class QuartzJobListener : JobListenerSupport, ISingletonDependency
var jobType = context.JobDetail.JobType;
if (jobType.IsGenericType)
{
jobType = jobType.GetGenericTypeDefinition();
jobType = jobType.GetGenericArguments()[0];
}
Logger.LogInformation($"The task {jobType.Name} could not be performed...");
@ -53,6 +53,20 @@ public class QuartzJobListener : JobListenerSupport, ISingletonDependency
return;
}
if (context.Trigger is ISimpleTrigger simpleTrigger)
{
// 增量数据写入临时数据
if (context.MergedJobDataMap.TryGetValue(nameof(JobInfo.TriggerCount), out var count) &&
int.TryParse(count?.ToString(), out var triggerCount))
{
context.Put(nameof(JobInfo.TriggerCount), triggerCount + simpleTrigger.TimesTriggered);
}
if (context.MergedJobDataMap.TryGetValue(nameof(JobInfo.MaxCount), out var maxCount))
{
context.Put(nameof(JobInfo.MaxCount), maxCount);
}
}
using var scope = ServiceScopeFactory.CreateScope();
var jobEventData = new JobEventData(
jobId,

4
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobSimpleAdapter.cs

@ -24,7 +24,9 @@ public class QuartzJobSimpleAdapter<TJobRunnable> : IJob
var jobContext = new JobRunnableContext(
typeof(TJobRunnable),
scope.ServiceProvider,
context.MergedJobDataMap.ToImmutableDictionary());
context.MergedJobDataMap.ToImmutableDictionary(),
getCache: (key) => context.Get(key),
setCache: context.Put);
await jobExecuter.ExecuteAsync(jobContext);

13
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/Internal/InMemoryJobStore.cs

@ -66,19 +66,6 @@ internal class InMemoryJobStore : IJobStore, ISingletonDependency
job.TriggerCount = jobInfo.TriggerCount;
job.TryCount = jobInfo.TryCount;
job.IsAbandoned = jobInfo.IsAbandoned;
foreach (var arg in jobInfo.Args)
{
if (!job.Args.ContainsKey(arg.Key))
{
job.Args[arg.Key] = arg.Value;
continue;
}
if (job.Args.TryGetValue(arg.Key, out var value) &&
!Equals(value, arg.Value))
{
job.Args[arg.Key] = arg.Value;
}
}
}
else
{

3
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/Internal/JobExecutedEvent.cs

@ -24,7 +24,6 @@ public class JobExecutedEvent : JobEventBase<JobExecutedEvent>, ITransientDepend
job.NextRunTime = context.EventData.NextRunTime;
job.Result = context.EventData.Result ?? "OK";
job.Status = JobStatus.Running;
job.Args[nameof(JobInfo.TriggerCount)] = job.TriggerCount;
// 一次性任务执行一次后标记为已完成
if (job.JobType == JobType.Once)
@ -36,7 +35,6 @@ public class JobExecutedEvent : JobEventBase<JobExecutedEvent>, ITransientDepend
if (context.EventData.Exception != null)
{
job.TryCount += 1;
job.Args[nameof(JobInfo.TryCount)] = job.TryCount;
job.IsAbandoned = false;
job.Result = GetExceptionMessage(context.EventData.Exception);
@ -70,7 +68,6 @@ public class JobExecutedEvent : JobEventBase<JobExecutedEvent>, ITransientDepend
{
// 成功一次重置重试次数
job.TryCount = 0;
job.Args[nameof(JobInfo.TryCount)] = job.TryCount;
// 所有任务达到上限则标记已完成
if (job.MaxCount > 0 && job.TriggerCount >= job.MaxCount)

14
aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobStore.cs

@ -66,20 +66,6 @@ public class BackgroundJobStore : IJobStore, ITransientDependency
backgroundJobInfo.TryCount = jobInfo.TryCount;
backgroundJobInfo.IsAbandoned = jobInfo.IsAbandoned;
foreach (var arg in jobInfo.Args)
{
if (!backgroundJobInfo.Args.ContainsKey(arg.Key))
{
backgroundJobInfo.Args[arg.Key] = arg.Value;
continue;
}
if (backgroundJobInfo.Args.TryGetValue(arg.Key, out var value) &&
!Equals(value, arg.Value))
{
backgroundJobInfo.Args[arg.Key] = arg.Value;
}
}
await JobInfoRepository.UpdateAsync(backgroundJobInfo);
}
else

Loading…
Cancel
Save