using MediaBrowser.Common.Kernel;
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
{
///
/// Class TaskManager
///
public class TaskManager : ITaskManager
{
///
/// Gets the list of Scheduled Tasks
///
/// The scheduled tasks.
public IScheduledTaskWorker[] ScheduledTasks { get; private set; }
///
/// The _task queue
///
private readonly List _taskQueue = new List();
///
/// Gets or sets the json serializer.
///
/// The json serializer.
private IJsonSerializer JsonSerializer { get; set; }
///
/// Gets or sets the application paths.
///
/// The application paths.
private IApplicationPaths ApplicationPaths { get; set; }
///
/// Gets the logger.
///
/// The logger.
private ILogger Logger { get; set; }
///
/// Gets or sets the server manager.
///
/// The server manager.
private IServerManager ServerManager { get; set; }
///
/// Initializes a new instance of the class.
///
/// The application paths.
/// The json serializer.
/// The logger.
/// The server manager.
/// kernel
public TaskManager(IApplicationPaths applicationPaths, IJsonSerializer jsonSerializer, ILogger logger, IServerManager serverManager)
{
ApplicationPaths = applicationPaths;
JsonSerializer = jsonSerializer;
Logger = logger;
ServerManager = serverManager;
ScheduledTasks = new IScheduledTaskWorker[] { };
}
///
/// Cancels if running and queue.
///
///
public void CancelIfRunningAndQueue()
where T : IScheduledTask
{
ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)).CancelIfRunning();
QueueScheduledTask();
}
///
/// Queues the scheduled task.
///
///
public void QueueScheduledTask()
where T : IScheduledTask
{
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
QueueScheduledTask(scheduledTask);
}
///
/// Queues the scheduled task.
///
/// The task.
public void QueueScheduledTask(IScheduledTask task)
{
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType());
QueueScheduledTask(scheduledTask);
}
///
/// Queues the scheduled task.
///
/// The task.
private void QueueScheduledTask(IScheduledTaskWorker task)
{
var type = task.GetType();
lock (_taskQueue)
{
// If it's idle just execute immediately
if (task.State == TaskState.Idle)
{
task.Execute();
return;
}
if (!_taskQueue.Contains(type))
{
Logger.Info("Queueing task {0}", type.Name);
_taskQueue.Add(type);
}
else
{
Logger.Info("Task already queued: {0}", type.Name);
}
}
}
///
/// Called when [task completed].
///
/// The task.
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);
}
}
}
}
///
/// Adds the tasks.
///
/// The tasks.
public void AddTasks(IEnumerable tasks)
{
var myTasks = ScheduledTasks.ToList();
myTasks.AddRange(tasks.Select(t => new ScheduledTaskWorker(t, ApplicationPaths, this, JsonSerializer, Logger, ServerManager)));
ScheduledTasks = myTasks.ToArray();
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool dispose)
{
foreach (var task in ScheduledTasks)
{
task.Dispose();
}
}
}
}