-
Notifications
You must be signed in to change notification settings - Fork 33
Make Quartz.NET Jobs Resilient #104
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
07855e9
Add transient exception detection and handling to Quartz module
sfmskywalker 0e88ebf
Add unit and component tests for Quartz scheduling module
sfmskywalker 61bd60a
Refactor transient exception handling to shared resilience module.
sfmskywalker d5397ac
Merge remote-tracking branch 'origin/enh/102' into enh/102
sfmskywalker 44ff831
Refactor Quartz module to use shared resilience module for transient …
sfmskywalker 856a2db
Remove database-specific transient exception detectors from schedulin…
sfmskywalker 7ced00f
Update workflow, package versions, and add `Elsa.Resilience.Core` pac…
sfmskywalker 3da73e3
Remove Quartz transient exception tests and integrate resilience modu…
sfmskywalker d118855
Update Elsa package version to 3.7.0-preview.4150
sfmskywalker 01d427f
Remove backup solution file `Elsa.Extensions.sln.bak`.
sfmskywalker 2affc3c
Refactor Quartz job retry logic to use `TimeSpan` for delay configura…
sfmskywalker 6cc26ce
Refactor `ResumeWorkflowJob` to use `IQuartzJobRetryScheduler` for re…
sfmskywalker c87a760
Refactor `RunWorkflowJob` to use `IQuartzJobRetryScheduler` for retry…
sfmskywalker 6091d75
Register `IQuartzJobRetryScheduler` as a singleton in Quartz Schedule…
sfmskywalker c9e7be2
Refactor `QuartzJobTransientRetryTests` to simplify job setup and par…
sfmskywalker 9061553
Parameterize `ResumeWorkflowJobTests` transient and non-transient exc…
sfmskywalker 9adcc21
Parameterize `RunWorkflowJobTests` exception scenarios; replace `IOpt…
sfmskywalker 9ccf385
Remove `QuartzJobExtensions` and its retry logic in favor of `IQuartz…
sfmskywalker ec7a4d9
Introduce `IQuartzJobRetryScheduler` and its default implementation; …
sfmskywalker ed2756d
Register scheduling configuration using Quartz in `WorkflowServer` te…
sfmskywalker f216d5b
Initial plan
Copilot 32d36bd
Initial plan
Copilot 23625be
Fix resource leak by disposing Scope in AppComponentTest
Copilot 75c1549
Add null-conditional operator to Scope disposal for extra safety
Copilot 6f74cdc
Address feedback: use manual job instance consistently in component t…
Copilot edb24a9
Merge pull request #105 from elsa-workflows/copilot/sub-pr-104
sfmskywalker 2e30901
Merge pull request #106 from elsa-workflows/copilot/sub-pr-104-again
sfmskywalker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
src/modules/scheduling/Elsa.Scheduling.Quartz/Contracts/IQuartzJobRetryScheduler.cs
This file contains hidden or 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,14 @@ | ||
| using Quartz; | ||
|
|
||
| namespace Elsa.Scheduling.Quartz.Contracts; | ||
|
|
||
| /// <summary> | ||
| /// Schedules retries for Quartz jobs. | ||
| /// </summary> | ||
| public interface IQuartzJobRetryScheduler | ||
| { | ||
| /// <summary> | ||
| /// Reschedules the trigger of the current job for a retry using the configured retry delay. | ||
| /// </summary> | ||
| Task ScheduleRetryAsync(IJobExecutionContext context, CancellationToken cancellationToken = default); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
12 changes: 12 additions & 0 deletions
12
src/modules/scheduling/Elsa.Scheduling.Quartz/Options/QuartzJobOptions.cs
This file contains hidden or 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,12 @@ | ||
| namespace Elsa.Scheduling.Quartz.Options; | ||
|
|
||
| /// <summary> | ||
| /// Options for Quartz job execution behavior. | ||
| /// </summary> | ||
| public class QuartzJobOptions | ||
| { | ||
| /// <summary> | ||
| /// The delay in seconds before rescheduling a job after a transient failure. | ||
| /// </summary> | ||
| public TimeSpan TransientExceptionRetryDelay { get; set; } = TimeSpan.FromSeconds(10); | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/modules/scheduling/Elsa.Scheduling.Quartz/Services/QuartzJobRetryScheduler.cs
This file contains hidden or 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,28 @@ | ||
| using Elsa.Common; | ||
| using Elsa.Scheduling.Quartz.Contracts; | ||
| using Elsa.Scheduling.Quartz.Options; | ||
| using Microsoft.Extensions.Options; | ||
| using Quartz; | ||
|
|
||
| namespace Elsa.Scheduling.Quartz.Services; | ||
|
|
||
| /// <summary> | ||
| /// Default implementation of <see cref="IQuartzJobRetryScheduler"/>. | ||
| /// </summary> | ||
| public class QuartzJobRetryScheduler(ISystemClock systemClock, IOptions<QuartzJobOptions> options) : IQuartzJobRetryScheduler | ||
| { | ||
| /// <inheritdoc /> | ||
| public async Task ScheduleRetryAsync(IJobExecutionContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| var delay = options.Value.TransientExceptionRetryDelay; | ||
| var now = systemClock.UtcNow; | ||
|
|
||
| var trigger = TriggerBuilder.Create() | ||
| .ForJob(context.JobDetail.Key) | ||
| .StartAt(now.Add(delay)) | ||
| .Build(); | ||
|
|
||
| await context.Scheduler.RescheduleJob(context.Trigger.Key, trigger, cancellationToken); | ||
| } | ||
| } | ||
|
|
22 changes: 22 additions & 0 deletions
22
...modules/scheduling/Elsa.Scheduling.Quartz.ComponentTests/Abstractions/AppComponentTest.cs
This file contains hidden or 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,22 @@ | ||
| using Elsa.Scheduling.Quartz.ComponentTests.Fixtures; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Elsa.Scheduling.Quartz.ComponentTests.Abstractions; | ||
|
|
||
| [Collection(nameof(SchedulingAppCollection))] | ||
| public abstract class AppComponentTest(SchedulingApp app) : IDisposable | ||
| { | ||
| protected WorkflowServer WorkflowServer { get; } = app.WorkflowServer; | ||
| protected IServiceScope Scope { get; private set; } = app.WorkflowServer.Services.CreateScope(); | ||
|
|
||
| public void Dispose() | ||
| { | ||
| OnDispose(); | ||
| Scope?.Dispose(); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
|
|
||
| protected virtual void OnDispose() | ||
| { | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
...duling/Elsa.Scheduling.Quartz.ComponentTests/Elsa.Scheduling.Quartz.ComponentTests.csproj
This file contains hidden or 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,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Elsa.Testing.Shared.Component" /> | ||
| <PackageReference Include="Elsa.Workflows.Core" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\src\modules\scheduling\Elsa.Scheduling.Quartz\Elsa.Scheduling.Quartz.csproj" /> | ||
| <ProjectReference Include="..\..\..\workbench\Elsa.TestServer.Web\Elsa.TestServer.Web.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
16 changes: 16 additions & 0 deletions
16
test/modules/scheduling/Elsa.Scheduling.Quartz.ComponentTests/Fixtures/SchedulingApp.cs
This file contains hidden or 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,16 @@ | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace Elsa.Scheduling.Quartz.ComponentTests.Fixtures; | ||
|
|
||
| [UsedImplicitly] | ||
| public class SchedulingApp : IAsyncLifetime | ||
| { | ||
| public WorkflowServer WorkflowServer { get; } = new("http://localhost:5010"); | ||
|
|
||
| public Task InitializeAsync() => Task.CompletedTask; | ||
|
|
||
| public async Task DisposeAsync() | ||
| { | ||
| await WorkflowServer.DisposeAsync(); | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
...ules/scheduling/Elsa.Scheduling.Quartz.ComponentTests/Fixtures/SchedulingAppCollection.cs
This file contains hidden or 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,4 @@ | ||
| namespace Elsa.Scheduling.Quartz.ComponentTests.Fixtures; | ||
|
|
||
| [CollectionDefinition(nameof(SchedulingAppCollection))] | ||
| public class SchedulingAppCollection : ICollectionFixture<SchedulingApp>; |
38 changes: 38 additions & 0 deletions
38
test/modules/scheduling/Elsa.Scheduling.Quartz.ComponentTests/Fixtures/WorkflowServer.cs
This file contains hidden or 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,38 @@ | ||
| using Elsa.Extensions; | ||
| using Elsa.Scheduling.Quartz.Options; | ||
| using JetBrains.Annotations; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Quartz; | ||
|
|
||
| namespace Elsa.Scheduling.Quartz.ComponentTests.Fixtures; | ||
|
|
||
| [UsedImplicitly] | ||
| public class WorkflowServer(string url) : WebApplicationFactory<Program> | ||
| { | ||
| public async Task<global::Quartz.IScheduler> GetSchedulerAsync() | ||
| { | ||
| var factory = Services.GetRequiredService<ISchedulerFactory>(); | ||
| return await factory.GetScheduler(); | ||
| } | ||
|
|
||
| protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
| { | ||
| builder.UseUrls(url); | ||
|
|
||
| TestServer.Web.Program.ConfigureForTest ??= elsa => | ||
| { | ||
| elsa.UseQuartz(); | ||
| elsa.UseScheduling(scheduling => scheduling.UseQuartzScheduler()); | ||
| elsa.UseWorkflowRuntime(); | ||
| }; | ||
|
|
||
| builder.ConfigureTestServices(services => | ||
| { | ||
| // Configure Quartz job options for fast retries in tests | ||
| services.Configure<QuartzJobOptions>(options => options.TransientExceptionRetryDelay = TimeSpan.FromSeconds(1)); | ||
| }); | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...odules/scheduling/Elsa.Scheduling.Quartz.ComponentTests/Helpers/FailingWorkflowStarter.cs
This file contains hidden or 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,32 @@ | ||
| using Elsa.Workflows.Runtime; | ||
|
|
||
| namespace Elsa.Scheduling.Quartz.ComponentTests.Helpers; | ||
|
|
||
| /// <summary> | ||
| /// A workflow starter that can be configured to fail with transient or non-transient exceptions. | ||
| /// </summary> | ||
| public class FailingWorkflowStarter(IWorkflowStarter innerStarter) : IWorkflowStarter | ||
| { | ||
| private int _callCount; | ||
|
|
||
| public int FailuresBeforeSuccess { get; set; } | ||
| public Exception? ExceptionToThrow { get; set; } | ||
| public int CallCount => _callCount; | ||
|
|
||
| public async Task<StartWorkflowResponse> StartWorkflowAsync(StartWorkflowRequest request, CancellationToken cancellationToken = default) | ||
| { | ||
| _callCount++; | ||
|
|
||
| if (ExceptionToThrow != null && _callCount <= FailuresBeforeSuccess) | ||
| { | ||
| throw ExceptionToThrow; | ||
| } | ||
|
|
||
| return await innerStarter.StartWorkflowAsync(request, cancellationToken); | ||
| } | ||
|
|
||
| public void Reset() | ||
| { | ||
| _callCount = 0; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.