123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json.Linq;
- using ProjectBase.Data.BaseDAL;
- using ProjectBase.Data.Logs;
- using Quartz;
- using Quartz.Impl;
- using SIMDP.Model;
- namespace SIMDP.BLL
- {
- public class Scheduler
- {
- #region 原始代码
- //private static string timeSpanClear = "0 0 11 1 * ?"; //每月1号11点执行
- //private static string timeSpanUpload = "0 */5 * * * ?"; // 每隔5分钟执行一次:
- //private static IScheduler scheduler;
- //private static IScheduler scheduler2;
- //private static IScheduler schedulerTest;
- ///// <summary>
- ///// 开启定时任务
- ///// </summary>
- //public static async Task Schedule()
- //{
- // try
- // {
- // // 1.创建scheduler的引用
- // StdSchedulerFactory factory = new StdSchedulerFactory();
- // scheduler = await factory.GetScheduler();
- // scheduler2 = await factory.GetScheduler();
- // schedulerTest = await factory.GetScheduler();
- // //2.启动 scheduler
- // await scheduler.Start();
- // await scheduler2.Start();
- // await schedulerTest.Start();
- // // 3.创建 job
- // IJobDetail job = JobBuilder.Create<ClearJobs>()
- // //.WithIdentity("clearJob", "group1")
- // //.WithDescription("This is clearJob")
- // .UsingJobData("YEARS", "3")
- // .Build();
- // IJobDetail job2 = JobBuilder.Create<UploadJobs>()
- // //.WithIdentity("uploadJob", "group1")
- // //.WithDescription("This is uploadJob")
- // .Build();
- // IJobDetail jobTest = JobBuilder.Create<TestJobs>()
- // .WithIdentity("TestJob", "group1")
- // .WithDescription("This is TestJob")
- // .UsingJobData("YEARS", "3")
- // .Build();
- // // 4.创建 trigger (创建 trigger 触发器)
- // ITrigger trigger = TriggerBuilder.Create()
- // //.WithIdentity("clearTrigger", "group1") //触发器 组
- // .StartNow()
- // //.WithCronSchedule("* * * * * ?")
- // .WithCronSchedule(timeSpanClear)
- // //.WithDescription("This is clearJob's clearTrigger")
- // .Build();
- // ITrigger trigger2 = TriggerBuilder.Create()
- // //.WithIdentity("uploadTrigger", "group1") //触发器 组
- // .StartNow()
- // //.WithCronSchedule("0 */1 * * * ?")
- // .WithCronSchedule(timeSpanUpload)
- // //.WithDescription("This is uploadJob's uploadTrigger")
- // .Build();
- // ITrigger triggerTest = TriggerBuilder.Create()
- // .WithIdentity("TestTrigger", "group1") //触发器 组
- // .StartNow()
- // .WithCronSchedule("* * * * * ?")
- // .WithDescription("This is TestJob's TestTrigger")
- // .Build();
- // // 5.使用trigger规划执行任务job (使用触发器规划执行任务)
- // await scheduler.ScheduleJob(job, trigger);
- // await scheduler2.ScheduleJob(job2, trigger2);
- // await schedulerTest.ScheduleJob(jobTest, triggerTest);
- // }
- // catch (Exception ex)
- // {
- // LogHelper.log.Error(string.Format("开启定时任务出现错误:{0}", ex.ToString()));
- // }
- //}
- ///// <summary>
- ///// 关闭定时任务
- ///// </summary>
- ///// <returns></returns>
- //public async static Task Stop()
- //{
- // try
- // {
- // await scheduler.Shutdown();
- // await scheduler2.Shutdown();
- // await schedulerTest.Shutdown();
- // }
- // catch (Exception ex)
- // {
- // LogHelper.log.Error(string.Format("关闭定时任务出现错误:{0}", ex.ToString()));
- // }
- //}
- #endregion
- private static List<MoSchedulerJob> jobList = new List<MoSchedulerJob>();
- private static List<IScheduler> schedulerList = new List<IScheduler>();
- public Scheduler()
- {
- }
- /// <summary>
- /// 开启定时任务
- /// </summary>
- public static async Task Schedule()
- {
- try
- {
- // 1.创建scheduler的引用
- StdSchedulerFactory factory = new StdSchedulerFactory();
- jobList = BLLFactory<BlSchedulerJob>.Instance.Find("job_valid = 1");
- if (jobList.Count <= 0)
- {
- return;
- }
- foreach (var jobItem in jobList)
- {
- schedulerList.Add(await factory.GetScheduler());
- }
- //2.启动 scheduler
- foreach (var item in schedulerList)
- {
- await item.Start();
- }
- for (int i = 0; i < schedulerList.Count; i++)
- {
- Type type = Type.GetType(jobList[i].JobName);
- string paraKey = "";
- string paraValue = "";
- JObject jObject = JObject.Parse(jobList[i].JobPara);
- JToken token = jObject["Key"];
- if (token != null) paraKey = token.ToString();
- token = jObject["Value"];
- if (token != null) paraValue = token.ToString();
- // 3.创建 job
- IJobDetail jobDetail;
- if (string.IsNullOrEmpty(paraKey) || string.IsNullOrEmpty(paraValue))
- {
- jobDetail = JobBuilder.Create(type)
- //.WithIdentity("TestJob", "group1")
- //.WithDescription("This is TestJob")
- .Build();
- }
- else
- {
- jobDetail = JobBuilder.Create(type)
- //.WithIdentity("TestJob", "group1")
- //.WithDescription("This is TestJob")
- .UsingJobData(paraKey, paraValue)
- .Build();
- }
- // 4.创建 trigger (创建 trigger 触发器)
- ITrigger trigger = TriggerBuilder.Create()
- //.WithIdentity("TestTrigger", "group1") //触发器 组
- .StartNow()
- .WithCronSchedule(jobList[i].JobTime)
- //.WithDescription("This is TestJob's TestTrigger")
- .Build();
- // 5.使用trigger规划执行任务job (使用触发器规划执行任务)
- await schedulerList[i].ScheduleJob(jobDetail, trigger);
- }
- }
- catch (Exception ex)
- {
- LogHelper.log.Error(string.Format("开启定时任务出现错误:{0}", ex.ToString()));
- }
- }
- /// <summary>
- /// 关闭定时任务
- /// </summary>
- /// <returns></returns>
- public async static Task Stop()
- {
- try
- {
- if (schedulerList.Count <= 0)
- {
- return;
- }
- foreach (var item in schedulerList)
- {
- await item.Shutdown();
- }
- }
- catch (Exception ex)
- {
- LogHelper.log.Error(string.Format("关闭定时任务出现错误:{0}", ex.ToString()));
- }
- }
- }
- }
|