Skip to content
Merged
Show file tree
Hide file tree
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 Dec 22, 2025
0e88ebf
Add unit and component tests for Quartz scheduling module
sfmskywalker Dec 22, 2025
61bd60a
Refactor transient exception handling to shared resilience module.
sfmskywalker Dec 23, 2025
d5397ac
Merge remote-tracking branch 'origin/enh/102' into enh/102
sfmskywalker Dec 24, 2025
44ff831
Refactor Quartz module to use shared resilience module for transient …
sfmskywalker Dec 24, 2025
856a2db
Remove database-specific transient exception detectors from schedulin…
sfmskywalker Dec 24, 2025
7ced00f
Update workflow, package versions, and add `Elsa.Resilience.Core` pac…
sfmskywalker Dec 24, 2025
3da73e3
Remove Quartz transient exception tests and integrate resilience modu…
sfmskywalker Dec 24, 2025
d118855
Update Elsa package version to 3.7.0-preview.4150
sfmskywalker Dec 24, 2025
01d427f
Remove backup solution file `Elsa.Extensions.sln.bak`.
sfmskywalker Dec 24, 2025
2affc3c
Refactor Quartz job retry logic to use `TimeSpan` for delay configura…
sfmskywalker Dec 24, 2025
6cc26ce
Refactor `ResumeWorkflowJob` to use `IQuartzJobRetryScheduler` for re…
sfmskywalker Dec 24, 2025
c87a760
Refactor `RunWorkflowJob` to use `IQuartzJobRetryScheduler` for retry…
sfmskywalker Dec 24, 2025
6091d75
Register `IQuartzJobRetryScheduler` as a singleton in Quartz Schedule…
sfmskywalker Dec 24, 2025
c9e7be2
Refactor `QuartzJobTransientRetryTests` to simplify job setup and par…
sfmskywalker Dec 24, 2025
9061553
Parameterize `ResumeWorkflowJobTests` transient and non-transient exc…
sfmskywalker Dec 24, 2025
9adcc21
Parameterize `RunWorkflowJobTests` exception scenarios; replace `IOpt…
sfmskywalker Dec 24, 2025
9ccf385
Remove `QuartzJobExtensions` and its retry logic in favor of `IQuartz…
sfmskywalker Dec 24, 2025
ec7a4d9
Introduce `IQuartzJobRetryScheduler` and its default implementation; …
sfmskywalker Dec 24, 2025
ed2756d
Register scheduling configuration using Quartz in `WorkflowServer` te…
sfmskywalker Dec 24, 2025
f216d5b
Initial plan
Copilot Dec 24, 2025
32d36bd
Initial plan
Copilot Dec 24, 2025
23625be
Fix resource leak by disposing Scope in AppComponentTest
Copilot Dec 24, 2025
75c1549
Add null-conditional operator to Scope disposal for extra safety
Copilot Dec 24, 2025
6f74cdc
Address feedback: use manual job instance consistently in component t…
Copilot Dec 24, 2025
edb24a9
Merge pull request #105 from elsa-workflows/copilot/sub-pr-104
sfmskywalker Dec 24, 2025
2e30901
Merge pull request #106 from elsa-workflows/copilot/sub-pr-104-again
sfmskywalker Dec 24, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
release:
types: [ prereleased, published ]
env:
base_version: '3.6.0'
base_version: '3.7.0'
feedz_feed_source: 'https://f.feedz.io/elsa-workflows/elsa-3/nuget/index.json'
nuget_feed_source: 'https://api.nuget.org/v3/index.json'

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</PropertyGroup>

<PropertyGroup Label="PackageVersions">
<ElsaVersion>3.6.0-rc1</ElsaVersion>
<ElsaVersion>3.7.0-preview.4150</ElsaVersion>
<ElsaStudioVersion>3.6.0-rc1</ElsaStudioVersion>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageVersion Include="Elsa.Persistence.EFCore.Common" Version="$(ElsaVersion)"/>
<PackageVersion Include="Elsa.Persistence.EFCore" Version="$(ElsaVersion)"/>
<PackageVersion Include="Elsa.Resilience" Version="$(ElsaVersion)"/>
<PackageVersion Include="Elsa.Resilience.Core" Version="$(ElsaVersion)"/>
<PackageVersion Include="Elsa.Tenants" Version="$(ElsaVersion)"/>
<PackageVersion Include="Elsa.Tenants.AspNetCore" Version="$(ElsaVersion)"/>
<PackageVersion Include="Elsa.Testing.Shared" Version="$(ElsaVersion)"/>
Expand Down
839 changes: 818 additions & 21 deletions Elsa.Extensions.sln

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static QuartzFeature UseMySql(this QuartzFeature feature, string connecti
store.UseClustering();
});
};

feature.Module.ConfigureHostedService<RunMigrationsHostedService<MySqlQuartzDbContext>>(-100);

return feature;
Expand Down
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

<ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' != 'true'">
<PackageReference Include="Elsa.Scheduling"/>
<PackageReference Include="Elsa.Resilience.Core"/>
</ItemGroup>

<ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' == 'true'">
<ProjectReference Include="..\..\..\..\..\elsa-core\src\modules\Elsa.Scheduling\Elsa.Scheduling.csproj"/>
<ProjectReference Include="..\..\..\..\..\elsa-core\src\modules\Elsa.Resilience.Core\Elsa.Resilience.Core.csproj"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using Elsa.Features.Abstractions;
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.Scheduling.Quartz.Contracts;
using Elsa.Scheduling.Quartz.Handlers;
using Elsa.Scheduling.Quartz.Options;
using Elsa.Scheduling.Quartz.Services;
using Elsa.Scheduling.Quartz.Tasks;
using Elsa.Scheduling.Features;
using Elsa.Scheduling.Quartz.Contracts;
using Elsa.Workflows;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Quartz;

Expand All @@ -17,8 +19,18 @@ namespace Elsa.Scheduling.Quartz.Features;
/// A feature that installs Quartz.NET implementations for <see cref="IWorkflowScheduler"/>.
/// </summary>
[DependsOn(typeof(SchedulingFeature))]
[UsedImplicitly]
public class QuartzSchedulerFeature(IModule module) : FeatureBase(module)
{
private Action<QuartzJobOptions> _configureQuartzJobOptions = _ => { };

[PublicAPI]
public QuartzSchedulerFeature ConfigureOptions(Action<QuartzJobOptions> configure)
{
_configureQuartzJobOptions += configure;
return this;
}

/// <inheritdoc />
public override void Configure()
{
Expand All @@ -40,7 +52,10 @@ public override void Apply()
.AddSingleton<QuartzCronParser>()
.AddScoped<QuartzWorkflowScheduler>()
.AddScoped<IJobKeyProvider, JobKeyProvider>()
.AddSingleton<IQuartzJobRetryScheduler, QuartzJobRetryScheduler>()
.AddStartupTask<RegisterJobsTask>()
.AddQuartz();

Services.Configure(_configureQuartzJobOptions);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
using Elsa.Common;
using Elsa.Common.Multitenancy;
using Elsa.Extensions;
using Elsa.Resilience;
using Elsa.Scheduling.Quartz.Contracts;
using Elsa.Workflows.Models;
using Elsa.Workflows.Runtime;
using Elsa.Workflows.Runtime.Messages;
using Microsoft.Extensions.Logging;
using Quartz;

namespace Elsa.Scheduling.Quartz.Jobs;

/// <summary>
/// A job that resumes a workflow.
/// </summary>
public class ResumeWorkflowJob(IWorkflowRuntime workflowRuntime, IJsonSerializer jsonSerializer, ITenantFinder tenantFinder, ITenantAccessor tenantAccessor) : IJob
public class ResumeWorkflowJob(
IWorkflowRuntime workflowRuntime,
IJsonSerializer jsonSerializer,
ITenantFinder tenantFinder,
ITenantAccessor tenantAccessor,
ITransientExceptionDetector transientExceptionDetector,
IQuartzJobRetryScheduler retryScheduler,
ILogger<ResumeWorkflowJob> logger) : IJob
{
/// <inheritdoc />
public async Task Execute(IJobExecutionContext context)
Expand All @@ -23,15 +33,32 @@ public async Task Execute(IJobExecutionContext context)
var serializedActivityHandle = (string)map.Get(nameof(ScheduleExistingWorkflowInstanceRequest.ActivityHandle));
var activityHandle = serializedActivityHandle != null! ? jsonSerializer.Deserialize<ActivityHandle>(serializedActivityHandle) : null;
var workflowInstanceId = (string)map.Get(nameof(ScheduleExistingWorkflowInstanceRequest.WorkflowInstanceId));
var workflowClient = await workflowRuntime.CreateClientAsync(workflowInstanceId, context.CancellationToken);
var request = new RunWorkflowInstanceRequest
var cancellationToken = context.CancellationToken;

try
{
var workflowClient = await workflowRuntime.CreateClientAsync(workflowInstanceId, cancellationToken);
var request = new RunWorkflowInstanceRequest
{
BookmarkId = (string)map.Get(nameof(ScheduleExistingWorkflowInstanceRequest.BookmarkId)),
ActivityHandle = activityHandle,
Input = map.GetDictionary(nameof(ScheduleExistingWorkflowInstanceRequest.Input)),
Properties = map.GetDictionary(nameof(ScheduleExistingWorkflowInstanceRequest.Properties)),
};
await workflowClient.RunInstanceAsync(request, cancellationToken: cancellationToken);

logger.LogInformation("Resumed workflow instance {WorkflowInstanceId}", workflowInstanceId);
}
catch (Exception e) when (transientExceptionDetector.IsTransient(e))
{
logger.LogWarning(e, "A transient error occurred while resuming workflow instance {WorkflowInstanceId}. Rescheduling job for retry", workflowInstanceId);
await retryScheduler.ScheduleRetryAsync(context, cancellationToken);
}
catch (Exception e)
{
BookmarkId = (string)map.Get(nameof(ScheduleExistingWorkflowInstanceRequest.BookmarkId)),
ActivityHandle = activityHandle,
Input = map.GetDictionary(nameof(ScheduleExistingWorkflowInstanceRequest.Input)),
Properties = map.GetDictionary(nameof(ScheduleExistingWorkflowInstanceRequest.Properties)),
};
await workflowClient.RunInstanceAsync(request, cancellationToken: context.CancellationToken);
logger.LogError(e, "An error occurred while resuming workflow instance {WorkflowInstanceId}", workflowInstanceId);
await context.Scheduler.DeleteJob(context.JobDetail.Key, cancellationToken);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Elsa.Common.Multitenancy;
using Elsa.Extensions;
using Elsa.Resilience;
using Elsa.Scheduling.Quartz.Contracts;
using Elsa.Workflows.Models;
using Elsa.Workflows.Runtime;
using Elsa.Workflows.Runtime.Exceptions;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Quartz;

Expand All @@ -11,10 +14,13 @@ namespace Elsa.Scheduling.Quartz.Jobs;
/// <summary>
/// A job that runs a workflow.
/// </summary>
[UsedImplicitly]
public class RunWorkflowJob(
ITenantAccessor tenantAccessor,
ITenantFinder tenantFinder,
IWorkflowStarter workflowStarter,
ITransientExceptionDetector transientExceptionDetector,
IQuartzJobRetryScheduler retryScheduler,
ILogger<RunWorkflowJob> logger) : IJob
{
/// <inheritdoc />
Expand Down Expand Up @@ -54,6 +60,11 @@ public async Task Execute(IJobExecutionContext context)
logger.LogWarning(e, "Could not find workflow graph for workflow definition handle {WorkflowDefinitionHandle}", startRequest.WorkflowDefinitionHandle);
await context.Scheduler.DeleteJob(context.JobDetail.Key, cancellationToken);
}
catch (Exception e) when (transientExceptionDetector.IsTransient(e))
{
logger.LogWarning(e, "A transient error occurred while starting workflow {WorkflowDefinitionHandle} with correlation ID {CorrelationId}. Rescheduling job for retry", startRequest.WorkflowDefinitionHandle, startRequest.CorrelationId);
await retryScheduler.ScheduleRetryAsync(context, cancellationToken);
}
catch (Exception e)
{
logger.LogError(e, "An error occurred while starting workflow {WorkflowDefinitionHandle} with correlation ID {CorrelationId}", startRequest.WorkflowDefinitionHandle, startRequest.CorrelationId);
Expand Down
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);
}
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);
}
}

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()
{
}
}
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>
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();
}
}
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>;
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));
});
}
}
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;
}
}
Loading
Loading