2013-03-04 05:43:06 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Kernel;
|
2013-02-24 21:53:54 +00:00
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using MediaBrowser.Model.Tasks;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class TaskManager
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class TaskManager : ITaskManager
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of Scheduled Tasks
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The scheduled tasks.</value>
|
2013-02-26 03:43:04 +00:00
|
|
|
|
public IScheduledTaskWorker[] ScheduledTasks { get; private set; }
|
2013-02-24 21:53:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _task queue
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly List<Type> _taskQueue = new List<Type>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-26 03:43:04 +00:00
|
|
|
|
/// Gets or sets the json serializer.
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// </summary>
|
2013-02-26 03:43:04 +00:00
|
|
|
|
/// <value>The json serializer.</value>
|
|
|
|
|
private IJsonSerializer JsonSerializer { get; set; }
|
2013-02-24 21:53:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-26 03:43:04 +00:00
|
|
|
|
/// Gets or sets the application paths.
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// </summary>
|
2013-02-26 03:43:04 +00:00
|
|
|
|
/// <value>The application paths.</value>
|
|
|
|
|
private IApplicationPaths ApplicationPaths { get; set; }
|
2013-02-24 21:53:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-26 03:43:04 +00:00
|
|
|
|
/// Gets the logger.
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// </summary>
|
2013-02-26 03:43:04 +00:00
|
|
|
|
/// <value>The logger.</value>
|
|
|
|
|
private ILogger Logger { get; set; }
|
2013-02-24 21:53:54 +00:00
|
|
|
|
|
2013-02-26 16:10:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the server manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The server manager.</value>
|
|
|
|
|
private IServerManager ServerManager { get; set; }
|
|
|
|
|
|
2013-02-24 21:53:54 +00: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>
|
2013-02-26 16:10:55 +00:00
|
|
|
|
/// <param name="serverManager">The server manager.</param>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// <exception cref="System.ArgumentException">kernel</exception>
|
2013-02-26 16:10:55 +00:00
|
|
|
|
public TaskManager(IApplicationPaths applicationPaths, IJsonSerializer jsonSerializer, ILogger logger, IServerManager serverManager)
|
2013-02-24 21:53:54 +00:00
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
|
ApplicationPaths = applicationPaths;
|
|
|
|
|
JsonSerializer = jsonSerializer;
|
|
|
|
|
Logger = logger;
|
2013-02-26 16:10:55 +00:00
|
|
|
|
ServerManager = serverManager;
|
2013-02-24 21:53:54 +00:00
|
|
|
|
|
2013-02-26 03:43:04 +00:00
|
|
|
|
ScheduledTasks = new IScheduledTaskWorker[] { };
|
2013-02-24 21:53:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Cancels if running and queue.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public void CancelIfRunningAndQueue<T>()
|
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
|
ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)).CancelIfRunning();
|
2013-02-24 21:53:54 +00:00
|
|
|
|
QueueScheduledTask<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public void QueueScheduledTask<T>()
|
|
|
|
|
where T : IScheduledTask
|
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
2013-02-24 21:53:54 +00:00
|
|
|
|
|
|
|
|
|
QueueScheduledTask(scheduledTask);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
|
public void QueueScheduledTask(IScheduledTask task)
|
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType());
|
2013-02-24 21:53:54 +00:00
|
|
|
|
|
2013-02-26 03:43:04 +00:00
|
|
|
|
QueueScheduledTask(scheduledTask);
|
|
|
|
|
}
|
2013-03-01 21:22:34 +00:00
|
|
|
|
|
2013-02-26 03:43:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
|
private void QueueScheduledTask(IScheduledTaskWorker task)
|
|
|
|
|
{
|
|
|
|
|
var type = task.GetType();
|
2013-02-24 21:53:54 +00:00
|
|
|
|
|
|
|
|
|
lock (_taskQueue)
|
|
|
|
|
{
|
|
|
|
|
// If it's idle just execute immediately
|
2013-02-26 03:43:04 +00:00
|
|
|
|
if (task.State == TaskState.Idle)
|
2013-02-24 21:53:54 +00:00
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
|
task.Execute();
|
2013-02-24 21:53:54 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_taskQueue.Contains(type))
|
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
|
Logger.Info("Queueing task {0}", type.Name);
|
2013-02-24 21:53:54 +00:00
|
|
|
|
_taskQueue.Add(type);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
|
Logger.Info("Task already queued: {0}", type.Name);
|
2013-02-24 21:53:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [task completed].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
|
public void OnTaskCompleted(IScheduledTask task)
|
|
|
|
|
{
|
|
|
|
|
// Execute queued tasks
|
|
|
|
|
lock (_taskQueue)
|
|
|
|
|
{
|
|
|
|
|
var copy = _taskQueue.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var type in copy)
|
|
|
|
|
{
|
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.GetType() == type);
|
|
|
|
|
|
|
|
|
|
if (scheduledTask.State == TaskState.Idle)
|
|
|
|
|
{
|
|
|
|
|
scheduledTask.Execute();
|
|
|
|
|
|
|
|
|
|
_taskQueue.Remove(type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the tasks.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tasks">The tasks.</param>
|
|
|
|
|
public void AddTasks(IEnumerable<IScheduledTask> tasks)
|
|
|
|
|
{
|
|
|
|
|
var myTasks = ScheduledTasks.ToList();
|
|
|
|
|
|
2013-02-26 16:10:55 +00:00
|
|
|
|
myTasks.AddRange(tasks.Select(t => new ScheduledTaskWorker(t, ApplicationPaths, this, JsonSerializer, Logger, ServerManager)));
|
2013-02-24 21:53:54 +00: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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|