2013-09-23 20:53:06 +02:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-03-07 06:34:00 +01:00
|
|
|
|
using MediaBrowser.Common.Events;
|
2013-02-24 22:53:54 +01:00
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
2014-05-08 22:09:53 +02:00
|
|
|
|
using MediaBrowser.Model.Events;
|
2013-02-24 22:53:54 +01:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using MediaBrowser.Model.Tasks;
|
|
|
|
|
using System;
|
2015-03-27 05:17:04 +01:00
|
|
|
|
using System.Collections.Concurrent;
|
2013-02-24 22:53:54 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-09-23 20:53:06 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 06:23:11 +02:00
|
|
|
|
using CommonIO;
|
2015-10-25 16:48:44 +01:00
|
|
|
|
using Microsoft.Win32;
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class TaskManager
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class TaskManager : ITaskManager
|
|
|
|
|
{
|
2014-05-10 19:28:03 +02:00
|
|
|
|
public event EventHandler<GenericEventArgs<IScheduledTaskWorker>> TaskExecuting;
|
|
|
|
|
public event EventHandler<TaskCompletionEventArgs> TaskCompleted;
|
2013-03-07 06:34:00 +01:00
|
|
|
|
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of Scheduled Tasks
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The scheduled tasks.</value>
|
2013-02-26 04:43:04 +01:00
|
|
|
|
public IScheduledTaskWorker[] ScheduledTasks { get; private set; }
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _task queue
|
|
|
|
|
/// </summary>
|
2015-03-27 05:17:04 +01:00
|
|
|
|
private readonly ConcurrentQueue<Tuple<Type, TaskExecutionOptions>> _taskQueue =
|
|
|
|
|
new ConcurrentQueue<Tuple<Type, TaskExecutionOptions>>();
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-26 04:43:04 +01:00
|
|
|
|
/// Gets or sets the json serializer.
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// </summary>
|
2013-02-26 04:43:04 +01:00
|
|
|
|
/// <value>The json serializer.</value>
|
|
|
|
|
private IJsonSerializer JsonSerializer { get; set; }
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-26 04:43:04 +01:00
|
|
|
|
/// Gets or sets the application paths.
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// </summary>
|
2013-02-26 04:43:04 +01:00
|
|
|
|
/// <value>The application paths.</value>
|
|
|
|
|
private IApplicationPaths ApplicationPaths { get; set; }
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-26 04:43:04 +01:00
|
|
|
|
/// Gets the logger.
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// </summary>
|
2013-02-26 04:43:04 +01:00
|
|
|
|
/// <value>The logger.</value>
|
|
|
|
|
private ILogger Logger { get; set; }
|
2015-09-14 01:07:54 +02:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
2016-02-03 21:52:45 +01:00
|
|
|
|
private bool _suspendTriggers;
|
|
|
|
|
|
|
|
|
|
public bool SuspendTriggers
|
|
|
|
|
{
|
|
|
|
|
get { return _suspendTriggers; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Setting SuspendTriggers to {0}", value);
|
|
|
|
|
var executeQueued = _suspendTriggers && !value;
|
|
|
|
|
|
|
|
|
|
_suspendTriggers = value;
|
|
|
|
|
|
|
|
|
|
if (executeQueued)
|
|
|
|
|
{
|
|
|
|
|
ExecuteQueuedTasks();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="TaskManager" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="applicationPaths">The application paths.</param>
|
|
|
|
|
/// <param name="jsonSerializer">The json serializer.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
/// <exception cref="System.ArgumentException">kernel</exception>
|
2015-09-14 01:07:54 +02:00
|
|
|
|
public TaskManager(IApplicationPaths applicationPaths, IJsonSerializer jsonSerializer, ILogger logger, IFileSystem fileSystem)
|
2013-02-24 22:53:54 +01:00
|
|
|
|
{
|
2013-02-26 04:43:04 +01:00
|
|
|
|
ApplicationPaths = applicationPaths;
|
|
|
|
|
JsonSerializer = jsonSerializer;
|
|
|
|
|
Logger = logger;
|
2015-09-14 01:07:54 +02:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
2013-02-26 04:43:04 +01:00
|
|
|
|
ScheduledTasks = new IScheduledTaskWorker[] { };
|
2015-10-25 16:48:44 +01:00
|
|
|
|
|
|
|
|
|
BindToSystemEvent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BindToSystemEvent()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
foreach (var task in ScheduledTasks)
|
|
|
|
|
{
|
|
|
|
|
task.ReloadTriggerEvents();
|
|
|
|
|
}
|
2013-02-24 22:53:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Cancels if running and queue.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2015-02-25 21:55:01 +01:00
|
|
|
|
/// <param name="options">Task options.</param>
|
|
|
|
|
public void CancelIfRunningAndQueue<T>(TaskExecutionOptions options)
|
2013-02-24 22:53:54 +01:00
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
2013-03-07 06:34:00 +01:00
|
|
|
|
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
((ScheduledTaskWorker)task).CancelIfRunning();
|
|
|
|
|
|
2015-02-25 21:55:01 +01:00
|
|
|
|
QueueScheduledTask<T>(options);
|
2013-02-24 22:53:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-07 18:19:44 +01:00
|
|
|
|
public void CancelIfRunningAndQueue<T>()
|
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
|
|
|
|
CancelIfRunningAndQueue<T>(new TaskExecutionOptions());
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-23 20:53:06 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Cancels if running
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public void CancelIfRunning<T>()
|
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
|
|
|
|
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
((ScheduledTaskWorker)task).CancelIfRunning();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2015-02-25 21:55:01 +01:00
|
|
|
|
/// <param name="options">Task options</param>
|
|
|
|
|
public void QueueScheduledTask<T>(TaskExecutionOptions options)
|
2013-02-24 22:53:54 +01:00
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
2015-09-04 03:34:57 +02:00
|
|
|
|
var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
|
|
|
|
|
if (scheduledTask == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Unable to find scheduled task of type {0} in QueueScheduledTask.", typeof(T).Name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QueueScheduledTask(scheduledTask, options);
|
|
|
|
|
}
|
2013-02-24 22:53:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-07 18:19:44 +01:00
|
|
|
|
public void QueueScheduledTask<T>()
|
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
|
|
|
|
QueueScheduledTask<T>(new TaskExecutionOptions());
|
|
|
|
|
}
|
2015-10-25 16:48:44 +01:00
|
|
|
|
|
2016-03-21 04:04:44 +01:00
|
|
|
|
public void QueueIfNotRunning<T>()
|
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
|
|
|
|
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
|
|
|
|
|
if (task.State != TaskState.Running)
|
|
|
|
|
{
|
|
|
|
|
QueueScheduledTask<T>(new TaskExecutionOptions());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-03 21:52:45 +01:00
|
|
|
|
public void Execute<T>()
|
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
|
|
|
|
var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
|
|
|
|
|
if (scheduledTask == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Unable to find scheduled task of type {0} in Execute.", typeof(T).Name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var type = scheduledTask.ScheduledTask.GetType();
|
|
|
|
|
|
|
|
|
|
Logger.Info("Queueing task {0}", type.Name);
|
|
|
|
|
|
|
|
|
|
lock (_taskQueue)
|
|
|
|
|
{
|
|
|
|
|
if (scheduledTask.State == TaskState.Idle)
|
|
|
|
|
{
|
|
|
|
|
Execute(scheduledTask, new TaskExecutionOptions());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 22:53:54 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="task">The task.</param>
|
2015-02-25 21:55:01 +01:00
|
|
|
|
/// <param name="options">The task options.</param>
|
|
|
|
|
public void QueueScheduledTask(IScheduledTask task, TaskExecutionOptions options)
|
2013-02-24 22:53:54 +01:00
|
|
|
|
{
|
2015-09-04 03:34:57 +02:00
|
|
|
|
var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == task.GetType());
|
|
|
|
|
|
|
|
|
|
if (scheduledTask == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Unable to find scheduled task of type {0} in QueueScheduledTask.", task.GetType().Name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QueueScheduledTask(scheduledTask, options);
|
|
|
|
|
}
|
2013-02-26 04:43:04 +01:00
|
|
|
|
}
|
2013-03-07 06:34:00 +01:00
|
|
|
|
|
2013-02-26 04:43:04 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="task">The task.</param>
|
2015-02-25 21:55:01 +01:00
|
|
|
|
/// <param name="options">The task options.</param>
|
|
|
|
|
private void QueueScheduledTask(IScheduledTaskWorker task, TaskExecutionOptions options)
|
2013-02-26 04:43:04 +01:00
|
|
|
|
{
|
2013-03-13 19:36:27 +01:00
|
|
|
|
var type = task.ScheduledTask.GetType();
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
2015-03-27 05:17:04 +01:00
|
|
|
|
Logger.Info("Queueing task {0}", type.Name);
|
|
|
|
|
|
2013-02-24 22:53:54 +01:00
|
|
|
|
lock (_taskQueue)
|
|
|
|
|
{
|
2016-02-03 21:52:45 +01:00
|
|
|
|
if (task.State == TaskState.Idle && !SuspendTriggers)
|
2013-02-24 22:53:54 +01:00
|
|
|
|
{
|
2015-02-25 21:55:01 +01:00
|
|
|
|
Execute(task, options);
|
2013-02-24 22:53:54 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 05:17:04 +01:00
|
|
|
|
_taskQueue.Enqueue(new Tuple<Type, TaskExecutionOptions>(type, options));
|
2013-02-24 22:53:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the tasks.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tasks">The tasks.</param>
|
|
|
|
|
public void AddTasks(IEnumerable<IScheduledTask> tasks)
|
|
|
|
|
{
|
|
|
|
|
var myTasks = ScheduledTasks.ToList();
|
|
|
|
|
|
2014-05-10 19:28:03 +02:00
|
|
|
|
var list = tasks.ToList();
|
2015-09-14 01:07:54 +02:00
|
|
|
|
myTasks.AddRange(list.Select(t => new ScheduledTaskWorker(t, ApplicationPaths, this, JsonSerializer, Logger, _fileSystem)));
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
|
|
|
|
ScheduledTasks = myTasks.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
|
{
|
|
|
|
|
foreach (var task in ScheduledTasks)
|
|
|
|
|
{
|
|
|
|
|
task.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-07 06:34:00 +01:00
|
|
|
|
|
|
|
|
|
public void Cancel(IScheduledTaskWorker task)
|
|
|
|
|
{
|
|
|
|
|
((ScheduledTaskWorker)task).Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-25 21:55:01 +01:00
|
|
|
|
public Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options)
|
2013-03-07 06:34:00 +01:00
|
|
|
|
{
|
2015-02-25 21:55:01 +01:00
|
|
|
|
return ((ScheduledTaskWorker)task).Execute(options);
|
2013-03-07 06:34:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 19:36:27 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [task executing].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="task">The task.</param>
|
2014-05-10 19:28:03 +02:00
|
|
|
|
internal void OnTaskExecuting(IScheduledTaskWorker task)
|
2013-03-07 06:34:00 +01:00
|
|
|
|
{
|
2014-05-14 02:46:45 +02:00
|
|
|
|
EventHelper.FireEventIfNotNull(TaskExecuting, this, new GenericEventArgs<IScheduledTaskWorker>
|
2014-05-10 19:28:03 +02:00
|
|
|
|
{
|
|
|
|
|
Argument = task
|
|
|
|
|
|
|
|
|
|
}, Logger);
|
2013-03-07 06:34:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 19:36:27 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [task completed].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
|
/// <param name="result">The result.</param>
|
2014-05-10 19:28:03 +02:00
|
|
|
|
internal void OnTaskCompleted(IScheduledTaskWorker task, TaskResult result)
|
2013-03-07 06:34:00 +01:00
|
|
|
|
{
|
2014-05-14 02:46:45 +02:00
|
|
|
|
EventHelper.FireEventIfNotNull(TaskCompleted, task, new TaskCompletionEventArgs
|
2014-05-10 19:28:03 +02:00
|
|
|
|
{
|
|
|
|
|
Result = result,
|
|
|
|
|
Task = task
|
|
|
|
|
|
|
|
|
|
}, Logger);
|
|
|
|
|
|
2013-03-13 19:36:27 +01:00
|
|
|
|
ExecuteQueuedTasks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes the queued tasks.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ExecuteQueuedTasks()
|
|
|
|
|
{
|
2016-02-03 21:52:45 +01:00
|
|
|
|
if (SuspendTriggers)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Info("ExecuteQueuedTasks");
|
|
|
|
|
|
2013-03-13 19:36:27 +01:00
|
|
|
|
// Execute queued tasks
|
|
|
|
|
lock (_taskQueue)
|
|
|
|
|
{
|
2015-03-27 05:17:04 +01:00
|
|
|
|
var list = new List<Tuple<Type, TaskExecutionOptions>>();
|
|
|
|
|
|
|
|
|
|
Tuple<Type, TaskExecutionOptions> item;
|
|
|
|
|
while (_taskQueue.TryDequeue(out item))
|
2013-03-13 19:36:27 +01:00
|
|
|
|
{
|
2015-03-27 05:17:04 +01:00
|
|
|
|
if (list.All(i => i.Item1 != item.Item1))
|
|
|
|
|
{
|
|
|
|
|
list.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var enqueuedType in list)
|
|
|
|
|
{
|
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == enqueuedType.Item1);
|
2013-03-13 19:36:27 +01:00
|
|
|
|
|
|
|
|
|
if (scheduledTask.State == TaskState.Idle)
|
|
|
|
|
{
|
2015-03-27 05:17:04 +01:00
|
|
|
|
Execute(scheduledTask, enqueuedType.Item2);
|
2013-03-13 19:36:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-07 06:34:00 +01:00
|
|
|
|
}
|
2013-02-24 22:53:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|