QuartzJobService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using IMCS.CCS.Common;
  2. using IMCS.CCS.Entitys;
  3. using IMCS.CCS.Service.Jobs;
  4. using Quartz;
  5. using Quartz.Impl.Matchers;
  6. using Quartz.Impl.Triggers;
  7. using System;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. namespace IMCS.CCS.Service.Impl
  11. {
  12. /// <summary>
  13. /// 任务调度服务
  14. /// </summary>
  15. public class QuartzJobService : IQuartzJobService
  16. {
  17. private readonly ISchedulerFactory _schedulerFactory;
  18. private readonly ResultfulApiJobFactory _resultfulApiJobFactory;
  19. public QuartzJobService(ISchedulerFactory schedulerFactory, ResultfulApiJobFactory resultfulApiJobFactory)
  20. {
  21. _schedulerFactory = schedulerFactory;
  22. _resultfulApiJobFactory = resultfulApiJobFactory;
  23. }
  24. /// <summary>
  25. /// 开始运行一个调度器
  26. /// </summary>
  27. /// <param name="tasks"></param>
  28. /// <returns></returns>
  29. public async Task<bool> RunAsync(Tasks tasks)
  30. {
  31. //1、通过调度工厂获得调度器
  32. var scheduler = await _schedulerFactory.GetScheduler();
  33. var taskName = $"{tasks.ProjectId}>{tasks.ProjectName}>{tasks.Name}";
  34. //2、创建一个触发器
  35. var trigger = TriggerBuilder.Create()
  36. .WithIdentity(taskName, tasks.GroupName)
  37. .StartNow()
  38. .WithDescription(tasks.Remark)
  39. // 触发表达式 0 0 0 1 1 ?
  40. .WithCronSchedule(tasks.Cron)
  41. .Build();
  42. //3、创建任务
  43. var jobDetail = JobBuilder.Create<ResultfulApiJob>()
  44. .WithIdentity(taskName, tasks.GroupName)
  45. .UsingJobData("TasksId", tasks.Id.ToString())
  46. .Build();
  47. //4、写入 Job 实例工厂 解决 Job 中取 ioc 对象
  48. scheduler.JobFactory = _resultfulApiJobFactory;
  49. //5、将触发器和任务器绑定到调度器中
  50. await scheduler.ScheduleJob(jobDetail, trigger);
  51. //6、开启调度器
  52. await scheduler.Start();
  53. return await Task.FromResult(true);
  54. }
  55. /// <summary>
  56. /// 关闭调度器
  57. /// </summary>
  58. /// <param name="tasks"></param>
  59. /// <returns></returns>
  60. public async Task<bool> CloseAsync(Tasks tasks)
  61. {
  62. IScheduler scheduler = await _schedulerFactory.GetScheduler();
  63. var jobKeys = (await scheduler
  64. .GetJobKeys(GroupMatcher<JobKey>.GroupEquals(tasks.GroupName)))
  65. .ToList();
  66. var taskName = $"{tasks.ProjectId}>{tasks.ProjectName}>{tasks.Name}";
  67. if (jobKeys == null || jobKeys.Count() == 0)
  68. {
  69. throw new MessageBox($"未找到分组:{tasks.GroupName}");
  70. }
  71. JobKey jobKey = jobKeys
  72. .FirstOrDefault(w => scheduler.GetTriggersOfJob(w).Result.Any(x => (x as CronTriggerImpl).Name == taskName));
  73. if (jobKey == null)
  74. {
  75. throw new MessageBox($"未找到触发器:{taskName}");
  76. }
  77. //
  78. var triggers = await scheduler.GetTriggersOfJob(jobKey);
  79. ITrigger trigger = triggers?.Where(x => (x as CronTriggerImpl).Name == taskName).FirstOrDefault();
  80. if (trigger == null)
  81. {
  82. throw new MessageBox($"未找到触发器:{taskName}");
  83. }
  84. //
  85. await scheduler.PauseTrigger(trigger.Key);
  86. await scheduler.UnscheduleJob(trigger.Key);// 移除触发器
  87. await scheduler.DeleteJob(trigger.JobKey);
  88. return await Task.FromResult(true);
  89. }
  90. }
  91. }