2013-02-21 02:33:05 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
2015-02-25 21:55:01 +01:00
|
|
|
|
using MediaBrowser.Model.Events;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.ScheduledTasks
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a task trigger that fires everyday
|
|
|
|
|
/// </summary>
|
2013-02-24 22:53:54 +01:00
|
|
|
|
public class DailyTrigger : ITaskTrigger
|
2013-02-21 02:33:05 +01: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 timer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The timer.</value>
|
|
|
|
|
private Timer Timer { get; set; }
|
|
|
|
|
|
2015-02-25 21:55:01 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the execution properties of this task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>
|
|
|
|
|
/// The execution properties of this task.
|
|
|
|
|
/// </value>
|
|
|
|
|
public TaskExecutionOptions TaskOptions { get; set; }
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stars waiting for the trigger action
|
|
|
|
|
/// </summary>
|
2013-02-23 08:57:11 +01:00
|
|
|
|
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
|
2013-02-24 22:53:54 +01:00
|
|
|
|
public void Start(bool isApplicationStartup)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
var triggerDate = now.TimeOfDay > TimeOfDay ? now.Date.AddDays(1) : now.Date;
|
|
|
|
|
triggerDate = triggerDate.Add(TimeOfDay);
|
|
|
|
|
|
|
|
|
|
Timer = new Timer(state => OnTriggered(), null, triggerDate - now, TimeSpan.FromMilliseconds(-1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops waiting for the trigger action
|
|
|
|
|
/// </summary>
|
2013-02-24 22:53:54 +01:00
|
|
|
|
public void Stop()
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// Disposes the timer.
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
2013-02-24 22:53:54 +01:00
|
|
|
|
private void DisposeTimer()
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-02-24 22:53:54 +01:00
|
|
|
|
if (Timer != null)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-02-24 22:53:54 +01:00
|
|
|
|
Timer.Dispose();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// Occurs when [triggered].
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
2015-02-25 21:55:01 +01:00
|
|
|
|
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [triggered].
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnTriggered()
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-02-24 22:53:54 +01:00
|
|
|
|
if (Triggered != null)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-02-25 21:55:01 +01:00
|
|
|
|
Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|