You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Common.Impleme.../ScheduledTasks/TaskManager.cs

230 lines
7.4 KiB

11 years ago
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Events;
12 years ago
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;
11 years ago
using System.Threading.Tasks;
12 years ago
namespace MediaBrowser.Common.Implementations.ScheduledTasks
{
/// <summary>
/// Class TaskManager
/// </summary>
public class TaskManager : ITaskManager
{
public event EventHandler<EventArgs> TaskExecuting;
public event EventHandler<GenericEventArgs<TaskResult>> TaskCompleted;
12 years ago
/// <summary>
/// Gets the list of Scheduled Tasks
/// </summary>
/// <value>The scheduled tasks.</value>
public IScheduledTaskWorker[] ScheduledTasks { get; private set; }
12 years ago
/// <summary>
/// The _task queue
/// </summary>
private readonly List<Type> _taskQueue = new List<Type>();
/// <summary>
/// Gets or sets the json serializer.
12 years ago
/// </summary>
/// <value>The json serializer.</value>
private IJsonSerializer JsonSerializer { get; set; }
12 years ago
/// <summary>
/// Gets or sets the application paths.
12 years ago
/// </summary>
/// <value>The application paths.</value>
private IApplicationPaths ApplicationPaths { get; set; }
12 years ago
/// <summary>
/// Gets the logger.
12 years ago
/// </summary>
/// <value>The logger.</value>
private ILogger Logger { get; set; }
12 years ago
/// <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>
public TaskManager(IApplicationPaths applicationPaths, IJsonSerializer jsonSerializer, ILogger logger)
12 years ago
{
ApplicationPaths = applicationPaths;
JsonSerializer = jsonSerializer;
Logger = logger;
12 years ago
ScheduledTasks = new IScheduledTaskWorker[] { };
12 years ago
}
/// <summary>
/// Cancels if running and queue.
/// </summary>
/// <typeparam name="T"></typeparam>
public void CancelIfRunningAndQueue<T>()
where T : IScheduledTask
{
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
((ScheduledTaskWorker)task).CancelIfRunning();
12 years ago
QueueScheduledTask<T>();
}
11 years ago
/// <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();
}
12 years ago
/// <summary>
/// Queues the scheduled task.
/// </summary>
/// <typeparam name="T"></typeparam>
public void QueueScheduledTask<T>()
where T : IScheduledTask
{
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
12 years ago
QueueScheduledTask(scheduledTask);
}
/// <summary>
/// Queues the scheduled task.
/// </summary>
/// <param name="task">The task.</param>
public void QueueScheduledTask(IScheduledTask task)
{
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType());
12 years ago
QueueScheduledTask(scheduledTask);
}
/// <summary>
/// Queues the scheduled task.
/// </summary>
/// <param name="task">The task.</param>
private void QueueScheduledTask(IScheduledTaskWorker task)
{
var type = task.ScheduledTask.GetType();
12 years ago
lock (_taskQueue)
{
// If it's idle just execute immediately
if (task.State == TaskState.Idle)
12 years ago
{
((ScheduledTaskWorker)task).Execute();
12 years ago
return;
}
if (!_taskQueue.Contains(type))
{
Logger.Info("Queueing task {0}", type.Name);
12 years ago
_taskQueue.Add(type);
}
else
{
Logger.Info("Task already queued: {0}", type.Name);
12 years ago
}
}
}
/// <summary>
/// Adds the tasks.
/// </summary>
/// <param name="tasks">The tasks.</param>
public void AddTasks(IEnumerable<IScheduledTask> tasks)
{
var myTasks = ScheduledTasks.ToList();
myTasks.AddRange(tasks.Select(t => new ScheduledTaskWorker(t, ApplicationPaths, this, JsonSerializer, Logger)));
12 years ago
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();
}
}
public void Cancel(IScheduledTaskWorker task)
{
((ScheduledTaskWorker)task).Cancel();
}
public Task Execute(IScheduledTaskWorker task)
{
return ((ScheduledTaskWorker)task).Execute();
}
/// <summary>
/// Called when [task executing].
/// </summary>
/// <param name="task">The task.</param>
internal void OnTaskExecuting(IScheduledTask task)
{
EventHelper.QueueEventIfNotNull(TaskExecuting, task, EventArgs.Empty, Logger);
}
/// <summary>
/// Called when [task completed].
/// </summary>
/// <param name="task">The task.</param>
/// <param name="result">The result.</param>
internal void OnTaskCompleted(IScheduledTask task, TaskResult result)
{
EventHelper.QueueEventIfNotNull(TaskCompleted, task, new GenericEventArgs<TaskResult> { Argument = result }, Logger);
ExecuteQueuedTasks();
}
/// <summary>
/// Executes the queued tasks.
/// </summary>
private void ExecuteQueuedTasks()
{
// Execute queued tasks
lock (_taskQueue)
{
foreach (var type in _taskQueue.ToList())
{
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == type);
if (scheduledTask.State == TaskState.Idle)
{
((ScheduledTaskWorker)scheduledTask).Execute();
_taskQueue.Remove(type);
}
}
}
}
12 years ago
}
}