added scheduler system

net6.0
Don Oerkfitz 4 years ago
parent b1f73d73ef
commit 2abd63e42e

@ -13,6 +13,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
<PackageReference Include="MySql.Data" Version="8.0.22" />
<PackageReference Include="Quartz" Version="3.2.4" />
<PackageReference Include="Quartz.Jobs" Version="3.2.4" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.2" />
<PackageReference Include="System.Text.Json" Version="5.0.0" />
<PackageReference Include="TMDbLib" Version="1.6.0" />

@ -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<string, IConfiguration>() { { "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<string, IConfiguration>() { { "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()}";
}
}

@ -0,0 +1,13 @@
namespace Common.Library.JobScheduler
{
public class SchedulerEnums
{
public enum TaskRunInterval
{
Seconds,
Minutes,
Hours,
Cron
}
}
}
Loading…
Cancel
Save