-
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.
Merge pull request #6200 from elsa-workflows/bug/quartz-scheduled-once
Fix Quartz scheduler implementation
- Loading branch information
Showing
6 changed files
with
100 additions
and
40 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
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); | ||
} | ||
} |