|
|
@ -29,7 +29,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// The _task queue
|
|
|
|
/// The _task queue
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
private readonly List<Type> _taskQueue = new List<Type>();
|
|
|
|
private readonly SortedDictionary<Type, TaskExecutionOptions> _taskQueue = new SortedDictionary<Type, TaskExecutionOptions>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the json serializer.
|
|
|
|
/// Gets or sets the json serializer.
|
|
|
@ -69,13 +69,14 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|
|
|
/// Cancels if running and queue.
|
|
|
|
/// Cancels if running and queue.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
public void CancelIfRunningAndQueue<T>()
|
|
|
|
/// <param name="options">Task options.</param>
|
|
|
|
|
|
|
|
public void CancelIfRunningAndQueue<T>(TaskExecutionOptions options)
|
|
|
|
where T : IScheduledTask
|
|
|
|
where T : IScheduledTask
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
((ScheduledTaskWorker)task).CancelIfRunning();
|
|
|
|
((ScheduledTaskWorker)task).CancelIfRunning();
|
|
|
|
|
|
|
|
|
|
|
|
QueueScheduledTask<T>();
|
|
|
|
QueueScheduledTask<T>(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
@ -93,30 +94,33 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
public void QueueScheduledTask<T>()
|
|
|
|
/// <param name="options">Task options</param>
|
|
|
|
|
|
|
|
public void QueueScheduledTask<T>(TaskExecutionOptions options)
|
|
|
|
where T : IScheduledTask
|
|
|
|
where T : IScheduledTask
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
|
|
|
|
|
|
|
QueueScheduledTask(scheduledTask);
|
|
|
|
QueueScheduledTask(scheduledTask, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
public void QueueScheduledTask(IScheduledTask task)
|
|
|
|
/// <param name="options">The task options.</param>
|
|
|
|
|
|
|
|
public void QueueScheduledTask(IScheduledTask task, TaskExecutionOptions options)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType());
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType());
|
|
|
|
|
|
|
|
|
|
|
|
QueueScheduledTask(scheduledTask);
|
|
|
|
QueueScheduledTask(scheduledTask, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
private void QueueScheduledTask(IScheduledTaskWorker task)
|
|
|
|
/// <param name="options">The task options.</param>
|
|
|
|
|
|
|
|
private void QueueScheduledTask(IScheduledTaskWorker task, TaskExecutionOptions options)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var type = task.ScheduledTask.GetType();
|
|
|
|
var type = task.ScheduledTask.GetType();
|
|
|
|
|
|
|
|
|
|
|
@ -125,17 +129,18 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|
|
|
// If it's idle just execute immediately
|
|
|
|
// If it's idle just execute immediately
|
|
|
|
if (task.State == TaskState.Idle)
|
|
|
|
if (task.State == TaskState.Idle)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Execute(task);
|
|
|
|
Execute(task, options);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!_taskQueue.Contains(type))
|
|
|
|
if (!_taskQueue.ContainsKey(type))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Logger.Info("Queueing task {0}", type.Name);
|
|
|
|
Logger.Info("Queueing task {0}", type.Name);
|
|
|
|
_taskQueue.Add(type);
|
|
|
|
_taskQueue.Add(type, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
_taskQueue[type] = options;
|
|
|
|
Logger.Info("Task already queued: {0}", type.Name);
|
|
|
|
Logger.Info("Task already queued: {0}", type.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -181,9 +186,9 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|
|
|
((ScheduledTaskWorker)task).Cancel();
|
|
|
|
((ScheduledTaskWorker)task).Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task Execute(IScheduledTaskWorker task)
|
|
|
|
public Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return ((ScheduledTaskWorker)task).Execute();
|
|
|
|
return ((ScheduledTaskWorker)task).Execute(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
@ -224,15 +229,15 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|
|
|
// Execute queued tasks
|
|
|
|
// Execute queued tasks
|
|
|
|
lock (_taskQueue)
|
|
|
|
lock (_taskQueue)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach (var type in _taskQueue.ToList())
|
|
|
|
foreach (var enqueuedType in _taskQueue.ToList())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == type);
|
|
|
|
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == enqueuedType.Key);
|
|
|
|
|
|
|
|
|
|
|
|
if (scheduledTask.State == TaskState.Idle)
|
|
|
|
if (scheduledTask.State == TaskState.Idle)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Execute(scheduledTask);
|
|
|
|
Execute(scheduledTask, enqueuedType.Value);
|
|
|
|
|
|
|
|
|
|
|
|
_taskQueue.Remove(type);
|
|
|
|
_taskQueue.Remove(enqueuedType.Key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|