/// <summary> /// Schedule job with trigger to underlying service collection. This API maybe change! /// </summary> public static IServiceCollectionQuartzConfigurator ScheduleJob <T>( this IServiceCollectionQuartzConfigurator options, Action <ITriggerConfigurator> trigger, Action <IJobConfigurator>?job = null) where T : IJob { if (trigger is null) { throw new ArgumentNullException(nameof(trigger)); } var jobConfigurator = new JobConfigurator(); var jobDetail = ConfigureAndBuildJobDetail(typeof(T), jobConfigurator, job, out var jobHasCustomKey); options.Services.Configure <QuartzOptions>(quartzOptions => { quartzOptions.jobDetails.Add(jobDetail); }); var triggerConfigurator = new TriggerConfigurator(); triggerConfigurator.ForJob(jobDetail); trigger.Invoke(triggerConfigurator); var t = triggerConfigurator.Build(); // The job configurator is optional and omitted in most examples // If no job key was specified, have the job key match the trigger key if (!jobHasCustomKey) { ((JobDetailImpl)jobDetail).Key = new JobKey(t.Key.Name, t.Key.Group); // Keep ITrigger.JobKey in sync with IJobDetail.Key ((IMutableTrigger)t).JobKey = jobDetail.Key; } if (t.JobKey is null || !t.JobKey.Equals(jobDetail.Key)) { throw new InvalidOperationException("Trigger doesn't refer to job being scheduled"); } options.Services.Configure <QuartzOptions>(quartzOptions => { quartzOptions.triggers.Add(t); }); return(options); }
/// <summary> /// Schedule job with trigger to underlying service collection. This API maybe change! /// </summary> public static IServiceCollectionQuartzConfigurator ScheduleJob <T>( this IServiceCollectionQuartzConfigurator options, Action <ITriggerConfigurator> trigger, Action <IJobConfigurator>?job = null) where T : IJob { if (trigger is null) { throw new ArgumentNullException(nameof(trigger)); } var jobConfigurator = new JobConfigurator(); var jobDetail = ConfigureAndBuildJobDetail <T>(jobConfigurator, job); options.Services.Configure <QuartzOptions>(quartzOptions => { quartzOptions.jobDetails.Add(jobDetail); }); options.Services.TryAddTransient(jobDetail.JobType); var triggerConfigurator = new TriggerConfigurator(); triggerConfigurator.ForJob(jobDetail); trigger.Invoke(triggerConfigurator); var t = triggerConfigurator.Build(); if (t.JobKey is null || !t.JobKey.Equals(jobDetail.Key)) { throw new InvalidOperationException("Trigger doesn't refer to job being scheduled"); } options.Services.Configure <QuartzOptions>(quartzOptions => { quartzOptions.triggers.Add(t); }); return(options); }