using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Threading; using System.Threading.Tasks; using Quartz; using Quartz.Impl; using Microsoft.Extensions.Configuration; namespace Common.Library.JobScheduler { public class Scheduler { protected StdSchedulerFactory SchedulerFactory { get; set; } protected CancellationToken CancellationToken { get; set; } protected IScheduler JobScheduler { get; set; } public Scheduler() { var properties = new NameValueCollection { { "quartz.threadPool.threadCount", "1" } }; SchedulerFactory = new StdSchedulerFactory(properties); } public struct JobDetails { public Type JobType { get; set; } public string JobName { get; set; } public SchedulerEnums.TaskRunInterval JobRunInterval { get; set;} public int JobTimeInterval { get; set; } public string CronExpression { get; set; } public IConfiguration Config { get; set; } } protected async Task StartJobScheduler() { JobScheduler = await SchedulerFactory.GetScheduler(); await JobScheduler.Start(); } protected async Task AddJob(JobDetails jobDetails) { IDictionary dataMap = new Dictionary() { { "Config", jobDetails.Config } }; IJobDetail jobInfo = JobBuilder.Create(jobDetails.JobType) .WithIdentity(jobDetails.JobName, "Jobs") .UsingJobData(new JobDataMap(dataMap)) .Build(); ITrigger triggerInfo = null; switch (jobDetails.JobRunInterval) { case SchedulerEnums.TaskRunInterval.Seconds: triggerInfo = TriggerBuilder.Create() .WithIdentity(GetTriggerName(jobDetails.JobName, jobDetails.JobTimeInterval, jobDetails.JobRunInterval), "Jobs") .StartNow() .WithSimpleSchedule(val => val.WithIntervalInSeconds(jobDetails.JobTimeInterval).RepeatForever()) .Build(); break; case SchedulerEnums.TaskRunInterval.Minutes: triggerInfo = TriggerBuilder.Create() .WithIdentity(GetTriggerName(jobDetails.JobName, jobDetails.JobTimeInterval, jobDetails.JobRunInterval), "Jobs") .StartNow() .WithSimpleSchedule(val => val.WithIntervalInMinutes(jobDetails.JobTimeInterval).RepeatForever()) .Build(); break; case SchedulerEnums.TaskRunInterval.Hours: triggerInfo = TriggerBuilder.Create() .WithIdentity(GetTriggerName(jobDetails.JobName, jobDetails.JobTimeInterval, jobDetails.JobRunInterval), "Jobs") .StartNow() .WithSimpleSchedule(val => val.WithIntervalInHours(jobDetails.JobTimeInterval).RepeatForever()) .Build(); break; case SchedulerEnums.TaskRunInterval.Cron: triggerInfo = TriggerBuilder.Create() .WithIdentity($"trigger_{jobDetails.JobName}_with_cron_schedule", "Jobs") .StartNow() .WithCronSchedule(jobDetails.CronExpression) .Build(); break; } await JobScheduler.ScheduleJob(jobInfo, triggerInfo, CancellationToken); } protected async Task AddOneRunJob(JobDetails jobDetails) { IDictionary dataMap = new Dictionary() { { "Config", jobDetails.Config } }; IJobDetail jobInfo = JobBuilder.Create(jobDetails.JobType) .WithIdentity(jobDetails.JobName, "Jobs") .UsingJobData(new JobDataMap(dataMap)) .Build(); ITrigger triggerInfo = null; switch (jobDetails.JobRunInterval) { case SchedulerEnums.TaskRunInterval.Seconds: triggerInfo = TriggerBuilder.Create() .WithIdentity(GetTriggerName(jobDetails.JobName, jobDetails.JobTimeInterval, jobDetails.JobRunInterval), "Jobs") .StartNow() .WithSimpleSchedule(val => val.WithIntervalInSeconds(jobDetails.JobTimeInterval)) .Build(); break; case SchedulerEnums.TaskRunInterval.Minutes: triggerInfo = TriggerBuilder.Create() .WithIdentity(GetTriggerName(jobDetails.JobName, jobDetails.JobTimeInterval, jobDetails.JobRunInterval), "Jobs") .StartNow() .WithSimpleSchedule(val => val.WithIntervalInMinutes(jobDetails.JobTimeInterval)) .Build(); break; case SchedulerEnums.TaskRunInterval.Hours: triggerInfo = TriggerBuilder.Create() .WithIdentity(GetTriggerName(jobDetails.JobName, jobDetails.JobTimeInterval, jobDetails.JobRunInterval), "Jobs") .StartNow() .WithSimpleSchedule(val => val.WithIntervalInHours(jobDetails.JobTimeInterval)) .Build(); break; } await JobScheduler.ScheduleJob(jobInfo, triggerInfo, CancellationToken); } private string GetTriggerName(string jobName, int jobTimeInterval, SchedulerEnums.TaskRunInterval jobRunInterval) => $"trigger_{jobName}_every_{jobTimeInterval}_{Enum.GetName(typeof(SchedulerEnums.TaskRunInterval), jobRunInterval).ToLower()}"; } }