Skip to content

Commit ac28225

Browse files
authored
Merge pull request #22714 from abpframework/GetBackgroundJobName
Add support for custom job names in `AbpBackgroundJobOptions`.
2 parents 18d1fea + a54462e commit ac28225

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

docs/en/framework/infrastructure/background-jobs/index.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ namespace MyProject
134134
}
135135
```
136136

137+
##### Custom Job Name
138+
139+
You can configure `GetBackgroundJobName` delegate of the `AbpBackgroundJobOptions` to change the default job name.
140+
141+
```csharp
142+
Configure<AbpBackgroundJobOptions>(options =>
143+
{
144+
options.GetBackgroundJobName = (jobType) =>
145+
{
146+
if (jobTyep == typeof(EmailSendingArgs))
147+
{
148+
return "emails";
149+
}
150+
151+
return BackgroundJobNameAttribute.GetName(jobType);
152+
};
153+
});
154+
```
155+
137156
### Queue a Job Item
138157

139158
Now, you can queue an email sending job using the `IBackgroundJobManager` service:

framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AbpBackgroundJobOptions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ public class AbpBackgroundJobOptions
1414
/// </summary>
1515
public bool IsJobExecutionEnabled { get; set; } = true;
1616

17+
/// <summary>
18+
/// The delegate to get the name of a background job.
19+
/// Default: <see cref="BackgroundJobNameAttribute.GetName"/>.
20+
/// </summary>
21+
public Func<Type, string> GetBackgroundJobName { get; set; }
22+
1723
public AbpBackgroundJobOptions()
1824
{
1925
_jobConfigurationsByArgsType = new Dictionary<Type, BackgroundJobConfiguration>();
2026
_jobConfigurationsByName = new Dictionary<string, BackgroundJobConfiguration>();
27+
GetBackgroundJobName = BackgroundJobNameAttribute.GetName;
2128
}
2229

2330
public BackgroundJobConfiguration GetJob<TArgs>()
@@ -61,7 +68,7 @@ public void AddJob<TJob>()
6168

6269
public void AddJob(Type jobType)
6370
{
64-
AddJob(new BackgroundJobConfiguration(jobType));
71+
AddJob(new BackgroundJobConfiguration(jobType, GetBackgroundJobName(jobType)));
6572
}
6673

6774
public void AddJob(BackgroundJobConfiguration jobConfiguration)

framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public class BackgroundJobConfiguration
1010

1111
public string JobName { get; }
1212

13-
public BackgroundJobConfiguration(Type jobType)
13+
public BackgroundJobConfiguration(Type jobType, string jobName)
1414
{
1515
JobType = jobType;
1616
ArgsType = BackgroundJobArgsHelper.GetJobArgsType(jobType);
17-
JobName = BackgroundJobNameAttribute.GetName(ArgsType);
17+
JobName = jobName;
1818
}
1919
}

framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/DefaultBackgroundJobManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@ public class DefaultBackgroundJobManager : IBackgroundJobManager, ITransientDepe
1818
protected IBackgroundJobSerializer Serializer { get; }
1919
protected IGuidGenerator GuidGenerator { get; }
2020
protected IBackgroundJobStore Store { get; }
21+
protected IOptions<AbpBackgroundJobOptions> BackgroundJobOptions { get; }
2122
protected IOptions<AbpBackgroundJobWorkerOptions> BackgroundJobWorkerOptions { get; }
2223

2324
public DefaultBackgroundJobManager(
2425
IClock clock,
2526
IBackgroundJobSerializer serializer,
2627
IBackgroundJobStore store,
2728
IGuidGenerator guidGenerator,
29+
IOptions<AbpBackgroundJobOptions> backgroundJobOptions,
2830
IOptions<AbpBackgroundJobWorkerOptions> backgroundJobWorkerOptions)
2931
{
3032
Clock = clock;
3133
Serializer = serializer;
3234
GuidGenerator = guidGenerator;
35+
BackgroundJobOptions = backgroundJobOptions;
3336
BackgroundJobWorkerOptions = backgroundJobWorkerOptions;
3437
Store = store;
3538
}
3639

3740
public virtual async Task<string> EnqueueAsync<TArgs>(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null)
3841
{
39-
var jobName = BackgroundJobNameAttribute.GetName<TArgs>();
42+
var jobName = BackgroundJobOptions.Value.GetBackgroundJobName(typeof(TArgs));
4043
var jobId = await EnqueueAsync(jobName, args!, priority, delay);
4144
return jobId.ToString();
4245
}

0 commit comments

Comments
 (0)