CancelPushRecordTimedJob.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using Quartz;
  3. using Quartz.Impl;
  4. using Quartz.Impl.Matchers;
  5. using Quartz.Impl.Triggers;
  6. using Quartz.Spi;
  7. using System.Collections.Specialized;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using CoreEntity.TimeJob;
  13. using System.Collections.Generic;
  14. using Common.Wechat;
  15. using System.Threading;
  16. namespace CoreEntity.TimedTask
  17. {
  18. public class CancelPushRecordTimedJob
  19. {
  20. public const int HoursToCancel = Config.HoursToCancel;
  21. public class CancelJob : IJob
  22. {
  23. public CancelJob()
  24. {
  25. }
  26. public Task Execute(IJobExecutionContext context)
  27. {
  28. JobDataMap dataMap = context.JobDetail.JobDataMap;
  29. string RecordId = dataMap.GetString("RecordId");
  30. string result;
  31. CancelTask.CancelRecord(RecordId, out result );
  32. return Console.Out.WriteLineAsync("Greetings from CacncelJob!--"+ result);
  33. }
  34. }
  35. public static async void CancelPushRecord(string RecordId,DateTime start, int hour)
  36. {
  37. try
  38. {
  39. // Grab the Scheduler instance from the Factory
  40. NameValueCollection props = new NameValueCollection
  41. {
  42. { "quartz.serializer.type", "binary" }
  43. };
  44. StdSchedulerFactory factory = new StdSchedulerFactory(props);
  45. IScheduler scheduler = await factory.GetScheduler();
  46. // 启动任务调度器
  47. await scheduler.Start();
  48. // 定义一个 Job
  49. string key = "job_" + RecordId;
  50. IJobDetail job = JobBuilder.Create<CancelJob>()
  51. .WithIdentity(key, "CancelRecord")
  52. .UsingJobData("RecordId", RecordId)
  53. .Build();
  54. DateTime past = DateTime.Now;
  55. ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
  56. .WithIdentity("trigger_" + key) // 给任务一个名字
  57. //.StartAt(start) // 设置任务开始时间
  58. //.StartAt(past)
  59. //.StartNow()
  60. .StartAt(DateBuilder.FutureDate(hour, IntervalUnit.Hour))
  61. .ForJob(key, "CancelRecord") //给任务指定一个分组
  62. //.WithSimpleSchedule(x => x
  63. //.WithIntervalInHours(hour)
  64. //.WithIntervalInSeconds(1) //循环的时间 1秒1次
  65. //.RepeatForever()
  66. //.WithRepeatCount(0))
  67. .Build();
  68. ////每天9:30:00执行
  69. //CronTriggerImpl trigger = (CronTriggerImpl)TriggerBuilder.Create()
  70. // .WithIdentity("trigger1") // 给任务一个名字
  71. // .StartAt(DateTime.Now) // 设置任务开始时间
  72. // .ForJob("job1", "group1") //给任务指定一个分组
  73. // .WithCronSchedule(second + " " + minute + " " + hour + " * * ?")
  74. // .Build();
  75. //GroupMatcher<JobKey> groupMatcher = GroupMatcher<JobKey>.GroupEquals("CancelRecord");
  76. //IReadOnlyCollection<JobKey> set = await scheduler.GetJobKeys(groupMatcher);
  77. //if (set.Count > 0)
  78. //{
  79. // foreach (JobKey jobKey in set)
  80. // {
  81. // if (jobKey.Name == "job_" +RecordId)
  82. // {
  83. // bool delsuccess = await scheduler.DeleteJob(jobKey);
  84. // }
  85. // }
  86. //}
  87. // 等待执行任务
  88. await scheduler.ScheduleJob(job, trigger);
  89. // some sleep to show what's happening
  90. //await Task.Delay(TimeSpan.FromMilliseconds(2000));
  91. }
  92. catch (SchedulerException se)
  93. {
  94. await Console.Error.WriteLineAsync(se.ToString());
  95. }
  96. }
  97. }
  98. }