| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Threading.Tasks; |
| | 4 | | using Jellyfin.Data.Events; |
| | 5 | | using MediaBrowser.Controller.Net; |
| | 6 | | using MediaBrowser.Model.Session; |
| | 7 | | using MediaBrowser.Model.Tasks; |
| | 8 | | using Microsoft.Extensions.Logging; |
| | 9 | |
|
| | 10 | | namespace Jellyfin.Api.WebSocketListeners; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Class ScheduledTasksWebSocketListener. |
| | 14 | | /// </summary> |
| | 15 | | public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<TaskInfo>, WebSocketListenerSta |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Gets or sets the task manager. |
| | 19 | | /// </summary> |
| | 20 | | /// <value>The task manager.</value> |
| | 21 | | private readonly ITaskManager _taskManager; |
| | 22 | |
|
| | 23 | | private bool _disposed; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Initializes a new instance of the <see cref="ScheduledTasksWebSocketListener"/> class. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="logger">Instance of the <see cref="ILogger{ScheduledTasksWebSocketListener}"/> interface.</param> |
| | 29 | | /// <param name="taskManager">Instance of the <see cref="ITaskManager"/> interface.</param> |
| | 30 | | public ScheduledTasksWebSocketListener(ILogger<ScheduledTasksWebSocketListener> logger, ITaskManager taskManager) |
| 19 | 31 | | : base(logger) |
| | 32 | | { |
| 19 | 33 | | _taskManager = taskManager; |
| | 34 | |
|
| 19 | 35 | | _taskManager.TaskExecuting += OnTaskExecuting; |
| 19 | 36 | | _taskManager.TaskCompleted += OnTaskCompleted; |
| 19 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <inheritdoc /> |
| 0 | 40 | | protected override SessionMessageType Type => SessionMessageType.ScheduledTasksInfo; |
| | 41 | |
|
| | 42 | | /// <inheritdoc /> |
| 0 | 43 | | protected override SessionMessageType StartType => SessionMessageType.ScheduledTasksInfoStart; |
| | 44 | |
|
| | 45 | | /// <inheritdoc /> |
| 0 | 46 | | protected override SessionMessageType StopType => SessionMessageType.ScheduledTasksInfoStop; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets the data to send. |
| | 50 | | /// </summary> |
| | 51 | | /// <returns>Task{IEnumerable{TaskInfo}}.</returns> |
| | 52 | | protected override Task<IEnumerable<TaskInfo>> GetDataToSend() |
| | 53 | | { |
| 0 | 54 | | return Task.FromResult(_taskManager.ScheduledTasks |
| 0 | 55 | | .OrderBy(i => i.Name) |
| 0 | 56 | | .Select(ScheduledTaskHelpers.GetTaskInfo) |
| 0 | 57 | | .Where(i => !i.IsHidden)); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | /// <inheritdoc /> |
| | 61 | | protected override async ValueTask DisposeAsyncCore() |
| | 62 | | { |
| | 63 | | if (!_disposed) |
| | 64 | | { |
| | 65 | | _taskManager.TaskExecuting -= OnTaskExecuting; |
| | 66 | | _taskManager.TaskCompleted -= OnTaskCompleted; |
| | 67 | | _disposed = true; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | await base.DisposeAsyncCore().ConfigureAwait(false); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | private void OnTaskCompleted(object? sender, TaskCompletionEventArgs e) |
| | 74 | | { |
| 8 | 75 | | e.Task.TaskProgress -= OnTaskProgress; |
| 8 | 76 | | SendData(true); |
| 8 | 77 | | } |
| | 78 | |
|
| | 79 | | private void OnTaskExecuting(object? sender, GenericEventArgs<IScheduledTaskWorker> e) |
| | 80 | | { |
| 19 | 81 | | SendData(true); |
| 19 | 82 | | e.Argument.TaskProgress += OnTaskProgress; |
| 19 | 83 | | } |
| | 84 | |
|
| | 85 | | private void OnTaskProgress(object? sender, GenericEventArgs<double> e) |
| | 86 | | { |
| 130 | 87 | | SendData(false); |
| 130 | 88 | | } |
| | 89 | | } |