2022-03-07 21:56:16 +00:00
using Elsa.Jobs.Contracts ;
2022-01-13 21:38:01 +00:00
using Elsa.Modules.Quartz.Contracts ;
using Elsa.Modules.Quartz.Jobs ;
using Elsa.Modules.Quartz.Services ;
2022-02-01 19:45:11 +00:00
using Elsa.Modules.Scheduling.Jobs ;
2022-01-13 21:38:01 +00:00
using Microsoft.Extensions.DependencyInjection ;
using Quartz ;
2022-03-07 21:56:16 +00:00
using IJob = Elsa . Jobs . Contracts . IJob ;
2022-01-13 21:38:01 +00:00
namespace Elsa.Modules.Quartz.Extensions ;
public static class ServiceCollectionExtensions
{
/// <summary>
2022-03-07 21:56:16 +00:00
/// Registers both Quartz as well as Elsa-specific services and jobs.
/// If you register Quartz yourself, use <see cref="AddQuartzModule"/> instead.
2022-01-13 21:38:01 +00:00
/// </summary>
2022-03-07 21:56:16 +00:00
public static IServiceCollection AddQuartzAndModule (
2022-01-13 21:38:01 +00:00
this IServiceCollection services ,
Action < QuartzOptions > ? configureQuartzOptions = default ,
Action < IServiceCollectionQuartzConfigurator > ? configureQuartz = default ,
Action < QuartzHostedServiceOptions > ? configureQuartzHostedService = default )
{
if ( configureQuartzOptions ! = null )
services . Configure ( configureQuartzOptions ) ;
return services
. AddQuartz ( configure = >
{
ConfigureQuartz ( configure , configureQuartz ) ;
2022-03-07 21:56:16 +00:00
AddQuartzModule ( services , configure ) ;
2022-01-13 21:38:01 +00:00
} )
. AddQuartzHostedService ( options = > ConfigureQuartzHostedService ( options , configureQuartzHostedService ) ) ;
}
/// <summary>
2022-03-07 21:56:16 +00:00
/// This will register Elsa-specific services and jobs, but will **not** register Quartz itself. To register Quartz, you need to do so yourself, or use <see cref="AddQuartzAndModule"/> to register & configure Quartz for Elsa.
2022-01-13 21:38:01 +00:00
/// </summary>
2022-03-07 21:56:16 +00:00
public static IServiceCollection AddQuartzModule ( this IServiceCollection services , IServiceCollectionQuartzConfigurator quartz )
2022-01-13 21:38:01 +00:00
{
services
. AddSingleton < IElsaJobSerializer , ElsaJobSerializer > ( )
. AddSingleton < IJobScheduler , QuartzJobScheduler > ( ) ;
quartz . AddElsaJobs ( ) ;
return services ;
}
private static IServiceCollectionQuartzConfigurator AddElsaJobs ( this IServiceCollectionQuartzConfigurator quartz )
{
quartz . AddJob < RunWorkflowJob > ( ) ;
2022-01-18 19:28:48 +00:00
quartz . AddJob < ResumeWorkflowJob > ( ) ;
2022-01-13 21:38:01 +00:00
return quartz ;
}
private static void ConfigureQuartzHostedService ( QuartzHostedServiceOptions options , Action < QuartzHostedServiceOptions > ? configureQuartzHostedService )
{
options . WaitForJobsToComplete = true ;
configureQuartzHostedService ? . Invoke ( options ) ;
}
2022-03-07 21:56:16 +00:00
private static IServiceCollectionQuartzConfigurator AddJob < TJob > ( this IServiceCollectionQuartzConfigurator quartz ) where TJob : IJob = >
2022-01-18 19:28:48 +00:00
quartz . AddJob < QuartzJob < TJob > > ( job = > job . StoreDurably ( ) . WithIdentity ( typeof ( TJob ) . Name ) ) ;
2022-01-13 21:38:01 +00:00
private static void ConfigureQuartz ( IServiceCollectionQuartzConfigurator quartz , Action < IServiceCollectionQuartzConfigurator > ? configureQuartz )
{
quartz . UseMicrosoftDependencyInjectionJobFactory ( ) ;
quartz . UseSimpleTypeLoader ( ) ;
quartz . UseInMemoryStore ( ) ;
configureQuartz ? . Invoke ( quartz ) ;
}
}