jellyfin/MediaBrowser.Common/ScheduledTasks/WeeklyTrigger.cs

116 lines
3.3 KiB
C#
Raw Normal View History

2013-02-20 18:33:05 -07:00
using System;
using System.Threading;
using MediaBrowser.Model.Events;
2015-07-28 05:33:30 -07:00
using MediaBrowser.Model.Tasks;
2013-02-20 18:33:05 -07:00
namespace MediaBrowser.Common.ScheduledTasks
{
/// <summary>
/// Represents a task trigger that fires on a weekly basis
/// </summary>
2013-02-24 14:53:54 -07:00
public class WeeklyTrigger : ITaskTrigger
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Get the time of day to trigger the task to run
/// </summary>
/// <value>The time of day.</value>
public TimeSpan TimeOfDay { get; set; }
/// <summary>
/// Gets or sets the day of week.
/// </summary>
/// <value>The day of week.</value>
public DayOfWeek DayOfWeek { get; set; }
/// <summary>
/// Gets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
public TaskExecutionOptions TaskOptions { get; set; }
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the timer.
/// </summary>
/// <value>The timer.</value>
private Timer Timer { get; set; }
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
2015-07-28 05:33:30 -07:00
/// <param name="lastResult">The last result.</param>
2013-02-23 00:57:11 -07:00
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
2015-07-28 05:33:30 -07:00
public void Start(TaskResult lastResult, bool isApplicationStartup)
2013-02-20 18:33:05 -07:00
{
DisposeTimer();
var triggerDate = GetNextTriggerDateTime();
Timer = new Timer(state => OnTriggered(), null, triggerDate - DateTime.Now, TimeSpan.FromMilliseconds(-1));
}
/// <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
if (now.DayOfWeek == DayOfWeek)
{
// It's either later today, or a week from now
return now.TimeOfDay < TimeOfDay ? now.Date.Add(TimeOfDay) : now.Date.AddDays(7).Add(TimeOfDay);
}
var triggerDate = now.Date;
// Walk the date forward until we get to the trigger day
while (triggerDate.DayOfWeek != DayOfWeek)
{
triggerDate = triggerDate.AddDays(1);
}
// Return the trigger date plus the time offset
return triggerDate.Add(TimeOfDay);
}
/// <summary>
/// Stops waiting for the trigger action
/// </summary>
2013-02-24 14:53:54 -07:00
public void Stop()
2013-02-20 18:33:05 -07:00
{
DisposeTimer();
}
/// <summary>
2013-02-24 14:53:54 -07:00
/// Disposes the timer.
2013-02-20 18:33:05 -07:00
/// </summary>
2013-02-24 14:53:54 -07:00
private void DisposeTimer()
2013-02-20 18:33:05 -07:00
{
2013-02-24 14:53:54 -07:00
if (Timer != null)
2013-02-20 18:33:05 -07:00
{
2013-02-24 14:53:54 -07:00
Timer.Dispose();
2013-02-20 18:33:05 -07:00
}
}
/// <summary>
2013-02-24 14:53:54 -07:00
/// Occurs when [triggered].
2013-02-20 18:33:05 -07:00
/// </summary>
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
2013-02-24 14:53:54 -07:00
/// <summary>
/// Called when [triggered].
/// </summary>
private void OnTriggered()
2013-02-20 18:33:05 -07:00
{
2013-02-24 14:53:54 -07:00
if (Triggered != null)
2013-02-20 18:33:05 -07:00
{
Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
2013-02-20 18:33:05 -07:00
}
}
}
}