You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.1 KiB
C#

1 year ago
using MESDataExtractQuartz.Job;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MESDataExtractQuartz
{
public class Trigger
{
//调度器
private readonly IScheduler _scheduler;
public Trigger()
{
_scheduler = new StdSchedulerFactory().GetScheduler().Result;
}
public virtual void Run()
{
try
{
// define the job and tie it to our Job class
var firstJob = JobBuilder.Create<ExtractMLJob>()
.WithIdentity("MESTestJob", "MESTestJobGroup")
.Build();
var firstTrigger = TriggerBuilder.Create()
.WithIdentity("Trigger", "DataMonitorGroup")
.StartNow()
.WithCronSchedule("0 0 0/2 * * ?") //每两小时执行一次0 0 0/2 * * ?
.Build();
var secondJob = JobBuilder.Create<SumMLTeamJob>()
.WithIdentity("SumMLTeamJob", "SumMLTeamJobGroup")
.Build();
var secondTrigger = TriggerBuilder.Create()
.WithIdentity("ReportYearTrigger", "DataMonitorGroup")
.StartNow()
.WithCronSchedule("0 35 10 * * ?") //0 0 11 * * ?
.Build();
// Tell quartz to schedule the job using our trigger
_scheduler.ScheduleJob(firstJob, firstTrigger);
_scheduler.ScheduleJob(secondJob, secondTrigger);
// Start up the scheduler (nothing can actually run until the scheduler has been started)
_scheduler.Start();
}
catch (Exception ex)
{
LogHelper.Error("任务调度启动失败:" + ex.Message);
}
}
public virtual void Stop()
{
try
{
if (_scheduler != null)
{
_scheduler.Shutdown(true);
}
LogHelper.Info("任务计划停止成功!");
}
catch (Exception e)
{
LogHelper.Error("停止任务计划调度失败", e);
throw;
}
}
public virtual void Continue()
{
try
{
_scheduler.ResumeAll();
LogHelper.Info("任务计划继续成功!");
}
catch (Exception e)
{
LogHelper.Error("继续任务计划调度失败", e);
throw;
}
}
public virtual void Pause()
{
try
{
_scheduler.PauseAll();
LogHelper.Info("任务计划暂停成功!");
}
catch (Exception e)
{
LogHelper.Error("暂停任务计划调度失败", e);
throw;
}
}
}
}