57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Threading.Tasks;
|
|
using Quartz;
|
|
using Quartz.Impl;
|
|
|
|
namespace JXCMS.Core.TimingTask
|
|
{
|
|
public class QuartzTask
|
|
{
|
|
private ISchedulerFactory _schedulerFactory;
|
|
private IScheduler _scheduler;
|
|
|
|
private QuartzTask()
|
|
{
|
|
_schedulerFactory = new StdSchedulerFactory();
|
|
_scheduler = _schedulerFactory.GetScheduler().Result;
|
|
_scheduler.Start();
|
|
}
|
|
|
|
private static QuartzTask _instance;
|
|
|
|
public static QuartzTask Instance()
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new QuartzTask();
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
|
|
public async Task<bool> AddTask(Type jobType, string cron, string jobName, string jobGroup = "default", params (string name, object value)[] param)
|
|
{
|
|
var time = DateTime.Now;
|
|
var job = JobBuilder.Create(jobType).WithIdentity(jobName, jobGroup).Build();
|
|
var trigger = TriggerBuilder.Create().WithIdentity(jobName + "trigger").StartNow().WithCronSchedule(cron).Build();
|
|
foreach (var par in param)
|
|
{
|
|
job.JobDataMap.Put(par.name, par.value);
|
|
}
|
|
var offset = await _scheduler.ScheduleJob(job, trigger);
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> DeleteTask(string jobName, string jobGroup = "default")
|
|
{
|
|
return await _scheduler.DeleteJob(new JobKey(jobName, jobGroup));
|
|
}
|
|
|
|
public async Task<bool> DeleteTasks(ReadOnlyCollection<JobKey> jobKeys)
|
|
{
|
|
return await _scheduler.DeleteJobs(jobKeys);
|
|
}
|
|
|
|
}
|
|
} |