jellyfin/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs

78 lines
2.5 KiB
C#
Raw Normal View History

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;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
2013-02-20 18:33:05 -07:00
2013-02-21 21:23:06 -07:00
namespace MediaBrowser.Api.ScheduledTasks
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// 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>
private ITaskManager TaskManager { get; set; }
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
protected override string Name => "ScheduledTasksInfo";
2013-02-21 13:26:35 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="ScheduledTasksWebSocketListener" /> class.
/// </summary>
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
{
2013-02-23 00:57:11 -07:00
TaskManager = taskManager;
2014-05-10 10:28:03 -07:00
TaskManager.TaskExecuting += TaskManager_TaskExecuting;
TaskManager.TaskCompleted += TaskManager_TaskCompleted;
}
void TaskManager_TaskCompleted(object sender, TaskCompletionEventArgs e)
{
SendData(true);
e.Task.TaskProgress -= Argument_TaskProgress;
}
void TaskManager_TaskExecuting(object sender, GenericEventArgs<IScheduledTaskWorker> e)
{
SendData(true);
e.Argument.TaskProgress += Argument_TaskProgress;
}
void Argument_TaskProgress(object sender, GenericEventArgs<double> e)
{
SendData(false);
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
{
2014-05-09 16:08:08 -07:00
return Task.FromResult(TaskManager.ScheduledTasks
.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
2014-05-13 17:46:45 -07:00
protected override void Dispose(bool dispose)
{
TaskManager.TaskExecuting -= TaskManager_TaskExecuting;
TaskManager.TaskCompleted -= TaskManager_TaskCompleted;
2016-10-31 20:07:45 -07:00
2014-05-13 17:46:45 -07:00
base.Dispose(dispose);
}
2013-02-20 18:33:05 -07:00
}
}