From 2abd63e42ea37bff3d17d918f0c295d2935254aa Mon Sep 17 00:00:00 2001 From: Don Oerkfitz Date: Wed, 17 Mar 2021 13:57:43 -0500 Subject: [PATCH] added scheduler system --- Common.Library/Common.Library.csproj | 2 + Common.Library/JobScheduler/Scheduler.cs | 132 ++++++++++++++++++ Common.Library/JobScheduler/SchedulerEnums.cs | 13 ++ 3 files changed, 147 insertions(+) create mode 100644 Common.Library/JobScheduler/Scheduler.cs create mode 100644 Common.Library/JobScheduler/SchedulerEnums.cs diff --git a/Common.Library/Common.Library.csproj b/Common.Library/Common.Library.csproj index a865e65..93685a0 100644 --- a/Common.Library/Common.Library.csproj +++ b/Common.Library/Common.Library.csproj @@ -13,6 +13,8 @@ + + diff --git a/Common.Library/JobScheduler/Scheduler.cs b/Common.Library/JobScheduler/Scheduler.cs new file mode 100644 index 0000000..f63dfd1 --- /dev/null +++ b/Common.Library/JobScheduler/Scheduler.cs @@ -0,0 +1,132 @@ +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()}"; + } +} \ No newline at end of file diff --git a/Common.Library/JobScheduler/SchedulerEnums.cs b/Common.Library/JobScheduler/SchedulerEnums.cs new file mode 100644 index 0000000..f3392fb --- /dev/null +++ b/Common.Library/JobScheduler/SchedulerEnums.cs @@ -0,0 +1,13 @@ +namespace Common.Library.JobScheduler +{ + public class SchedulerEnums + { + public enum TaskRunInterval + { + Seconds, + Minutes, + Hours, + Cron + } + } +} \ No newline at end of file