2
0

WeeklyTrigger.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Threading;
  3. using MediaBrowser.Model.Tasks;
  4. using Microsoft.Extensions.Logging;
  5. namespace Emby.Server.Implementations.ScheduledTasks.Triggers
  6. {
  7. /// <summary>
  8. /// Represents a task trigger that fires on a weekly basis.
  9. /// </summary>
  10. public sealed class WeeklyTrigger : ITaskTrigger, IDisposable
  11. {
  12. private readonly TimeSpan _timeOfDay;
  13. private readonly DayOfWeek _dayOfWeek;
  14. private Timer? _timer;
  15. private bool _disposed = false;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="WeeklyTrigger"/> class.
  18. /// </summary>
  19. /// <param name="timeofDay">The time of day to trigger the task to run.</param>
  20. /// <param name="dayOfWeek">The day of week.</param>
  21. /// <param name="taskOptions">The options of this task.</param>
  22. public WeeklyTrigger(TimeSpan timeofDay, DayOfWeek dayOfWeek, TaskOptions taskOptions)
  23. {
  24. _timeOfDay = timeofDay;
  25. _dayOfWeek = dayOfWeek;
  26. TaskOptions = taskOptions;
  27. }
  28. /// <summary>
  29. /// Occurs when [triggered].
  30. /// </summary>
  31. public event EventHandler<EventArgs>? Triggered;
  32. /// <summary>
  33. /// Gets the options of this task.
  34. /// </summary>
  35. public TaskOptions TaskOptions { get; }
  36. /// <summary>
  37. /// Stars waiting for the trigger action.
  38. /// </summary>
  39. /// <param name="lastResult">The last result.</param>
  40. /// <param name="logger">The logger.</param>
  41. /// <param name="taskName">The name of the task.</param>
  42. /// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
  43. public void Start(TaskResult? lastResult, ILogger logger, string taskName, bool isApplicationStartup)
  44. {
  45. DisposeTimer();
  46. var triggerDate = GetNextTriggerDateTime();
  47. _timer = new Timer(_ => OnTriggered(), null, triggerDate - DateTime.Now, TimeSpan.FromMilliseconds(-1));
  48. }
  49. /// <summary>
  50. /// Gets the next trigger date time.
  51. /// </summary>
  52. /// <returns>DateTime.</returns>
  53. private DateTime GetNextTriggerDateTime()
  54. {
  55. var now = DateTime.Now;
  56. // If it's on the same day
  57. if (now.DayOfWeek == _dayOfWeek)
  58. {
  59. // It's either later today, or a week from now
  60. return now.TimeOfDay < _timeOfDay ? now.Date.Add(_timeOfDay) : now.Date.AddDays(7).Add(_timeOfDay);
  61. }
  62. var triggerDate = now.Date;
  63. // Walk the date forward until we get to the trigger day
  64. while (triggerDate.DayOfWeek != _dayOfWeek)
  65. {
  66. triggerDate = triggerDate.AddDays(1);
  67. }
  68. // Return the trigger date plus the time offset
  69. return triggerDate.Add(_timeOfDay);
  70. }
  71. /// <summary>
  72. /// Stops waiting for the trigger action.
  73. /// </summary>
  74. public void Stop()
  75. {
  76. DisposeTimer();
  77. }
  78. /// <summary>
  79. /// Disposes the timer.
  80. /// </summary>
  81. private void DisposeTimer()
  82. {
  83. _timer?.Dispose();
  84. _timer = null;
  85. }
  86. /// <summary>
  87. /// Called when [triggered].
  88. /// </summary>
  89. private void OnTriggered()
  90. {
  91. Triggered?.Invoke(this, EventArgs.Empty);
  92. }
  93. /// <inheritdoc />
  94. public void Dispose()
  95. {
  96. if (_disposed)
  97. {
  98. return;
  99. }
  100. DisposeTimer();
  101. _disposed = true;
  102. }
  103. }
  104. }