2020-05-29 09:28:19 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2016-10-29 05:40:15 +00:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-14 00:48:28 +00:00
|
|
|
using Jellyfin.Data.Events;
|
2017-08-16 06:43:41 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Model.Tasks;
|
2019-01-13 19:22:24 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2017-08-16 06:43:41 +00:00
|
|
|
namespace Emby.Server.Implementations.ScheduledTasks
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class TaskManager.
|
2016-10-29 05:40:15 +00:00
|
|
|
/// </summary>
|
|
|
|
public class TaskManager : ITaskManager
|
|
|
|
{
|
|
|
|
public event EventHandler<GenericEventArgs<IScheduledTaskWorker>> TaskExecuting;
|
2020-12-23 12:12:40 +00:00
|
|
|
|
2016-10-29 05:40:15 +00:00
|
|
|
public event EventHandler<TaskCompletionEventArgs> TaskCompleted;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Gets the list of Scheduled Tasks.
|
2016-10-29 05:40:15 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <value>The scheduled tasks.</value>
|
|
|
|
public IScheduledTaskWorker[] ScheduledTasks { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The _task queue.
|
2016-10-29 05:40:15 +00:00
|
|
|
/// </summary>
|
2018-09-12 17:26:21 +00:00
|
|
|
private readonly ConcurrentQueue<Tuple<Type, TaskOptions>> _taskQueue =
|
|
|
|
new ConcurrentQueue<Tuple<Type, TaskOptions>>();
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2019-10-25 10:47:20 +00:00
|
|
|
private readonly IApplicationPaths _applicationPaths;
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<TaskManager> _logger;
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="TaskManager" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="applicationPaths">The application paths.</param>
|
2020-04-05 01:00:11 +00:00
|
|
|
/// <param name="logger">The logger.</param>
|
2019-01-17 22:55:05 +00:00
|
|
|
public TaskManager(
|
|
|
|
IApplicationPaths applicationPaths,
|
2020-07-24 01:42:36 +00:00
|
|
|
ILogger<TaskManager> logger)
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
2019-10-25 10:47:20 +00:00
|
|
|
_applicationPaths = applicationPaths;
|
2020-04-05 01:00:11 +00:00
|
|
|
_logger = logger;
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2019-10-25 10:47:20 +00:00
|
|
|
ScheduledTasks = Array.Empty<IScheduledTaskWorker>();
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Cancels if running and queue.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="options">Task options.</param>
|
2018-09-12 17:26:21 +00:00
|
|
|
public void CancelIfRunningAndQueue<T>(TaskOptions options)
|
2019-10-25 10:47:20 +00:00
|
|
|
where T : IScheduledTask
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
((ScheduledTaskWorker)task).CancelIfRunning();
|
|
|
|
|
|
|
|
QueueScheduledTask<T>(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CancelIfRunningAndQueue<T>()
|
|
|
|
where T : IScheduledTask
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
CancelIfRunningAndQueue<T>(new TaskOptions());
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Cancels if running.
|
2016-10-29 05:40:15 +00:00
|
|
|
/// </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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2020-06-19 10:21:49 +00:00
|
|
|
/// <param name="options">Task options.</param>
|
2018-09-12 17:26:21 +00:00
|
|
|
public void QueueScheduledTask<T>(TaskOptions options)
|
2016-10-29 05:40:15 +00:00
|
|
|
where T : IScheduledTask
|
|
|
|
{
|
|
|
|
var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
|
|
|
if (scheduledTask == null)
|
|
|
|
{
|
2019-10-25 10:47:20 +00:00
|
|
|
_logger.LogError("Unable to find scheduled task of type {0} in QueueScheduledTask.", typeof(T).Name);
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QueueScheduledTask(scheduledTask, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void QueueScheduledTask<T>()
|
|
|
|
where T : IScheduledTask
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
QueueScheduledTask<T>(new TaskOptions());
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void QueueIfNotRunning<T>()
|
|
|
|
where T : IScheduledTask
|
|
|
|
{
|
|
|
|
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
|
|
|
if (task.State != TaskState.Running)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
QueueScheduledTask<T>(new TaskOptions());
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute<T>()
|
|
|
|
where T : IScheduledTask
|
|
|
|
{
|
|
|
|
var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == typeof(T));
|
|
|
|
|
|
|
|
if (scheduledTask == null)
|
|
|
|
{
|
2019-10-25 10:47:20 +00:00
|
|
|
_logger.LogError("Unable to find scheduled task of type {0} in Execute.", typeof(T).Name);
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var type = scheduledTask.ScheduledTask.GetType();
|
|
|
|
|
2020-11-18 13:23:45 +00:00
|
|
|
_logger.LogInformation("Queuing task {0}", type.Name);
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
lock (_taskQueue)
|
|
|
|
{
|
|
|
|
if (scheduledTask.State == TaskState.Idle)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
Execute(scheduledTask, new TaskOptions());
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
/// <param name="options">The task options.</param>
|
2018-09-12 17:26:21 +00:00
|
|
|
public void QueueScheduledTask(IScheduledTask task, TaskOptions options)
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == task.GetType());
|
|
|
|
|
|
|
|
if (scheduledTask == null)
|
|
|
|
{
|
2019-10-25 10:47:20 +00:00
|
|
|
_logger.LogError("Unable to find scheduled task of type {0} in QueueScheduledTask.", task.GetType().Name);
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QueueScheduledTask(scheduledTask, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Queues the scheduled task.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
/// <param name="options">The task options.</param>
|
2018-09-12 17:26:21 +00:00
|
|
|
private void QueueScheduledTask(IScheduledTaskWorker task, TaskOptions options)
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
var type = task.ScheduledTask.GetType();
|
|
|
|
|
2020-11-18 13:23:45 +00:00
|
|
|
_logger.LogInformation("Queuing task {0}", type.Name);
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
lock (_taskQueue)
|
|
|
|
{
|
2016-11-19 05:52:49 +00:00
|
|
|
if (task.State == TaskState.Idle)
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
Execute(task, options);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
_taskQueue.Enqueue(new Tuple<Type, TaskOptions>(type, options));
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds the tasks.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="tasks">The tasks.</param>
|
|
|
|
public void AddTasks(IEnumerable<IScheduledTask> tasks)
|
|
|
|
{
|
2020-12-23 12:12:40 +00:00
|
|
|
var list = tasks.Select(t => new ScheduledTaskWorker(t, _applicationPaths, this, _logger));
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2019-01-27 09:49:23 +00:00
|
|
|
ScheduledTasks = ScheduledTasks.Concat(list).ToArray();
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
2020-08-20 10:16:24 +00:00
|
|
|
GC.SuppressFinalize(this);
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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();
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public Task Execute(IScheduledTaskWorker task, TaskOptions options)
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
return ((ScheduledTaskWorker)task).Execute(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when [task executing].
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
internal void OnTaskExecuting(IScheduledTaskWorker task)
|
|
|
|
{
|
2020-04-05 16:10:56 +00:00
|
|
|
TaskExecuting?.Invoke(this, new GenericEventArgs<IScheduledTaskWorker>(task));
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when [task completed].
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="task">The task.</param>
|
|
|
|
/// <param name="result">The result.</param>
|
|
|
|
internal void OnTaskCompleted(IScheduledTaskWorker task, TaskResult result)
|
|
|
|
{
|
2020-04-05 16:10:56 +00:00
|
|
|
TaskCompleted?.Invoke(task, new TaskCompletionEventArgs(task, result));
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
ExecuteQueuedTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Executes the queued tasks.
|
|
|
|
/// </summary>
|
|
|
|
private void ExecuteQueuedTasks()
|
|
|
|
{
|
2019-10-25 10:47:20 +00:00
|
|
|
_logger.LogInformation("ExecuteQueuedTasks");
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
// Execute queued tasks
|
|
|
|
lock (_taskQueue)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var list = new List<Tuple<Type, TaskOptions>>();
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2019-01-13 20:46:33 +00:00
|
|
|
while (_taskQueue.TryDequeue(out var item))
|
2016-10-29 05:40:15 +00: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);
|
|
|
|
|
|
|
|
if (scheduledTask.State == TaskState.Idle)
|
|
|
|
{
|
|
|
|
Execute(scheduledTask, enqueuedType.Item2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|