using Elsa.Features.Abstractions; using Elsa.Features.Services; using Microsoft.Extensions.DependencyInjection; using Quartz; namespace Elsa.Quartz.Features; /// /// A feature that installs and configures Quartz.NET. Only enable this feature if you are not configuring Quartz.NET yourself. /// public class QuartzFeature : FeatureBase { /// public QuartzFeature(IModule module) : base(module) { } /// /// A delegate that can be used to configure Quartz.NET options. /// public Action? ConfigureQuartzOptions { get; set; } /// /// A delegate that can be used to configure Quartz.NET itself. /// public Action? ConfigureQuartz { get; set; } /// /// A delegate that can be used to configure Quartz.NET hosted service. /// public Action? ConfigureQuartzHostedService { get; set; } = options => options.WaitForJobsToComplete = true; /// public override void ConfigureHostedServices() { var type = Type.GetType("Quartz.QuartzHostedService, Quartz.Extensions.Hosting")!; Module.ConfigureHostedService(type); if (ConfigureQuartzHostedService != null) Services.Configure(ConfigureQuartzHostedService); } /// public override void Apply() { if (ConfigureQuartzOptions != null) Services.Configure(ConfigureQuartzOptions); Services .AddQuartz(configure => { ConfigureQuartzInternal(configure, ConfigureQuartz); }); } private static void ConfigureQuartzInternal(IServiceCollectionQuartzConfigurator quartz, Action? configureQuartz) { quartz.UseMicrosoftDependencyInjectionJobFactory(); quartz.UseSimpleTypeLoader(); quartz.UseInMemoryStore(); configureQuartz?.Invoke(quartz); } }