using System; using Elsa.Activities.Temporal.Common.Options; using Elsa.Activities.Temporal.Common.Services; using Elsa.Activities.Temporal.Quartz.Handlers; using Elsa.Activities.Temporal.Quartz.Jobs; using Elsa.Activities.Temporal.Quartz.Services; using Microsoft.Extensions.DependencyInjection; using Quartz; // ReSharper disable once CheckNamespace namespace Elsa { public static class TimersOptionsExtensions { /// /// Add Elsa Quartz services and Quartz services. /// /// The timer options being configured. /// True to automatically register Quartz services. When false, make sure to register Quartz yourself. /// When is true, you can use this callback to further configure Quartz options. /// When is true, you can use this callback to further configure Quartz. /// When is true, you can use this callback to further configure the Quartz hosted service. public static void UseQuartzProvider( this TimersOptions timersOptions, bool registerQuartz = true, Action? configureQuartzOptions = default, Action? configureQuartz = default, Action? configureQuartzHostedService = default) { timersOptions.Services .AddSingleton() .AddSingleton() .AddTransient() .AddNotificationHandlers(typeof(ConfigureCronProperty)); if (registerQuartz) { if (configureQuartzOptions != null) timersOptions.Services.Configure(configureQuartzOptions); timersOptions.Services .AddQuartz(configure => ConfigureQuartz(configure, configureQuartz)) .AddQuartzHostedService(options => ConfigureQuartzHostedService(options, configureQuartzHostedService)); } } private static void ConfigureQuartzHostedService(QuartzHostedServiceOptions options, Action? configureQuartzHostedService) { options.WaitForJobsToComplete = true; configureQuartzHostedService?.Invoke(options); } private static void ConfigureQuartz(IServiceCollectionQuartzConfigurator quartz, Action? configureQuartz) { quartz.UseMicrosoftDependencyInjectionScopedJobFactory(options => options.AllowDefaultConstructor = true); quartz.AddJob(job => job.StoreDurably().WithIdentity(nameof(RunQuartzWorkflowJob))); quartz.UseSimpleTypeLoader(); quartz.UseInMemoryStore(); configureQuartz?.Invoke(quartz); } } }