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