Browse Source
Merge pull request #513 from colinin/increment-background-tasks
feat(background-tasks): 将触发次数写入作业参数中,以支持增量计算场景.
pull/533/head
yx lin
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
34 additions and
0 deletions
-
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobExecutorProvider.cs
-
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/Internal/InMemoryJobStore.cs
-
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/Internal/JobExecutedEvent.cs
-
aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobStore.cs
|
|
|
@ -55,6 +55,9 @@ public class QuartzJobExecutorProvider : IQuartzJobExecutorProvider, ISingletonD |
|
|
|
jobBuilder.UsingJobData(nameof(JobInfo.Group), job.Group); |
|
|
|
// 独占任务需要
|
|
|
|
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) |
|
|
|
|
|
|
|
@ -66,6 +66,19 @@ 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 |
|
|
|
{ |
|
|
|
|
|
|
|
@ -24,6 +24,7 @@ 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) |
|
|
|
@ -35,6 +36,7 @@ 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); |
|
|
|
|
|
|
|
@ -68,6 +70,7 @@ public class JobExecutedEvent : JobEventBase<JobExecutedEvent>, ITransientDepend |
|
|
|
{ |
|
|
|
// 成功一次重置重试次数
|
|
|
|
job.TryCount = 0; |
|
|
|
job.Args[nameof(JobInfo.TryCount)] = job.TryCount; |
|
|
|
|
|
|
|
// 所有任务达到上限则标记已完成
|
|
|
|
if (job.MaxCount > 0 && job.TriggerCount >= job.MaxCount) |
|
|
|
|
|
|
|
@ -3,6 +3,7 @@ using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
using Volo.Abp.MultiTenancy; |
|
|
|
using Volo.Abp.ObjectMapping; |
|
|
|
@ -65,6 +66,20 @@ 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 |
|
|
|
|