using ce.autofac.extension; using learun.iapplication; using learun.util; using Quartz; using Quartz.Impl; using Quartz.Impl.Triggers; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace learun.application { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2019.11.01 /// 描 述:任务调度操作类 /// public class QuartzHelper { private static readonly ISchedulerFactory schedFact = new StdSchedulerFactory();//构建一个调度工厂 #region 新增任务 /// /// 添加只执行一次的任务 /// /// 任务名称 /// 任务开始时间 /// 任务结束时间 /// 任务主键 public static async Task AddRepeatOneJob(string jobName, string starTime, string endTime, string keyValue) { IScheduler sched = await schedFact.GetScheduler(); //得到一个调度程序 IJobDetail job = JobBuilder.Create()//新建任务执行类 .WithIdentity(jobName, "lrts") // name "myJob", group "group1" .UsingJobData("keyValue", keyValue)//传递参数 .Build(); //开始时间处理 DateTime StarTime = DateTime.Now; if (!string.IsNullOrEmpty(starTime)) { StarTime = Convert.ToDateTime(starTime); } DateTimeOffset starRunTime = DateBuilder.NextGivenSecondDate(StarTime, 1); //结束时间处理 DateTime EndTime = DateTime.Now; if (!string.IsNullOrEmpty(endTime)) { EndTime = Convert.ToDateTime(endTime); } DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(EndTime, 1); // 创建一个时间触发器 SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.Name = jobName; trigger.Group = "lrts"; trigger.StartTimeUtc = starRunTime; trigger.EndTimeUtc = endRunTime; trigger.RepeatCount = 0; await sched.ScheduleJob(job, trigger); // 启动 if (!sched.IsStarted) { await sched.Start(); } } /// /// 添加简单重复执行任务 /// /// 任务名称 /// 开始时间 /// 结束时间 /// 任务主键 /// 间隔值 /// 间隔类型 public static async Task AddRepeatJob(string jobName, string starTime, string endTime, string keyValue, int value, string type) { IScheduler sched = await schedFact.GetScheduler(); //得到一个调度程序 IJobDetail job = JobBuilder.Create()//新建任务执行类 .WithIdentity(jobName, "lrts") // name "myJob", group "group1" .UsingJobData("keyValue", keyValue)//传递参数 .Build(); //开始时间处理 DateTime StarTime = DateTime.Now; if (!string.IsNullOrEmpty(starTime)) { StarTime = Convert.ToDateTime(starTime); } DateTimeOffset starRunTime = DateBuilder.NextGivenSecondDate(StarTime, 1); //结束时间处理 DateTime EndTime = DateTime.MaxValue.AddDays(-1); if (!string.IsNullOrEmpty(endTime)) { EndTime = Convert.ToDateTime(endTime); } DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(EndTime, 1); ITrigger trigger = null; switch (type) { case "minute": // 分钟 trigger = TriggerBuilder.Create().WithIdentity(jobName, "lrts") .StartAt(starRunTime) .EndAt(endRunTime) .WithSimpleSchedule(t => t.WithIntervalInMinutes(value) .RepeatForever()) .Build(); break; case "hours": // 小时 trigger = TriggerBuilder.Create() .WithIdentity(jobName, "lrts") .StartAt(starRunTime) .EndAt(endRunTime) .WithSimpleSchedule(t => t.WithIntervalInHours(value) .RepeatForever()) .Build(); break; case "day": // 天 trigger = TriggerBuilder.Create() .WithIdentity(jobName, "lrts") .StartAt(starRunTime) .EndAt(endRunTime) .WithSchedule( CalendarIntervalScheduleBuilder.Create() .WithIntervalInDays(value) ) .Build(); break; case "week":// 周 trigger = TriggerBuilder.Create() .WithIdentity(jobName, "lrts") .StartAt(starRunTime) .EndAt(endRunTime) .WithSchedule( CalendarIntervalScheduleBuilder.Create() .WithIntervalInWeeks(value) ) .Build(); break; } //实例化 await sched.ScheduleJob(job, trigger); //启动 if (!sched.IsStarted) { await sched.Start(); } } /// /// 添加Cron表达式任务(一个表达式) /// /// 任务名称 /// 开始时间 /// 结束时间 /// 主键名称 /// Cron表达式 public static async Task AddCronJob(string jobName, string starTime, string endTime, string keyValue, string corn) { IScheduler sched = await schedFact.GetScheduler(); //得到一个调度程序 IJobDetail job = JobBuilder.Create()//新建任务执行类 .WithIdentity(jobName, "lrts") // name "myJob", group "group1" .UsingJobData("keyValue", keyValue)//传递参数 .Build(); //开始时间处理 DateTime StarTime = DateTime.Now; if (!string.IsNullOrEmpty(starTime)) { StarTime = Convert.ToDateTime(starTime); } DateTimeOffset starRunTime = DateBuilder.NextGivenSecondDate(StarTime, 1); //结束时间处理 DateTime EndTime = DateTime.MaxValue.AddDays(-1); if (!string.IsNullOrEmpty(endTime)) { EndTime = Convert.ToDateTime(endTime); } DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(EndTime, 1); // 创建一个时间触发器 ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create() .StartAt(starRunTime) .EndAt(endRunTime) .WithIdentity(jobName, "lrts") .WithCronSchedule(corn) .Build(); await sched.ScheduleJob(job, trigger); // 启动 if (!sched.IsStarted) { await sched.Start(); } } /// /// 添加多触发器任务 /// /// 任务名称 /// 开始时间 /// 结束时间 /// 任务主键 /// cron数组 public static async Task AddListCronJob(string jobName, string starTime, string endTime, string keyValue, List list) { IScheduler sched = await schedFact.GetScheduler(); //得到一个调度程序 IJobDetail job = JobBuilder.Create()//新建任务执行类 .WithIdentity(jobName, "lrts") // name "myJob", group "group1" .UsingJobData("keyValue", keyValue)//传递参数 .StoreDurably() .Build(); //开始时间处理 DateTime StarTime = DateTime.Now; if (!string.IsNullOrEmpty(starTime)) { StarTime = Convert.ToDateTime(starTime); } DateTimeOffset starRunTime = DateBuilder.NextGivenSecondDate(StarTime, 1); //结束时间处理 DateTime EndTime = DateTime.MaxValue.AddDays(-1); if (!string.IsNullOrEmpty(endTime)) { EndTime = Convert.ToDateTime(endTime); } DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(EndTime, 1); await sched.AddJob(job, true); // 创建一个时间触发器 for (var i = 0; i < list.Count; i++) { ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger" + Guid.NewGuid().ToString()) .StartAt(starRunTime) .EndAt(endRunTime) .ForJob(job) .WithCronSchedule(list[i]) .Build(); await sched.ScheduleJob(trigger); } // 启动 if (!sched.IsStarted) { await sched.Start(); } } #endregion 新增任务 #region 删除任务 /// /// 删除任务 /// /// 任务名称 public static async Task DeleteJob(string jobName) { if (!string.IsNullOrEmpty(jobName)) { IScheduler sched = await schedFact.GetScheduler(); TriggerKey triggerKey = new TriggerKey(jobName, "lrts"); await sched.PauseTrigger(triggerKey);// 停止触发器 await sched.UnscheduleJob(triggerKey);// 移除触发器 await sched.DeleteJob(JobKey.Create(jobName, "lrts"));// 删除任务 } } #endregion 删除任务 #region 任务暂停和启动 /// /// 暂停一个job任务 /// /// 任务名称 public static async Task PauseJob(string jobName) { if (!string.IsNullOrEmpty(jobName)) { IScheduler sched = await schedFact.GetScheduler(); //得到一个调度程序 await sched.PauseJob(JobKey.Create(jobName, "lrts")); } } /// /// 重启启动一个job /// /// 任务名称 public static async Task ResumeJob(string jobName) { if (!string.IsNullOrEmpty(jobName)) { IScheduler sched = await schedFact.GetScheduler(); //得到一个调度程序 await sched.ResumeJob(JobKey.Create(jobName, "lrts")); } } #endregion 任务暂停和启动 #region 扩展应用 /// /// 添加任务 /// /// 模板信息主键 /// 任务模板 public static async Task AddJob(string schemeInfoId, TSSchemeModel scheme) { string startTime = ""; string endTime = ""; if (scheme.startType == 2 && scheme.startTime != null) { startTime = ((DateTime)scheme.startTime).ToString("yyyy-MM-dd hh:mm:ss"); } if (scheme.endType == 2 && scheme.endTime != null) { endTime = ((DateTime)scheme.endTime).ToString("yyyy-MM-dd hh:mm:ss"); } switch (scheme.executeType) { case 1:// 只执行一次 await AddRepeatOneJob(schemeInfoId, startTime, endTime, schemeInfoId);//加入只执行一次的任务 break; case 2:// 简单重复执行 await AddRepeatJob(schemeInfoId, startTime, endTime, schemeInfoId, scheme.simpleValue, scheme.simpleType); break; case 3:// 明细频率执行 List cornlist = new List(); foreach (var fre in scheme.frequencyList) { string cron = "0 "; cron += fre.minute + " " + fre.hour + " "; switch (fre.type) { case "day": cron += "* "; break; case "week": cron += "? "; break; case "month": cron += fre.carryDate + " "; break; } cron += fre.carryMounth + " "; if (fre.type == "week") { cron += fre.carryDate + " "; } else { cron += "? "; } cron += "*"; cornlist.Add(cron); } await AddListCronJob(schemeInfoId, startTime, endTime, schemeInfoId, cornlist); break; case 4:// corn表达式 await AddCronJob(schemeInfoId, startTime, endTime, schemeInfoId, scheme.cornValue); break; } } /// /// /// /// public static async Task InitJob() { TSSchemeIBLL tSSchemeIBLL = IocManager.Instance.GetService(); var list = await tSSchemeIBLL.GetList(); foreach (var item in list) { await AddJob(item.F_Id, item.F_Scheme.ToObject()); } } #endregion 扩展应用 } }