jellyfin/Emby.Server.Implementations/ScheduledTasks/TaskManager.cs

287 lines
9.2 KiB
C#
Raw Normal View History

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