/// <summary> /// Schedule job with trigger to underlying service collection. This API maybe change! /// </summary> public static IServiceCollectionQuartzConfigurator ScheduleJob <T>( this IServiceCollectionQuartzConfigurator configurator, Action <IServiceCollectionTriggerConfigurator> trigger, Action <IServiceCollectionJobConfigurator>?job = null) where T : IJob { if (trigger is null) { throw new ArgumentNullException(nameof(trigger)); } var jobConfigurator = new ServiceCollectionJobConfigurator(configurator.Services); var jobDetail = ConfigureAndBuildJobDetail <T>(jobConfigurator, job); configurator.Services.AddTransient(x => jobDetail); configurator.Services.AddTransient(jobDetail.JobType); var triggerConfigurator = new ServiceCollectionTriggerConfigurator(configurator.Services); 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"); } configurator.Services.AddTransient(x => t); return(configurator); }
private static void AddJobAndTrigger <T>( this IServiceCollectionQuartzConfigurator quartz, IConfiguration config) where T : IJob { const string QuartzConfig = "Quartz"; const string GroupName = "Template_NET_5_Worker"; var jobName = typeof(T).Name; var quartzJobConfiguration = config.GetSection(QuartzConfig)?.GetSection(jobName)?.Get <QuartzJobConfiguration>(); if (string.IsNullOrEmpty(quartzJobConfiguration?.CronConfig)) { Serilog.Log.Warning("CronConfig for {JobName} not configured, Job will not be configured!", jobName); return; } if (!CronExpression.IsValidExpression(quartzJobConfiguration?.CronConfig)) { Serilog.Log.Warning("CronConfig for {JobName} is invalid, Job will not be configured!", jobName); return; } var jobKey = new JobKey(jobName, GroupName); quartz.AddJob <T>(configurator => configurator.WithIdentity(jobKey)); quartz.AddTrigger( configurator => configurator.ForJob(jobKey) .WithCronSchedule(quartzJobConfiguration?.CronConfig) .WithIdentity($"{jobName}Trigger")); }
/// <summary> /// Add job to underlying service collection.jobType shoud be implement `IJob` /// </summary> public static IServiceCollectionQuartzConfigurator AddJob( this IServiceCollectionQuartzConfigurator options, Type jobType, JobKey?jobKey = null, Action <IJobConfigurator>?configure = null) { if (!typeof(IJob).IsAssignableFrom(jobType)) { ExceptionHelper.ThrowArgumentException("jobType must implement the IJob interface", nameof(jobType)); } var c = new JobConfigurator(); if (jobKey != null) { c.WithIdentity(jobKey); } var jobDetail = ConfigureAndBuildJobDetail(jobType, c, configure, hasCustomKey: out _); options.Services.Configure <QuartzOptions>(x => { x.jobDetails.Add(jobDetail); }); return(options); }
public static void AddJobAndTrigger <T>( this IServiceCollectionQuartzConfigurator quartz, IConfiguration config) where T : IJob { // Use the name of the IJob as the appsettings.json key var jobName = typeof(T).Name; // Try and load the schedule from configuration var configKey = $"Quartz:{jobName}"; var cronSchedule = config[configKey]; // Some minor validation if (string.IsNullOrEmpty(cronSchedule)) { return; } // register the job as before var jobKey = new JobKey(jobName); quartz.AddJob <T>(opts => opts.WithIdentity(jobKey)); quartz.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity(jobName + "-trigger") .WithCronSchedule(cronSchedule)); // use the schedule from configuration }
private static void AddJobAndTrigger <T>( this IServiceCollectionQuartzConfigurator quartz, string configKey, string cronSchedule) where T : IJob { // Use the name of the IJob as the appsettings.json key string jobName = typeof(T).Name; // Try and load the schedule from configuration // Some minor validation if (string.IsNullOrEmpty(cronSchedule)) { throw new Exception($"No Quartz.NET Cron schedule found for job in configuration at {configKey}"); } // register the job as before var jobKey = new JobKey(jobName); quartz.AddJob <T>(opts => opts.WithIdentity(jobKey)); quartz.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity(jobName + "-trigger") .WithCronSchedule(cronSchedule)); // use the schedule from configuration }
public static void AddJobAndTrigger <T>( this IServiceCollectionQuartzConfigurator quartz, IConfiguration config) where T : IJob { string jobName = typeof(T).Name; var configKey = $"Quartz:{jobName}"; var cronSchedule = config[configKey]; if (string.IsNullOrEmpty(cronSchedule)) { throw new Exception($"No Quartz.NET Cron schedule found for job in configuration at {configKey}"); } var jobKey = new JobKey(jobName); quartz.AddJob <T>(opts => opts.WithIdentity(jobKey)); quartz.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity(jobName + "-trigger") .WithCronSchedule(cronSchedule)); }
public static void InitJobAndTriggerFromJobsettings(this IServiceCollectionQuartzConfigurator quartz, IConfiguration configuration) { var allJobs = configuration.GetSection("Jobs").Get <List <BaseJobConfig> >(); Log.Logger.Information($"开始注册 Job"); Log.Logger.Information($"共获取到 {allJobs.Count} 个 Job"); foreach (var item in allJobs) { Log.Logger.Information($"{JsonConvert.SerializeObject(item)}"); var jobName = $"{item.JobType}_{item.Name}"; var jobKey = new JobKey(jobName); Log.Logger.Information($"{nameof(jobKey)}_{jobKey}"); var jobData = new JobDataMap(); jobData.PutAll(ToIDictionary(item)); if (item.JobType.ToLower().Contains("testjob")) { quartz.AddJob <Jobs.TestJob>(opts => { opts.WithIdentity(jobKey); opts.SetJobData(jobData); }); } if (item.JobType.ToLower().Contains("windowscmdjob")) { quartz.AddJob <Jobs.WindowsCMDJob>(opts => { opts.WithIdentity(jobKey); opts.SetJobData(jobData); }); } quartz.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity($"{jobName}_Trigger") .WithCronSchedule(item.Cron)); } Log.Logger.Information($"结束注册 Job"); }
/// <summary> /// Add job to underlying service collection. This API maybe change! /// </summary> public static IServiceCollectionQuartzConfigurator AddJob <T>( this IServiceCollectionQuartzConfigurator options, JobKey?jobKey = null, Action <IJobConfigurator>?configure = null) where T : IJob { return(AddJob(options, typeof(T), jobKey, configure)); }
public void Configure(IServiceCollectionQuartzConfigurator configurator) { configurator.ScheduleJob <SomeTask>( trigger => trigger .StartNow() .WithSimpleSchedule(x => x.WithRepeatCount(0)) ); }
public void Configure(IServiceCollectionQuartzConfigurator config) { config.ScheduleJob <DispatchSubscriptionOrdersTask>(x => x .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(11, 0))); // config.ScheduleJob<OrderPurge>(x => x // .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(11, 0))); }
private static void ConfigureQuartz(IServiceCollectionQuartzConfigurator quartz, Action <IServiceCollectionQuartzConfigurator>?configureQuartz) { quartz.UseMicrosoftDependencyInjectionScopedJobFactory(); quartz.AddJob <RunQuartzWorkflowDefinitionJob>(job => job.StoreDurably().WithIdentity(nameof(RunQuartzWorkflowDefinitionJob))); quartz.AddJob <RunQuartzWorkflowInstanceJob>(job => job.StoreDurably().WithIdentity(nameof(RunQuartzWorkflowInstanceJob))); quartz.UseSimpleTypeLoader(); quartz.UseInMemoryStore(); configureQuartz?.Invoke(quartz); }
/// <summary> /// Adds LibLog configuration to use Microsoft's logging abstraction instead of trying to find one. /// </summary> public static void UseQuartzMicrosoftLoggingBridge(this IServiceCollectionQuartzConfigurator configurator) { configurator.Services.TryAddSingleton(serviceProvider => { var loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>(); var loggingProvider = new LoggingProvider(loggerFactory); LogProvider.SetCurrentLogProvider(loggingProvider); return(loggingProvider); }); }
public static IServiceCollectionQuartzConfigurator AddCalendar( this IServiceCollectionQuartzConfigurator configurator, string name, ICalendar calendar, bool replace, bool updateTriggers) { configurator.Services.AddSingleton(new CalendarConfiguration(name, calendar, replace, updateTriggers)); return(configurator); }
public static void AddTask <TJob>(IServiceCollectionQuartzConfigurator configurator, IConfigurationSection rootSection) where TJob : class, IJob { var jobProperties = QuartzService.GetJobProperties <TJob>(rootSection); if (jobProperties != null) { configurator.RegisterJob <TJob>(jobProperties); } }
public static void AddJobAndTrigger <T>(this IServiceCollectionQuartzConfigurator quartz, IConfiguration config) where T : IJob { string jobName = typeof(T).Name; var jobKey = new JobKey(jobName); quartz.AddJob <T>(opts => opts.WithIdentity(jobKey)); quartz.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity(jobName + "-trigger") .WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever())); }
/// <summary> /// Adds LibLog configuration to use Microsoft's logging abstraction instead of trying to find one. /// </summary> public static IServiceCollectionQuartzConfigurator AddTrigger( this IServiceCollectionQuartzConfigurator configurator, Action <IServiceCollectionTriggerConfigurator>?configure = null) { var c = new ServiceCollectionTriggerConfigurator(configurator.Services); configure?.Invoke(c); var trigger = c.Build(); configurator.Services.AddTransient(x => trigger); return(configurator); }
public static IServiceCollectionQuartzConfigurator AddCalendar <T>( this IServiceCollectionQuartzConfigurator configurator, string name, bool replace, bool updateTriggers, Action <T> configure) where T : ICalendar, new() { var calendar = new T(); configure(calendar); configurator.Services.AddSingleton(new CalendarConfiguration(name, calendar, replace, updateTriggers)); return(configurator); }
public static void RegisterAllTasks(IServiceCollectionQuartzConfigurator configurator, IConfigurationSection rootSection) { AddTask <EnableUsersJob>(configurator, rootSection); AddTask <DisableUsersJob>(configurator, rootSection); AddTask <UsersSynchronizationJob>(configurator, rootSection); AddTask <InterfaceNotificationJob>(configurator, rootSection); AddTask <SystemNotificationJob>(configurator, rootSection); AddTask <InterfaceCleanupJob>(configurator, rootSection); AddTask <SystemCleanupJob>(configurator, rootSection); AddTask <T.CdcDataImportJob>(configurator, rootSection); AddTask <T.CheckNotificationQueueJob>(configurator, rootSection); AddTask <T.CdcDataImportJob>(configurator, rootSection); AddTask <T.CheckNotificationQueueJob>(configurator, rootSection); }
static void RegisterTaskKickJob(IServiceCollectionQuartzConfigurator configurator, JobOptions jobOptions) { var jobKey = new JobKey(jobOptions.Id); configurator .AddJob <KickTaskJob>(c => c .WithIdentity(jobKey) .UsingJobData(jobOptions.ToJobDataMap()) ) .AddTrigger(c => c .ForJob(jobKey) .WithIdentity(jobKey + "-trigger") .WithCronSchedule(jobOptions.Cron) ); }
public static IServiceCollectionQuartzConfigurator ConfigureJob(this IServiceCollectionQuartzConfigurator configurator, IConfiguration appConfig) { var interval = appConfig.GetValue <int>("Quartz:repeatInterval"); var jobKey = new JobKey("Main Job"); configurator.AddJob <MainJob>(opts => opts.WithIdentity(jobKey)); configurator.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity("Main Trigger") .WithSimpleSchedule(s => s.WithIntervalInMinutes(interval).RepeatForever().Build()) ); return(configurator); }
/// <summary> /// Adds LibLog configuration to use Microsoft's logging abstraction instead of trying to find one. /// </summary> public static IServiceCollectionQuartzConfigurator AddJob <T>( this IServiceCollectionQuartzConfigurator configurator, Action <IServiceCollectionJobConfigurator>?configure = null) where T : IJob { var c = new ServiceCollectionJobConfigurator(configurator.Services); c.OfType <T>(); configure?.Invoke(c); var jobDetail = c.Build(); configurator.Services.AddTransient(x => jobDetail); configurator.Services.AddTransient(jobDetail.JobType); return(configurator); }
/// <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); }
public static IServiceCollectionQuartzConfigurator AddJobAndTrigger <T>( this IServiceCollectionQuartzConfigurator quartz, string cronExpression) where T : IJob { var jobName = typeof(T).Name; var jobKey = new JobKey(jobName); quartz.AddJob <T>(configurator => configurator.WithIdentity(jobKey)); quartz.AddTrigger(configurator => configurator .ForJob(jobKey) .WithIdentity($"{jobName}Trigger") .WithCronSchedule(cronExpression) ); return(quartz); }
/// <summary> /// Add trigger to underlying service collection. This API maybe change! /// </summary> public static IServiceCollectionQuartzConfigurator AddTrigger( this IServiceCollectionQuartzConfigurator configurator, Action <IServiceCollectionTriggerConfigurator>?configure = null) { var c = new ServiceCollectionTriggerConfigurator(configurator.Services); configure?.Invoke(c); var trigger = c.Build(); if (trigger.JobKey is null) { throw new InvalidOperationException("Trigger hasn't been associated with a job"); } configurator.Services.AddTransient(x => trigger); return(configurator); }
/// <summary> /// Add job to underlying service collection. This API maybe change! /// </summary> public static IServiceCollectionQuartzConfigurator AddJob <T>( this IServiceCollectionQuartzConfigurator configurator, JobKey?jobKey = null, Action <IServiceCollectionJobConfigurator>?configure = null) where T : IJob { var c = new ServiceCollectionJobConfigurator(configurator.Services); if (jobKey != null) { c.WithIdentity(jobKey); } var jobDetail = ConfigureAndBuildJobDetail <T>(c, configure); configurator.Services.AddTransient(x => jobDetail); configurator.Services.AddTransient(jobDetail.JobType); return(configurator); }
/// <summary> /// Add trigger to underlying service collection. This API maybe change! /// </summary> public static IServiceCollectionQuartzConfigurator AddTrigger( this IServiceCollectionQuartzConfigurator options, Action <ITriggerConfigurator>?configure = null) { var c = new TriggerConfigurator(); configure?.Invoke(c); var trigger = c.Build(); if (trigger.JobKey is null) { throw new InvalidOperationException("Trigger hasn't been associated with a job"); } options.Services.Configure <QuartzOptions>(x => { x.triggers.Add(trigger); }); return(options); }
private static void RegistTasks(this IServiceCollectionQuartzConfigurator Quartz, IServiceCollection services) { var provider = services.BuildServiceProvider(); var tasks = provider.GetRequiredService <IOptions <List <JobWorkConfig> > >().Value; tasks.ForEach(task => { if (task.IsOpen) { var assly = AppDomain.CurrentDomain.GetAssemblies(); var type = assly.SelectMany(a => a.GetTypes() .Where(t => t.GetInterfaces().Contains(typeof(IJob)) && t.Name == task.TaskName)) .FirstOrDefault(); var jobkey = new JobKey(task.TaskName, task.TaskDescription); Action <IJobConfigurator> action = (job) => { job.StoreDurably() .WithIdentity(jobkey) .WithDescription(task.TaskDescription); }; var method = typeof(Quartz.ServiceCollectionExtensions).GetMethods() .Where(a => a.Name == "AddJob").FirstOrDefault(); var generic = method.MakeGenericMethod(type); generic.Invoke(null, new object[] { Quartz, action }); Quartz.AddTrigger(t => t .WithIdentity(task.TaskName) .ForJob(jobkey) .StartNow() //.WithSimpleSchedule(x=>x.WithInterval(TimeSpan.FromSeconds(60)).WithRepeatCount(0)) .WithCronSchedule(task.TriggerTime) .WithDescription(task.TaskDescription) ); } }); }
/// <summary> /// Add job to underlying service collection. This API maybe change! /// </summary> public static IServiceCollectionQuartzConfigurator AddJob <T>( this IServiceCollectionQuartzConfigurator options, JobKey?jobKey = null, Action <IJobConfigurator>?configure = null) where T : IJob { var c = new JobConfigurator(); if (jobKey != null) { c.WithIdentity(jobKey); } var jobDetail = ConfigureAndBuildJobDetail <T>(c, configure); options.Services.Configure <QuartzOptions>(x => { x.jobDetails.Add(jobDetail); }); options.Services.TryAddTransient(jobDetail.JobType); 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); }
public static IServiceCollectionQuartzConfigurator RegisterJob <TJob>(this IServiceCollectionQuartzConfigurator configurator, CommonSchedulerTaskProperties taskSettings) where TJob : class, IJob { var jobType = typeof(TJob); configurator.ScheduleJob <TJob>( trigger => trigger.WithIdentity(QuartzService.GetJobTriggerIdentity(jobType)) .WithDescription(QuartzService.GetJobTriggerDescription(jobType, taskSettings.Schedule)) .WithCronSchedule(taskSettings.Schedule), j => { j .WithIdentity(jobType.Name) .WithDescription(QuartzService.GetJobDescription(jobType, taskSettings.Description)); if (taskSettings.SpecifiedConditions != null) { j.UsingJobData(new JobDataMap(taskSettings.SpecifiedConditions)); } }); return(configurator); }