Skip to content

Add support for custom job names in AbpBackgroundJobOptions. #22714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/en/framework/infrastructure/background-jobs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbpBackgroundJobOptions>(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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ public class AbpBackgroundJobOptions
/// </summary>
public bool IsJobExecutionEnabled { get; set; } = true;

/// <summary>
/// The delegate to get the name of a background job.
/// Default: <see cref="BackgroundJobNameAttribute.GetName"/>.
/// </summary>
public Func<Type, string> GetBackgroundJobName { get; set; }

public AbpBackgroundJobOptions()
{
_jobConfigurationsByArgsType = new Dictionary<Type, BackgroundJobConfiguration>();
_jobConfigurationsByName = new Dictionary<string, BackgroundJobConfiguration>();
GetBackgroundJobName = BackgroundJobNameAttribute.GetName;
}

public BackgroundJobConfiguration GetJob<TArgs>()
Expand Down Expand Up @@ -61,7 +68,7 @@ public void AddJob<TJob>()

public void AddJob(Type jobType)
{
AddJob(new BackgroundJobConfiguration(jobType));
AddJob(new BackgroundJobConfiguration(jobType, GetBackgroundJobName(jobType)));
}

public void AddJob(BackgroundJobConfiguration jobConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@ public class DefaultBackgroundJobManager : IBackgroundJobManager, ITransientDepe
protected IBackgroundJobSerializer Serializer { get; }
protected IGuidGenerator GuidGenerator { get; }
protected IBackgroundJobStore Store { get; }
protected IOptions<AbpBackgroundJobOptions> BackgroundJobOptions { get; }
protected IOptions<AbpBackgroundJobWorkerOptions> BackgroundJobWorkerOptions { get; }

public DefaultBackgroundJobManager(
IClock clock,
IBackgroundJobSerializer serializer,
IBackgroundJobStore store,
IGuidGenerator guidGenerator,
IOptions<AbpBackgroundJobOptions> backgroundJobOptions,
IOptions<AbpBackgroundJobWorkerOptions> backgroundJobWorkerOptions)
{
Clock = clock;
Serializer = serializer;
GuidGenerator = guidGenerator;
BackgroundJobOptions = backgroundJobOptions;
BackgroundJobWorkerOptions = backgroundJobWorkerOptions;
Store = store;
}

public virtual async Task<string> EnqueueAsync<TArgs>(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null)
{
var jobName = BackgroundJobNameAttribute.GetName<TArgs>();
var jobName = BackgroundJobOptions.Value.GetBackgroundJobName(typeof(TArgs));
var jobId = await EnqueueAsync(jobName, args!, priority, delay);
return jobId.ToString();
}
Expand Down
Loading