jellyfin-server/MediaBrowser.Common/ScheduledTasks/IntervalTrigger.cs

108 lines
3.0 KiB
C#
Raw Normal View History

2015-07-28 12:33:30 +00:00
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
using System;
2013-02-21 01:33:05 +00:00
using System.Threading;
namespace MediaBrowser.Common.ScheduledTasks
{
/// <summary>
/// Represents a task trigger that runs repeatedly on an interval
/// </summary>
2013-02-24 21:53:54 +00:00
public class IntervalTrigger : ITaskTrigger
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the interval.
/// </summary>
/// <value>The interval.</value>
public TimeSpan Interval { get; set; }
/// <summary>
/// Gets or sets the timer.
/// </summary>
/// <value>The timer.</value>
private Timer Timer { get; set; }
/// <summary>
/// Gets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
public TaskExecutionOptions TaskOptions { get; set; }
2015-08-27 01:31:54 +00:00
/// <summary>
/// Gets or sets the first run delay.
/// </summary>
/// <value>The first run delay.</value>
public TimeSpan FirstRunDelay { get; set; }
public IntervalTrigger()
{
FirstRunDelay = TimeSpan.FromHours(1);
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
2015-07-28 12:33:30 +00:00
/// <param name="lastResult">The last result.</param>
2013-02-23 07:57:11 +00:00
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
2015-07-28 12:33:30 +00:00
public void Start(TaskResult lastResult, bool isApplicationStartup)
2013-02-21 01:33:05 +00:00
{
DisposeTimer();
2015-07-28 12:33:30 +00:00
var triggerDate = lastResult != null ?
2015-09-22 16:06:27 +00:00
lastResult.EndTimeUtc.Add(Interval) :
DateTime.UtcNow.Add(FirstRunDelay);
2015-07-28 12:33:30 +00:00
if (DateTime.UtcNow > triggerDate)
{
if (isApplicationStartup)
{
2015-09-21 15:18:14 +00:00
triggerDate = DateTime.UtcNow.AddMinutes(1);
2015-07-30 02:08:35 +00:00
}
else
{
2015-10-18 01:18:29 +00:00
triggerDate = DateTime.UtcNow.AddMinutes(1);
2015-07-28 12:33:30 +00:00
}
}
Timer = new Timer(state => OnTriggered(), null, triggerDate - DateTime.UtcNow, TimeSpan.FromMilliseconds(-1));
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Stops waiting for the trigger action
/// </summary>
2013-02-24 21:53:54 +00:00
public void Stop()
2013-02-21 01:33:05 +00:00
{
DisposeTimer();
}
/// <summary>
2013-02-24 21:53:54 +00:00
/// Disposes the timer.
2013-02-21 01:33:05 +00:00
/// </summary>
2013-02-24 21:53:54 +00:00
private void DisposeTimer()
2013-02-21 01:33:05 +00:00
{
2013-02-24 21:53:54 +00:00
if (Timer != null)
2013-02-21 01:33:05 +00:00
{
2013-02-24 21:53:54 +00:00
Timer.Dispose();
2013-02-21 01:33:05 +00:00
}
}
/// <summary>
2013-02-24 21:53:54 +00:00
/// Occurs when [triggered].
2013-02-21 01:33:05 +00:00
/// </summary>
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
2013-02-24 21:53:54 +00:00
/// <summary>
/// Called when [triggered].
/// </summary>
private void OnTriggered()
2013-02-21 01:33:05 +00:00
{
2013-02-24 21:53:54 +00:00
if (Triggered != null)
2013-02-21 01:33:05 +00:00
{
Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
2013-02-21 01:33:05 +00:00
}
}
}
}