-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce JobKeyProvider for managing Quartz job keys
Added JobKeyProvider to centralize job key and group name handling, simplifying Quartz job scheduling. Refactored QuartzWorkflowScheduler to use the new provider, improving maintainability. Updated QuartzSchedulerFeature to register the new provider and a startup task for job registration.
- Loading branch information
1 parent
9725be0
commit 96bf791
Showing
5 changed files
with
99 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Quartz; | ||
|
||
namespace Elsa.Quartz.Contracts; | ||
|
||
internal interface IJobKeyProvider | ||
{ | ||
JobKey GetJobKey<TJob>() where TJob : IJob; | ||
string GetGroupName(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Elsa.Common.Multitenancy; | ||
using Elsa.Quartz.Contracts; | ||
using Quartz; | ||
|
||
namespace Elsa.Quartz.Services; | ||
|
||
internal class JobKeyProvider(ITenantAccessor tenantAccessor) : IJobKeyProvider | ||
{ | ||
public JobKey GetJobKey<TJob>() where TJob : IJob | ||
{ | ||
return new(typeof(TJob).Name, GetGroupName()); | ||
} | ||
|
||
public string GetGroupName() | ||
{ | ||
var tenantId = tenantAccessor.Tenant?.Id; | ||
return string.IsNullOrWhiteSpace(tenantId) ? "Default" : tenantId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Elsa.Common; | ||
using Elsa.Quartz.Contracts; | ||
using Elsa.Quartz.Jobs; | ||
using JetBrains.Annotations; | ||
using Quartz; | ||
|
||
namespace Elsa.Quartz.Tasks; | ||
|
||
/// <summary> | ||
/// Registers the Quartz jobs. | ||
/// </summary> | ||
/// <param name="schedulerFactoryFactory"></param> | ||
/// <param name="jobKeyProvider"></param> | ||
[UsedImplicitly] | ||
internal class RegisterJobsTask(ISchedulerFactory schedulerFactoryFactory, IJobKeyProvider jobKeyProvider) : IStartupTask | ||
{ | ||
public async Task ExecuteAsync(CancellationToken cancellationToken) | ||
{ | ||
var scheduler = await schedulerFactoryFactory.GetScheduler(cancellationToken); | ||
await CreateJobAsync<RunWorkflowJob>(scheduler, cancellationToken); | ||
await CreateJobAsync<ResumeWorkflowJob>(scheduler, cancellationToken); | ||
} | ||
|
||
private async Task CreateJobAsync<TJobType>(IScheduler scheduler, CancellationToken cancellationToken) where TJobType : IJob | ||
{ | ||
var key = jobKeyProvider.GetJobKey<TJobType>(); | ||
var job = JobBuilder.Create<TJobType>() | ||
.WithIdentity(key) | ||
.StoreDurably() | ||
.Build(); | ||
|
||
if (!await scheduler.CheckExists(job.Key, cancellationToken)) | ||
await scheduler.AddJob(job, false, cancellationToken); | ||
} | ||
} |