jellyfin/Emby.Server.Implementations/ScheduledTasks/Triggers/WeeklyTrigger.cs

123 lines
3.7 KiB
C#
Raw Normal View History

using System;
2016-10-29 05:40:15 +00:00
using System.Threading;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
2016-10-29 05:40:15 +00:00
2021-12-15 17:25:36 +00:00
namespace Emby.Server.Implementations.ScheduledTasks.Triggers
2016-10-29 05:40:15 +00:00
{
/// <summary>
/// Represents a task trigger that fires on a weekly basis.
2016-10-29 05:40:15 +00:00
/// </summary>
2022-02-15 17:59:46 +00:00
public sealed class WeeklyTrigger : ITaskTrigger, IDisposable
2016-10-29 05:40:15 +00:00
{
2021-05-28 12:33:54 +00:00
private readonly TimeSpan _timeOfDay;
private readonly DayOfWeek _dayOfWeek;
private Timer? _timer;
2022-02-15 17:59:46 +00:00
private bool _disposed = false;
2016-10-29 05:40:15 +00:00
/// <summary>
2021-05-28 12:33:54 +00:00
/// Initializes a new instance of the <see cref="WeeklyTrigger"/> class.
2016-10-29 05:40:15 +00:00
/// </summary>
2021-05-28 12:33:54 +00:00
/// <param name="timeofDay">The time of day to trigger the task to run.</param>
/// <param name="dayOfWeek">The day of week.</param>
/// <param name="taskOptions">The options of this task.</param>
public WeeklyTrigger(TimeSpan timeofDay, DayOfWeek dayOfWeek, TaskOptions taskOptions)
{
_timeOfDay = timeofDay;
_dayOfWeek = dayOfWeek;
TaskOptions = taskOptions;
}
2016-10-29 05:40:15 +00:00
/// <summary>
2021-05-28 12:33:54 +00:00
/// Occurs when [triggered].
2016-10-29 05:40:15 +00:00
/// </summary>
2021-05-28 12:33:54 +00:00
public event EventHandler<EventArgs>? Triggered;
2016-10-29 05:40:15 +00:00
/// <summary>
2021-05-28 12:33:54 +00:00
/// Gets the options of this task.
2016-10-29 05:40:15 +00:00
/// </summary>
2021-05-28 12:33:54 +00:00
public TaskOptions TaskOptions { get; }
2016-10-29 05:40:15 +00:00
/// <summary>
/// Stars waiting for the trigger action.
2016-10-29 05:40:15 +00:00
/// </summary>
/// <param name="lastResult">The last result.</param>
/// <param name="logger">The logger.</param>
/// <param name="taskName">The name of the task.</param>
2016-10-29 05:40:15 +00:00
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
2021-12-15 17:25:36 +00:00
public void Start(TaskResult? lastResult, ILogger logger, string taskName, bool isApplicationStartup)
2016-10-29 05:40:15 +00:00
{
DisposeTimer();
var triggerDate = GetNextTriggerDateTime();
2021-12-15 17:25:36 +00:00
_timer = new Timer(_ => OnTriggered(), null, triggerDate - DateTime.Now, TimeSpan.FromMilliseconds(-1));
2016-10-29 05:40:15 +00:00
}
/// <summary>
/// Gets the next trigger date time.
/// </summary>
/// <returns>DateTime.</returns>
private DateTime GetNextTriggerDateTime()
{
var now = DateTime.Now;
// If it's on the same day
2021-05-28 12:33:54 +00:00
if (now.DayOfWeek == _dayOfWeek)
2016-10-29 05:40:15 +00:00
{
// It's either later today, or a week from now
2021-05-28 12:33:54 +00:00
return now.TimeOfDay < _timeOfDay ? now.Date.Add(_timeOfDay) : now.Date.AddDays(7).Add(_timeOfDay);
2016-10-29 05:40:15 +00:00
}
var triggerDate = now.Date;
// Walk the date forward until we get to the trigger day
2021-05-28 12:33:54 +00:00
while (triggerDate.DayOfWeek != _dayOfWeek)
2016-10-29 05:40:15 +00:00
{
triggerDate = triggerDate.AddDays(1);
}
// Return the trigger date plus the time offset
2021-05-28 12:33:54 +00:00
return triggerDate.Add(_timeOfDay);
2016-10-29 05:40:15 +00:00
}
/// <summary>
/// Stops waiting for the trigger action.
2016-10-29 05:40:15 +00:00
/// </summary>
public void Stop()
{
DisposeTimer();
}
/// <summary>
/// Disposes the timer.
/// </summary>
private void DisposeTimer()
{
2021-05-28 12:33:54 +00:00
_timer?.Dispose();
2022-02-15 17:59:46 +00:00
_timer = null;
2016-10-29 05:40:15 +00:00
}
/// <summary>
/// Called when [triggered].
/// </summary>
private void OnTriggered()
{
2020-08-31 20:20:19 +00:00
Triggered?.Invoke(this, EventArgs.Empty);
2016-10-29 05:40:15 +00:00
}
2022-02-15 17:59:46 +00:00
/// <inheritdoc />
public void Dispose()
{
if (_disposed)
{
return;
}
DisposeTimer();
_disposed = true;
}
2016-10-29 05:40:15 +00:00
}
}