Location>code7788 >text

HTTP job scheduling implemented

Popularity:440 ℃/2024-07-20 23:30:40
public class QuartzHelper { private IScheduler scheduler; private List<JobInfo> jobInfos; private string filePath = (, ""); /// <summary> /// Constructor to initialize the timed task manager /// </summary> public QuartzHelper() { ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); scheduler = ().Result; ().Wait(); LoadJobInfosApi().Wait(); } /// <summary> /// Save job information to a local JSON file /// </summary> private void SaveJobInfos() { string json = (jobInfos); (filePath, json); } /// <summary> /// Load job information in a local JSON file /// </summary> private async Task LoadJobInfosApi() { if ((filePath)) { string json = (filePath); jobInfos = <List<JobInfo>>(json) ?? new List<JobInfo>(); foreach (var jobInfo in jobInfos) { // Creating a unique key for a delegate var delegateKey = ().ToString(); // Storing delegates in a static dictionary [delegateKey] = ; // Creating and scheduling jobs IJobDetail job = <HttpJob>() .WithIdentity(, ).UsingJobData("delegateKey", delegateKey) // Add the delegate's keys to the JobDataMap .Build(); ITrigger trigger = () .WithIdentity(, ) .WithCronSchedule() //.StartNow() .Build(); await (job, trigger); // Resume or suspend tasks based on task status if ( == JobStatus.normal operation) { await ResumeJob(, ); } else { await PauseJob(, ); } } } else { jobInfos = new List<JobInfo>(); } } #region Used when performing common tasks, refer to this method when passing delegates ///// <summary> ///// Create a new task and execute it immediately ///// </summary> //[Obsolete("Used when performing common tasks, can pass delegate to use")] //public async Task AddJob(string jobName, string groupName, string cronExpression, Func<bool> func, string description = "") //{ // if ((c => == jobName && == groupName)) // { // return; // } // // Creating a unique key for a delegate // var delegateKey = ().ToString(); // // Storing delegates in a static dictionary // // [delegateKey] = func; // // Create job info and save to list Need to add func to jobInfo for job persistence !!!! // var jobInfo = new JobInfo { JobName = jobName, GroupName = groupName, CronExpression = cronExpression, Status = JobStatus.normal operation, Description = description, JobCreateTime = }; // (jobInfo); // SaveJobInfos(); // // Creating Quartz Jobs and Triggers // IJobDetail job = <MyJobClass>() // .WithIdentity(jobName, groupName) // .UsingJobData("delegateKey", delegateKey) // Add the delegate's keys to the JobDataMap // .Build(); // ITrigger trigger = () // .WithIdentity(jobName + "Trigger", groupName) // .StartNow() // .WithCronSchedule(cronExpression).WithDescription(description) // .Build(); // await (job, trigger); //} #endregion /// <summary> /// Create a new task and execute it immediately /// </summary> public async Task AddJobApi(string jobName, string groupName, string cronExpression, HttpJobInfo httpJobInfo, string description = "") { if ((c => == jobName && == groupName)) { return; } // Creating a unique key for a delegate var delegateKey = ().ToString(); // Storing delegates in a static dictionary [delegateKey] = httpJobInfo; // Create job info and save to list Need to add func to jobInfo for job persistence !!!! var jobInfo = new JobInfo { JobName = jobName, GroupName = groupName, CronExpression = cronExpression, HttpJob = httpJobInfo, Status = JobStatus.normal operation, Description = description, JobCreateTime = }; (jobInfo); SaveJobInfos(); // Creating Quartz Jobs and Triggers IJobDetail job = <HttpJob>() .WithIdentity(jobName, groupName) .UsingJobData("delegateKey", delegateKey) // Add the delegate's keys to the JobDataMap .Build(); ITrigger trigger = () .WithIdentity(jobName + "Trigger", groupName) .StartNow() .WithCronSchedule(cronExpression).WithDescription(description) .Build(); await (job, trigger); } /// <summary> /// Suspension of the mandate /// </summary> public async Task PauseJob(string jobName, string groupName) { await (new JobKey(jobName, groupName)); var job = (j => == jobName && == groupName); if (job != null) { = JobStatus.Pause; SaveJobInfos(); } } /// <summary> /// Open Tasks /// </summary> public async Task ResumeJob(string jobName, string groupName) { await (new JobKey(jobName, groupName)); var job = (j => == jobName && == groupName); if (job != null) { = JobStatus.Normal operation;. SaveJobInfos(); } } /// <summary> /// Immediate implementation of the mandate /// </summary> public async Task TriggerJob(string jobName, string groupName) { await (new JobKey(jobName, groupName)); var job = (j => == jobName && == groupName); if (job != null) { = ; SaveJobInfos(); } } /// <summary> /// Modification of tasks /// </summary> public async Task ModifyJob(string jobName, string groupName, string cronExpression, HttpJobInfo httpJobInfo, string description = "") { await DeleteJob(jobName, groupName); await AddJobApi(jobName, groupName, cronExpression, httpJobInfo, description); } /// <summary> /// Deletion of tasks /// </summary> public async Task DeleteJob(string jobName, string groupName) { await (new JobKey(jobName, groupName)); (j => == jobName && == groupName); SaveJobInfos(); } /// <summary> /// Get a list of all current tasks /// </summary> public List<JobInfo> GetAllJobs() { if ((filePath)) { string json = (filePath); jobInfos = <List<JobInfo>>(json) ?? new List<JobInfo>(); return jobInfos; } else return null; } }