using Quartz;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.Server
|
{
|
/// <summary>
|
/// Socket数据对接服务任务辅助类
|
/// </summary>
|
internal class ServiceJobHelper
|
{
|
private const string _jobName = "SocketServiceJob";
|
private const string _jobGroup = "SocketServiceJobGroup";
|
private const string _triggerName = "SocketServiceJobTrigger";
|
private static IScheduler _sched;//调度器
|
|
public async Task StartJob()
|
{
|
if (_sched != null)
|
return;
|
|
// 1.创建scheduler的引用
|
var fac = new Quartz.Impl.StdSchedulerFactory();
|
_sched = await fac.GetScheduler();
|
|
//2.启动 scheduler
|
await _sched.Start();
|
|
//3.创建任务
|
var job = JobBuilder.Create<ServiceJob>()
|
.WithIdentity(_jobName, _jobGroup)
|
.Build();
|
|
//4.创建Trigger
|
var trigger = TriggerBuilder.Create()
|
.WithIdentity(_triggerName, _jobGroup)
|
.WithSimpleSchedule(x => x.WithIntervalInMinutes(ConfigHelper.ResetFrequency)
|
.RepeatForever()
|
.WithMisfireHandlingInstructionNextWithRemainingCount())
|
.Build();
|
|
//5.加入调度管理器
|
await _sched.ScheduleJob(job, trigger);
|
}
|
|
/// <summary>
|
/// 取消任务
|
/// </summary>
|
public async Task CancelJob()
|
{
|
if (_sched == null)
|
return;
|
var triggerKey = new TriggerKey(_triggerName, _jobGroup);
|
if (await _sched.CheckExists(triggerKey))
|
{
|
await _sched.UnscheduleJob(triggerKey);
|
}
|
ServiceJob.CancelServers();
|
}
|
|
}
|
}
|