jellyfin-server/Emby.Server.Implementations/ScheduledTasks/SystemEventTrigger.cs

84 lines
2.4 KiB
C#
Raw Normal View History

2016-10-29 05:40:15 +00:00
using System;
using System.Threading.Tasks;
using MediaBrowser.Model.Events;
using Microsoft.Extensions.Logging;
2016-10-29 05:40:15 +00:00
using MediaBrowser.Model.System;
using MediaBrowser.Model.Tasks;
namespace Emby.Server.Implementations.ScheduledTasks
2016-10-29 05:40:15 +00:00
{
/// <summary>
/// Class SystemEventTrigger
/// </summary>
public class SystemEventTrigger : ITaskTrigger
{
/// <summary>
/// Gets or sets the system event.
/// </summary>
/// <value>The system event.</value>
public SystemEvent SystemEvent { get; set; }
/// <summary>
2018-09-12 17:26:21 +00:00
/// Gets or sets the options of this task.
2016-10-29 05:40:15 +00:00
/// </summary>
2018-09-12 17:26:21 +00:00
public TaskOptions TaskOptions { get; set; }
2016-10-29 05:40:15 +00:00
private readonly ISystemEvents _systemEvents;
public SystemEventTrigger(ISystemEvents systemEvents)
{
_systemEvents = systemEvents;
}
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
/// <param name="lastResult">The last result.</param>
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
public void Start(TaskResult lastResult, ILogger logger, string taskName, bool isApplicationStartup)
{
switch (SystemEvent)
{
case SystemEvent.WakeFromSleep:
_systemEvents.Resume += _systemEvents_Resume;
break;
}
}
private async void _systemEvents_Resume(object sender, EventArgs e)
{
if (SystemEvent == SystemEvent.WakeFromSleep)
{
// This value is a bit arbitrary, but add a delay to help ensure network connections have been restored before running the task
await Task.Delay(10000).ConfigureAwait(false);
OnTriggered();
}
}
/// <summary>
/// Stops waiting for the trigger action
/// </summary>
public void Stop()
{
_systemEvents.Resume -= _systemEvents_Resume;
}
/// <summary>
/// Occurs when [triggered].
/// </summary>
2018-09-12 17:26:21 +00:00
public event EventHandler<EventArgs> Triggered;
2016-10-29 05:40:15 +00:00
/// <summary>
/// Called when [triggered].
/// </summary>
private void OnTriggered()
{
if (Triggered != null)
{
2018-09-12 17:26:21 +00:00
Triggered(this, EventArgs.Empty);
2016-10-29 05:40:15 +00:00
}
}
}
}