Extract Quartz registration extensions into separate methods

This allows the caller to control whether or not Quartz itself should be registered.
This commit is contained in:
Sipke Schoorstra 2021-04-02 11:48:08 +02:00
parent 23e3d1fd81
commit 1dfa36b9c8
2 changed files with 35 additions and 23 deletions

View file

@ -23,13 +23,13 @@ namespace Elsa
}
/// <summary>
/// Add Elsa Hangfire Services for background processing and Hangfire Services
/// Add Elsa Hangfire Services for background processing and Hangfire services.
/// </summary>
/// <remarks>
/// Only if Hangfire is not already registered in DI
/// Use only if Hangfire is not already registered in DI.
/// </remarks>
/// <param name="timersOptions"></param>
/// <param name="configure">Hangfire settings</param>
/// <param name="timersOptions">The TimersOptions being configured</param>
/// <param name="configure">Configure Hangfire settings</param>
public static void UseHangfire(this TimersOptions timersOptions, Action<IGlobalConfiguration> configure)
{
timersOptions.UseHangfire();

View file

@ -11,39 +11,51 @@ namespace Elsa
{
public static class TimersOptionsExtensions
{
public static void UseQuartzProvider(this TimersOptions timersOptions, Action<QuartzOptions>? configureOptions = default, Action<IServiceCollectionQuartzConfigurator>? configureQuartz = default)
/// <summary>
/// Add Elsa Quartz services.
/// </summary>
public static void UseQuartzProvider(this TimersOptions timersOptions)
{
if (configureOptions != null)
timersOptions.Services.Configure(configureOptions);
else
timersOptions.Services.AddOptions<QuartzOptions>();
timersOptions.Services.AddQuartz(configure => ConfigureQuartz(configure, configureQuartz))
.AddQuartzHostedService(ConfigureQuartzHostedService)
timersOptions.Services
.AddSingleton<IWorkflowScheduler, QuartzWorkflowScheduler>()
.AddSingleton<ICrontabParser, QuartzCrontabParser>()
.AddTransient<RunQuartzWorkflowJob>();
}
private static void ConfigureQuartzHostedService(QuartzHostedServiceOptions options)
/// <summary>
/// Add Elsa Hangfire Services and Quartz services.
/// </summary>
/// <remarks>
/// Use only if Quartz is not already registered in DI.
/// </remarks>
public static void UseQuartzProvider(
this TimersOptions timersOptions,
Action<QuartzOptions> configureOptions,
Action<IServiceCollectionQuartzConfigurator>? configureQuartz = default,
Action<QuartzHostedServiceOptions>? configureQuartzHostedService = default)
{
timersOptions.UseQuartzProvider();
timersOptions.Services.Configure(configureOptions);
timersOptions.Services
.AddQuartz(configure => ConfigureQuartz(configure, configureQuartz))
.AddQuartzHostedService(options => ConfigureQuartzHostedService(options, configureQuartzHostedService));
}
private static void ConfigureQuartzHostedService(QuartzHostedServiceOptions options, Action<QuartzHostedServiceOptions>? configureQuartzHostedService)
{
options.WaitForJobsToComplete = true;
configureQuartzHostedService?.Invoke(options);
}
private static void ConfigureQuartz(IServiceCollectionQuartzConfigurator quartz, Action<IServiceCollectionQuartzConfigurator>? configureQuartz)
{
quartz.UseMicrosoftDependencyInjectionScopedJobFactory(options => options.AllowDefaultConstructor = true);
quartz.AddJob<RunQuartzWorkflowJob>(job => job.StoreDurably().WithIdentity(nameof(RunQuartzWorkflowJob)));
if (configureQuartz != null)
{
configureQuartz(quartz);
}
else
{
quartz.UseSimpleTypeLoader();
quartz.UseInMemoryStore();
}
quartz.UseSimpleTypeLoader();
quartz.UseInMemoryStore();
configureQuartz?.Invoke(quartz);
}
}
}