2019-01-13 15:01:16 -05:00
|
|
|
using System.Collections.Generic;
|
2013-02-20 20:33:05 -05:00
|
|
|
using System.Linq;
|
2017-11-23 10:46:16 -05:00
|
|
|
using System.Threading.Tasks;
|
2020-08-13 20:48:28 -04:00
|
|
|
using Jellyfin.Data.Events;
|
2019-01-13 14:24:58 -05:00
|
|
|
using MediaBrowser.Controller.Net;
|
2020-09-25 03:25:59 -04:00
|
|
|
using MediaBrowser.Model.Session;
|
2019-01-13 14:24:58 -05:00
|
|
|
using MediaBrowser.Model.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
namespace Jellyfin.Api.WebSocketListeners;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class ScheduledTasksWebSocketListener.
|
|
|
|
/// </summary>
|
|
|
|
public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<TaskInfo>, WebSocketListenerState>
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
|
|
|
/// <summary>
|
2023-01-31 06:18:10 -05:00
|
|
|
/// Gets or sets the task manager.
|
2013-02-20 20:33:05 -05:00
|
|
|
/// </summary>
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <value>The task manager.</value>
|
|
|
|
private readonly ITaskManager _taskManager;
|
2013-02-23 02:57:11 -05:00
|
|
|
|
2024-03-18 15:55:18 -04:00
|
|
|
private bool _disposed;
|
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ScheduledTasksWebSocketListener"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{ScheduledTasksWebSocketListener}"/> interface.</param>
|
|
|
|
/// <param name="taskManager">Instance of the <see cref="ITaskManager"/> interface.</param>
|
|
|
|
public ScheduledTasksWebSocketListener(ILogger<ScheduledTasksWebSocketListener> logger, ITaskManager taskManager)
|
|
|
|
: base(logger)
|
|
|
|
{
|
|
|
|
_taskManager = taskManager;
|
2014-05-10 13:28:03 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
_taskManager.TaskExecuting += OnTaskExecuting;
|
|
|
|
_taskManager.TaskCompleted += OnTaskCompleted;
|
|
|
|
}
|
2014-05-10 13:28:03 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
protected override SessionMessageType Type => SessionMessageType.ScheduledTasksInfo;
|
2020-09-25 03:25:59 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
protected override SessionMessageType StartType => SessionMessageType.ScheduledTasksInfoStart;
|
2020-09-25 03:25:59 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
protected override SessionMessageType StopType => SessionMessageType.ScheduledTasksInfoStop;
|
2013-02-21 15:26:35 -05:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the data to send.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task{IEnumerable{TaskInfo}}.</returns>
|
|
|
|
protected override Task<IEnumerable<TaskInfo>> GetDataToSend()
|
|
|
|
{
|
|
|
|
return Task.FromResult(_taskManager.ScheduledTasks
|
|
|
|
.OrderBy(i => i.Name)
|
|
|
|
.Select(ScheduledTaskHelpers.GetTaskInfo)
|
|
|
|
.Where(i => !i.IsHidden));
|
|
|
|
}
|
2014-05-10 13:28:03 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <inheritdoc />
|
2024-03-18 15:55:18 -04:00
|
|
|
protected override async ValueTask DisposeAsyncCore()
|
2023-01-31 06:18:10 -05:00
|
|
|
{
|
2024-03-18 15:55:18 -04:00
|
|
|
if (!_disposed)
|
2023-11-04 21:01:14 -04:00
|
|
|
{
|
|
|
|
_taskManager.TaskExecuting -= OnTaskExecuting;
|
|
|
|
_taskManager.TaskCompleted -= OnTaskCompleted;
|
2024-03-18 15:55:18 -04:00
|
|
|
_disposed = true;
|
2023-11-04 21:01:14 -04:00
|
|
|
}
|
2016-10-31 23:07:45 -04:00
|
|
|
|
2024-03-18 15:55:18 -04:00
|
|
|
await base.DisposeAsyncCore().ConfigureAwait(false);
|
2023-01-31 06:18:10 -05:00
|
|
|
}
|
2020-08-03 15:09:32 -04:00
|
|
|
|
2024-03-18 15:55:18 -04:00
|
|
|
private void OnTaskCompleted(object? sender, TaskCompletionEventArgs e)
|
2023-01-31 06:18:10 -05:00
|
|
|
{
|
|
|
|
e.Task.TaskProgress -= OnTaskProgress;
|
2024-03-18 15:55:18 -04:00
|
|
|
SendData(true);
|
2023-01-31 06:18:10 -05:00
|
|
|
}
|
2020-08-03 15:09:32 -04:00
|
|
|
|
2024-03-18 15:55:18 -04:00
|
|
|
private void OnTaskExecuting(object? sender, GenericEventArgs<IScheduledTaskWorker> e)
|
2023-01-31 06:18:10 -05:00
|
|
|
{
|
2024-03-18 15:55:18 -04:00
|
|
|
SendData(true);
|
2023-01-31 06:18:10 -05:00
|
|
|
e.Argument.TaskProgress += OnTaskProgress;
|
|
|
|
}
|
2020-08-03 15:09:32 -04:00
|
|
|
|
2024-03-18 15:55:18 -04:00
|
|
|
private void OnTaskProgress(object? sender, GenericEventArgs<double> e)
|
2023-01-31 06:18:10 -05:00
|
|
|
{
|
2024-03-18 15:55:18 -04:00
|
|
|
SendData(false);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
}
|