IntervalTrigger.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using MediaBrowser.Model.Events;
  2. using MediaBrowser.Model.Tasks;
  3. using System;
  4. using System.Threading;
  5. namespace MediaBrowser.Common.ScheduledTasks
  6. {
  7. /// <summary>
  8. /// Represents a task trigger that runs repeatedly on an interval
  9. /// </summary>
  10. public class IntervalTrigger : ITaskTrigger
  11. {
  12. /// <summary>
  13. /// Gets or sets the interval.
  14. /// </summary>
  15. /// <value>The interval.</value>
  16. public TimeSpan Interval { get; set; }
  17. /// <summary>
  18. /// Gets or sets the timer.
  19. /// </summary>
  20. /// <value>The timer.</value>
  21. private Timer Timer { get; set; }
  22. /// <summary>
  23. /// Gets the execution properties of this task.
  24. /// </summary>
  25. /// <value>
  26. /// The execution properties of this task.
  27. /// </value>
  28. public TaskExecutionOptions TaskOptions { get; set; }
  29. /// <summary>
  30. /// Gets or sets the first run delay.
  31. /// </summary>
  32. /// <value>The first run delay.</value>
  33. public TimeSpan FirstRunDelay { get; set; }
  34. public IntervalTrigger()
  35. {
  36. FirstRunDelay = TimeSpan.FromHours(1);
  37. }
  38. /// <summary>
  39. /// Stars waiting for the trigger action
  40. /// </summary>
  41. /// <param name="lastResult">The last result.</param>
  42. /// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
  43. public void Start(TaskResult lastResult, bool isApplicationStartup)
  44. {
  45. DisposeTimer();
  46. var triggerDate = lastResult != null ?
  47. lastResult.EndTimeUtc.Add(Interval) :
  48. DateTime.UtcNow.Add(FirstRunDelay);
  49. if (DateTime.UtcNow > triggerDate)
  50. {
  51. if (isApplicationStartup)
  52. {
  53. triggerDate = DateTime.UtcNow.AddMinutes(1);
  54. }
  55. else
  56. {
  57. triggerDate = DateTime.UtcNow.AddMinutes(1);
  58. }
  59. }
  60. Timer = new Timer(state => OnTriggered(), null, triggerDate - DateTime.UtcNow, TimeSpan.FromMilliseconds(-1));
  61. }
  62. /// <summary>
  63. /// Stops waiting for the trigger action
  64. /// </summary>
  65. public void Stop()
  66. {
  67. DisposeTimer();
  68. }
  69. /// <summary>
  70. /// Disposes the timer.
  71. /// </summary>
  72. private void DisposeTimer()
  73. {
  74. if (Timer != null)
  75. {
  76. Timer.Dispose();
  77. }
  78. }
  79. /// <summary>
  80. /// Occurs when [triggered].
  81. /// </summary>
  82. public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
  83. /// <summary>
  84. /// Called when [triggered].
  85. /// </summary>
  86. private void OnTriggered()
  87. {
  88. if (Triggered != null)
  89. {
  90. Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
  91. }
  92. }
  93. }
  94. }