diff --git a/docs/en/framework/infrastructure/background-jobs/index.md b/docs/en/framework/infrastructure/background-jobs/index.md index 5aa58a7d3e8..af0c33de217 100644 --- a/docs/en/framework/infrastructure/background-jobs/index.md +++ b/docs/en/framework/infrastructure/background-jobs/index.md @@ -134,6 +134,25 @@ namespace MyProject } ``` +##### Custom Job Name + +You can configure `GetBackgroundJobName` delegate of the `AbpBackgroundJobOptions` to change the default job name. + +```csharp +Configure(options => +{ + options.GetBackgroundJobName = (jobType) => + { + if (jobTyep == typeof(EmailSendingArgs)) + { + return "emails"; + } + + return BackgroundJobNameAttribute.GetName(jobType); + }; +}); +``` + ### Queue a Job Item Now, you can queue an email sending job using the `IBackgroundJobManager` service: diff --git a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AbpBackgroundJobOptions.cs b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AbpBackgroundJobOptions.cs index 6b3aaa52b8a..f63bc2bb887 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AbpBackgroundJobOptions.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AbpBackgroundJobOptions.cs @@ -14,10 +14,17 @@ public class AbpBackgroundJobOptions /// public bool IsJobExecutionEnabled { get; set; } = true; + /// + /// The delegate to get the name of a background job. + /// Default: . + /// + public Func GetBackgroundJobName { get; set; } + public AbpBackgroundJobOptions() { _jobConfigurationsByArgsType = new Dictionary(); _jobConfigurationsByName = new Dictionary(); + GetBackgroundJobName = BackgroundJobNameAttribute.GetName; } public BackgroundJobConfiguration GetJob() @@ -61,7 +68,7 @@ public void AddJob() public void AddJob(Type jobType) { - AddJob(new BackgroundJobConfiguration(jobType)); + AddJob(new BackgroundJobConfiguration(jobType, GetBackgroundJobName(jobType))); } public void AddJob(BackgroundJobConfiguration jobConfiguration) diff --git a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobConfiguration.cs b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobConfiguration.cs index d9f08da2e70..c17ccdc4438 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobConfiguration.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobConfiguration.cs @@ -10,10 +10,10 @@ public class BackgroundJobConfiguration public string JobName { get; } - public BackgroundJobConfiguration(Type jobType) + public BackgroundJobConfiguration(Type jobType, string jobName) { JobType = jobType; ArgsType = BackgroundJobArgsHelper.GetJobArgsType(jobType); - JobName = BackgroundJobNameAttribute.GetName(ArgsType); + JobName = jobName; } } diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/DefaultBackgroundJobManager.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/DefaultBackgroundJobManager.cs index 68851c50432..586b66b5d17 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/DefaultBackgroundJobManager.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/DefaultBackgroundJobManager.cs @@ -18,6 +18,7 @@ public class DefaultBackgroundJobManager : IBackgroundJobManager, ITransientDepe protected IBackgroundJobSerializer Serializer { get; } protected IGuidGenerator GuidGenerator { get; } protected IBackgroundJobStore Store { get; } + protected IOptions BackgroundJobOptions { get; } protected IOptions BackgroundJobWorkerOptions { get; } public DefaultBackgroundJobManager( @@ -25,18 +26,20 @@ public DefaultBackgroundJobManager( IBackgroundJobSerializer serializer, IBackgroundJobStore store, IGuidGenerator guidGenerator, + IOptions backgroundJobOptions, IOptions backgroundJobWorkerOptions) { Clock = clock; Serializer = serializer; GuidGenerator = guidGenerator; + BackgroundJobOptions = backgroundJobOptions; BackgroundJobWorkerOptions = backgroundJobWorkerOptions; Store = store; } public virtual async Task EnqueueAsync(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null) { - var jobName = BackgroundJobNameAttribute.GetName(); + var jobName = BackgroundJobOptions.Value.GetBackgroundJobName(typeof(TArgs)); var jobId = await EnqueueAsync(jobName, args!, priority, delay); return jobId.ToString(); }