jellyfin-server/Jellyfin.Api/WebSocketListeners/ScheduledTasksWebSocketListener.cs

84 lines
2.8 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
2013-02-21 01:33:05 +00:00
using System.Linq;
2017-11-23 15:46:16 +00:00
using System.Threading.Tasks;
using Jellyfin.Data.Events;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Session;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
2013-02-21 01:33:05 +00:00
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.WebSocketListeners;
/// <summary>
/// Class ScheduledTasksWebSocketListener.
/// </summary>
public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<TaskInfo>, WebSocketListenerState>
2013-02-21 01:33:05 +00:00
{
/// <summary>
2023-01-31 11:18:10 +00:00
/// Gets or sets the task manager.
2013-02-21 01:33:05 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <value>The task manager.</value>
private readonly ITaskManager _taskManager;
2013-02-23 07:57:11 +00:00
2023-01-31 11:18:10 +00: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 17:28:03 +00:00
2023-01-31 11:18:10 +00:00
_taskManager.TaskExecuting += OnTaskExecuting;
_taskManager.TaskCompleted += OnTaskCompleted;
}
2014-05-10 17:28:03 +00:00
2023-01-31 11:18:10 +00:00
/// <inheritdoc />
protected override SessionMessageType Type => SessionMessageType.ScheduledTasksInfo;
2023-01-31 11:18:10 +00:00
/// <inheritdoc />
protected override SessionMessageType StartType => SessionMessageType.ScheduledTasksInfoStart;
2023-01-31 11:18:10 +00:00
/// <inheritdoc />
protected override SessionMessageType StopType => SessionMessageType.ScheduledTasksInfoStop;
2013-02-21 20:26:35 +00:00
2023-01-31 11:18:10 +00: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 17:28:03 +00:00
2023-01-31 11:18:10 +00:00
/// <inheritdoc />
protected override void Dispose(bool dispose)
{
_taskManager.TaskExecuting -= OnTaskExecuting;
_taskManager.TaskCompleted -= OnTaskCompleted;
2016-11-01 03:07:45 +00:00
2023-01-31 11:18:10 +00:00
base.Dispose(dispose);
}
2023-01-31 11:18:10 +00:00
private async void OnTaskCompleted(object? sender, TaskCompletionEventArgs e)
{
e.Task.TaskProgress -= OnTaskProgress;
await SendData(true).ConfigureAwait(false);
}
2023-01-31 11:18:10 +00:00
private async void OnTaskExecuting(object? sender, GenericEventArgs<IScheduledTaskWorker> e)
{
await SendData(true).ConfigureAwait(false);
e.Argument.TaskProgress += OnTaskProgress;
}
2023-01-31 11:18:10 +00:00
private async void OnTaskProgress(object? sender, GenericEventArgs<double> e)
{
await SendData(false).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
}
}