You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
8.8 KiB

2 years ago
  1. using log4net;
  2. using Quartz;
  3. using Quartz.Impl;
  4. using System;
  5. using System.Threading.Tasks;
  6. namespace WebApp.Jobs.ClearFiles
  7. {
  8. public class RunJob : RJob
  9. {
  10. private static ILog log = LogManager.GetLogger(typeof(RunJob));
  11. public virtual async Task Run()
  12. {
  13. log.Debug("------- Initializing -------------------");
  14. // First we must get a reference to a scheduler
  15. ISchedulerFactory sf = new StdSchedulerFactory();
  16. var sched = await sf.GetScheduler();
  17. log.Debug("------- Initialization Complete --------");
  18. log.Debug("------- Scheduling Jobs ----------------");
  19. // jobs can be scheduled before sched.start() has been called
  20. // get a "nice round" time a few seconds in the future...
  21. var startTime = DateBuilder.NextGivenSecondDate(null, 15);
  22. // job1 will only fire once at date/time "ts"
  23. var job = JobBuilder.Create<ClearFilesJob>()
  24. .WithIdentity("job1", "group1")
  25. .Build();
  26. var trigger = (ISimpleTrigger)TriggerBuilder.Create()
  27. .WithIdentity("trigger1", "group1")
  28. .StartAt(startTime)
  29. .Build();
  30. // schedule it to run!
  31. DateTimeOffset? ft = await sched.ScheduleJob(job, trigger);
  32. log.Debug(job.Key +
  33. " will run at: " + ft +
  34. " and repeat: " + trigger.RepeatCount +
  35. " times, every " + trigger.RepeatInterval.TotalSeconds + " seconds");
  36. // job2 will only fire once at date/time "ts"
  37. job = JobBuilder.Create<ClearFilesJob>()
  38. .WithIdentity("job2", "group1")
  39. .Build();
  40. trigger = (ISimpleTrigger)TriggerBuilder.Create()
  41. .WithIdentity("trigger2", "group1")
  42. .StartAt(startTime)
  43. .Build();
  44. ft = await sched.ScheduleJob(job, trigger);
  45. log.Debug(job.Key +
  46. " will run at: " + ft +
  47. " and repeat: " + trigger.RepeatCount +
  48. " times, every " + trigger.RepeatInterval.TotalSeconds + " seconds");
  49. // job3 will run 11 times (run once and repeat 10 more times) job3 will repeat every 10 seconds
  50. job = JobBuilder.Create<ClearFilesJob>()
  51. .WithIdentity("job3", "group1")
  52. .Build();
  53. trigger = (ISimpleTrigger)TriggerBuilder.Create()
  54. .WithIdentity("trigger3", "group1")
  55. .StartAt(startTime)
  56. .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(10))
  57. .Build();
  58. ft = await sched.ScheduleJob(job, trigger);
  59. log.Debug(job.Key +
  60. " will run at: " + ft +
  61. " and repeat: " + trigger.RepeatCount +
  62. " times, every " + trigger.RepeatInterval.TotalSeconds + " seconds");
  63. // the same job (job3) will be scheduled by a another trigger this time will only repeat
  64. // twice at a 70 second interval
  65. trigger = (ISimpleTrigger)TriggerBuilder.Create()
  66. .WithIdentity("trigger3", "group2")
  67. .StartAt(startTime)
  68. .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(2))
  69. .ForJob(job)
  70. .Build();
  71. ft = await sched.ScheduleJob(trigger);
  72. log.Debug(job.Key +
  73. " will [also] run at: " + ft +
  74. " and repeat: " + trigger.RepeatCount +
  75. " times, every " + trigger.RepeatInterval.TotalSeconds + " seconds");
  76. // job4 will run 6 times (run once and repeat 5 more times) job4 will repeat every 10 seconds
  77. job = JobBuilder.Create<ClearFilesJob>()
  78. .WithIdentity("job4", "group1")
  79. .Build();
  80. trigger = (ISimpleTrigger)TriggerBuilder.Create()
  81. .WithIdentity("trigger4", "group1")
  82. .StartAt(startTime)
  83. .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(5))
  84. .Build();
  85. ft = await sched.ScheduleJob(job, trigger);
  86. log.Debug(job.Key +
  87. " will run at: " + ft +
  88. " and repeat: " + trigger.RepeatCount +
  89. " times, every " + trigger.RepeatInterval.TotalSeconds + " seconds");
  90. // job5 will run once, five minutes in the future
  91. job = JobBuilder.Create<ClearFilesJob>()
  92. .WithIdentity("job5", "group1")
  93. .Build();
  94. trigger = (ISimpleTrigger)TriggerBuilder.Create()
  95. .WithIdentity("trigger5", "group1")
  96. .StartAt(DateBuilder.FutureDate(5, IntervalUnit.Minute))
  97. .Build();
  98. ft = await sched.ScheduleJob(job, trigger);
  99. log.Debug(job.Key +
  100. " will run at: " + ft +
  101. " and repeat: " + trigger.RepeatCount +
  102. " times, every " + trigger.RepeatInterval.TotalSeconds + " seconds");
  103. // job6 will run indefinitely, every 40 seconds
  104. job = JobBuilder.Create<ClearFilesJob>()
  105. .WithIdentity("job6", "group1")
  106. .Build();
  107. trigger = (ISimpleTrigger)TriggerBuilder.Create()
  108. .WithIdentity("trigger6", "group1")
  109. .StartAt(startTime)
  110. .WithSimpleSchedule(x => x.WithIntervalInSeconds(40).RepeatForever())
  111. .Build();
  112. ft = await sched.ScheduleJob(job, trigger);
  113. log.Debug(job.Key +
  114. " will run at: " + ft +
  115. " and repeat: " + trigger.RepeatCount +
  116. " times, every " + trigger.RepeatInterval.TotalSeconds + " seconds");
  117. log.Debug("------- Starting Scheduler ----------------");
  118. // All of the jobs have been added to the scheduler, but none of the jobs will run until
  119. // the scheduler has been started
  120. await sched.Start();
  121. log.Debug("------- Started Scheduler -----------------");
  122. // jobs can also be scheduled after start() has been called... job7 will repeat 20 times,
  123. // repeat every five minutes
  124. job = JobBuilder.Create<ClearFilesJob>()
  125. .WithIdentity("job7", "group1")
  126. .Build();
  127. trigger = (ISimpleTrigger)TriggerBuilder.Create()
  128. .WithIdentity("trigger7", "group1")
  129. .StartAt(startTime)
  130. .WithSimpleSchedule(x => x.WithIntervalInMinutes(5).WithRepeatCount(20))
  131. .Build();
  132. ft = await sched.ScheduleJob(job, trigger);
  133. log.Debug(job.Key +
  134. " will run at: " + ft +
  135. " and repeat: " + trigger.RepeatCount +
  136. " times, every " + trigger.RepeatInterval.TotalSeconds + " seconds");
  137. // jobs can be fired directly... (rather than waiting for a trigger)
  138. job = JobBuilder.Create<ClearFilesJob>()
  139. .WithIdentity("job8", "group1")
  140. .StoreDurably()
  141. .Build();
  142. await sched.AddJob(job, true);
  143. log.Debug("'Manually' triggering job8...");
  144. await sched.TriggerJob(new JobKey("job8", "group1"));
  145. log.Debug("------- Waiting 30 seconds... --------------");
  146. try
  147. {
  148. // wait 30 seconds to show jobs
  149. await Task.Delay(TimeSpan.FromSeconds(30));
  150. // executing...
  151. }
  152. catch (Exception)
  153. {
  154. throw;
  155. }
  156. // jobs can be re-scheduled... job 7 will run immediately and repeat 10 times for every second
  157. log.Debug("------- Rescheduling... --------------------");
  158. trigger = (ISimpleTrigger)TriggerBuilder.Create()
  159. .WithIdentity("trigger7", "group1")
  160. .StartAt(startTime)
  161. .WithSimpleSchedule(x => x.WithIntervalInMinutes(5).WithRepeatCount(20))
  162. .Build();
  163. ft = await sched.RescheduleJob(trigger.Key, trigger);
  164. log.Debug("job7 rescheduled to run at: " + ft);
  165. log.Debug("------- Waiting five minutes... ------------");
  166. // wait five minutes to show jobs
  167. await Task.Delay(TimeSpan.FromMinutes(5));
  168. // executing...
  169. log.Debug("------- Shutting Down ---------------------");
  170. await sched.Shutdown(true);
  171. log.Debug("------- Shutdown Complete -----------------");
  172. // display some stats about the schedule that just ran
  173. var metaData = await sched.GetMetaData();
  174. log.Debug($"Executed {metaData.NumberOfJobsExecuted} jobs.");
  175. }
  176. }
  177. }