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")); }
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 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"); }
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 }
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 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())); }
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); }
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); }
private static async Task <IServiceCollectionQuartzConfigurator> AddMixJobsAsync(this IServiceCollectionQuartzConfigurator quartzConfiguration) { List <MixJobModel> jobConfiguraions = MixQuartzHelper.LoadJobConfiguraions(); var assembly = Assembly.GetExecutingAssembly(); var mixJobs = assembly .GetExportedTypes() .Where(m => m.BaseType.Name == typeof(BaseJob).Name); StdSchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = await factory.GetScheduler(); foreach (var job in mixJobs) { var jobConfig = jobConfiguraions.FirstOrDefault(j => j.JobType == job); if (jobConfig == null) { jobConfig = GetDefaultJob(job); } var jobKey = new JobKey(jobConfig.Key, jobConfig.Group); Action <IJobConfigurator> jobConfigurator = j => j.WithDescription(jobConfig.Description); var applyGenericMethod = typeof(Quartz.ServiceCollectionExtensions) .GetMethods(BindingFlags.Static | BindingFlags.Public) .FirstOrDefault(m => m.Name == nameof(Quartz.ServiceCollectionExtensions.AddJob) && m.GetParameters()[1].ParameterType == typeof(JobKey)); var parameters = applyGenericMethod.GetParameters(); var applyConcreteMethod = applyGenericMethod.MakeGenericMethod(jobConfig.JobType); applyConcreteMethod.Invoke(quartzConfiguration, new object[] { quartzConfiguration, jobKey, jobConfigurator }); quartzConfiguration.AddTrigger(t => t .WithIdentity("trigger_" + jobConfig.Key, jobConfig.Group) .ForJob(jobKey) .StartNowIf(jobConfig.Trigger.IsStartNow) .StartAtIf(jobConfig.Trigger.StartAt.HasValue, jobConfig.Trigger.StartAt.Value) .WithMixSchedule(jobConfig.Trigger.Interval, jobConfig.Trigger.IntervalType, jobConfig.Trigger.RepeatCount) .WithDescription(jobConfig.Description)); } return(quartzConfiguration); }
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) ); } }); }