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