Scheduler.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json.Linq;
  7. using ProjectBase.Data.BaseDAL;
  8. using ProjectBase.Data.Logs;
  9. using Quartz;
  10. using Quartz.Impl;
  11. using SIMDP.Model;
  12. namespace SIMDP.BLL
  13. {
  14. public class Scheduler
  15. {
  16. #region 原始代码
  17. //private static string timeSpanClear = "0 0 11 1 * ?"; //每月1号11点执行
  18. //private static string timeSpanUpload = "0 */5 * * * ?"; // 每隔5分钟执行一次:
  19. //private static IScheduler scheduler;
  20. //private static IScheduler scheduler2;
  21. //private static IScheduler schedulerTest;
  22. ///// <summary>
  23. ///// 开启定时任务
  24. ///// </summary>
  25. //public static async Task Schedule()
  26. //{
  27. // try
  28. // {
  29. // // 1.创建scheduler的引用
  30. // StdSchedulerFactory factory = new StdSchedulerFactory();
  31. // scheduler = await factory.GetScheduler();
  32. // scheduler2 = await factory.GetScheduler();
  33. // schedulerTest = await factory.GetScheduler();
  34. // //2.启动 scheduler
  35. // await scheduler.Start();
  36. // await scheduler2.Start();
  37. // await schedulerTest.Start();
  38. // // 3.创建 job
  39. // IJobDetail job = JobBuilder.Create<ClearJobs>()
  40. // //.WithIdentity("clearJob", "group1")
  41. // //.WithDescription("This is clearJob")
  42. // .UsingJobData("YEARS", "3")
  43. // .Build();
  44. // IJobDetail job2 = JobBuilder.Create<UploadJobs>()
  45. // //.WithIdentity("uploadJob", "group1")
  46. // //.WithDescription("This is uploadJob")
  47. // .Build();
  48. // IJobDetail jobTest = JobBuilder.Create<TestJobs>()
  49. // .WithIdentity("TestJob", "group1")
  50. // .WithDescription("This is TestJob")
  51. // .UsingJobData("YEARS", "3")
  52. // .Build();
  53. // // 4.创建 trigger (创建 trigger 触发器)
  54. // ITrigger trigger = TriggerBuilder.Create()
  55. // //.WithIdentity("clearTrigger", "group1") //触发器 组
  56. // .StartNow()
  57. // //.WithCronSchedule("* * * * * ?")
  58. // .WithCronSchedule(timeSpanClear)
  59. // //.WithDescription("This is clearJob's clearTrigger")
  60. // .Build();
  61. // ITrigger trigger2 = TriggerBuilder.Create()
  62. // //.WithIdentity("uploadTrigger", "group1") //触发器 组
  63. // .StartNow()
  64. // //.WithCronSchedule("0 */1 * * * ?")
  65. // .WithCronSchedule(timeSpanUpload)
  66. // //.WithDescription("This is uploadJob's uploadTrigger")
  67. // .Build();
  68. // ITrigger triggerTest = TriggerBuilder.Create()
  69. // .WithIdentity("TestTrigger", "group1") //触发器 组
  70. // .StartNow()
  71. // .WithCronSchedule("* * * * * ?")
  72. // .WithDescription("This is TestJob's TestTrigger")
  73. // .Build();
  74. // // 5.使用trigger规划执行任务job (使用触发器规划执行任务)
  75. // await scheduler.ScheduleJob(job, trigger);
  76. // await scheduler2.ScheduleJob(job2, trigger2);
  77. // await schedulerTest.ScheduleJob(jobTest, triggerTest);
  78. // }
  79. // catch (Exception ex)
  80. // {
  81. // LogHelper.log.Error(string.Format("开启定时任务出现错误:{0}", ex.ToString()));
  82. // }
  83. //}
  84. ///// <summary>
  85. ///// 关闭定时任务
  86. ///// </summary>
  87. ///// <returns></returns>
  88. //public async static Task Stop()
  89. //{
  90. // try
  91. // {
  92. // await scheduler.Shutdown();
  93. // await scheduler2.Shutdown();
  94. // await schedulerTest.Shutdown();
  95. // }
  96. // catch (Exception ex)
  97. // {
  98. // LogHelper.log.Error(string.Format("关闭定时任务出现错误:{0}", ex.ToString()));
  99. // }
  100. //}
  101. #endregion
  102. private static List<MoSchedulerJob> jobList = new List<MoSchedulerJob>();
  103. private static List<IScheduler> schedulerList = new List<IScheduler>();
  104. public Scheduler()
  105. {
  106. }
  107. /// <summary>
  108. /// 开启定时任务
  109. /// </summary>
  110. public static async Task Schedule()
  111. {
  112. try
  113. {
  114. // 1.创建scheduler的引用
  115. StdSchedulerFactory factory = new StdSchedulerFactory();
  116. jobList = BLLFactory<BlSchedulerJob>.Instance.Find("job_valid = 1");
  117. if (jobList.Count <= 0)
  118. {
  119. return;
  120. }
  121. foreach (var jobItem in jobList)
  122. {
  123. schedulerList.Add(await factory.GetScheduler());
  124. }
  125. //2.启动 scheduler
  126. foreach (var item in schedulerList)
  127. {
  128. await item.Start();
  129. }
  130. for (int i = 0; i < schedulerList.Count; i++)
  131. {
  132. Type type = Type.GetType(jobList[i].JobName);
  133. string paraKey = "";
  134. string paraValue = "";
  135. JObject jObject = JObject.Parse(jobList[i].JobPara);
  136. JToken token = jObject["Key"];
  137. if (token != null) paraKey = token.ToString();
  138. token = jObject["Value"];
  139. if (token != null) paraValue = token.ToString();
  140. // 3.创建 job
  141. IJobDetail jobDetail;
  142. if (string.IsNullOrEmpty(paraKey) || string.IsNullOrEmpty(paraValue))
  143. {
  144. jobDetail = JobBuilder.Create(type)
  145. //.WithIdentity("TestJob", "group1")
  146. //.WithDescription("This is TestJob")
  147. .Build();
  148. }
  149. else
  150. {
  151. jobDetail = JobBuilder.Create(type)
  152. //.WithIdentity("TestJob", "group1")
  153. //.WithDescription("This is TestJob")
  154. .UsingJobData(paraKey, paraValue)
  155. .Build();
  156. }
  157. // 4.创建 trigger (创建 trigger 触发器)
  158. ITrigger trigger = TriggerBuilder.Create()
  159. //.WithIdentity("TestTrigger", "group1") //触发器 组
  160. .StartNow()
  161. .WithCronSchedule(jobList[i].JobTime)
  162. //.WithDescription("This is TestJob's TestTrigger")
  163. .Build();
  164. // 5.使用trigger规划执行任务job (使用触发器规划执行任务)
  165. await schedulerList[i].ScheduleJob(jobDetail, trigger);
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. LogHelper.log.Error(string.Format("开启定时任务出现错误:{0}", ex.ToString()));
  171. }
  172. }
  173. /// <summary>
  174. /// 关闭定时任务
  175. /// </summary>
  176. /// <returns></returns>
  177. public async static Task Stop()
  178. {
  179. try
  180. {
  181. if (schedulerList.Count <= 0)
  182. {
  183. return;
  184. }
  185. foreach (var item in schedulerList)
  186. {
  187. await item.Shutdown();
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. LogHelper.log.Error(string.Format("关闭定时任务出现错误:{0}", ex.ToString()));
  193. }
  194. }
  195. }
  196. }