jellyfin/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs

76 lines
2.3 KiB
C#
Raw Normal View History

2013-03-06 22:34:00 -07:00
using MediaBrowser.Common.Net;
2013-02-20 18:33:05 -07:00
using MediaBrowser.Common.ScheduledTasks;
2013-02-21 13:26:35 -07:00
using MediaBrowser.Model.Logging;
2013-02-20 18:33:05 -07:00
using MediaBrowser.Model.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2013-02-21 21:23:06 -07:00
namespace MediaBrowser.Api.ScheduledTasks
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Class ScheduledTasksWebSocketListener
/// </summary>
2013-02-22 08:16:48 -07:00
public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<TaskInfo>, object>
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
{
get { return "ScheduledTasksInfo"; }
}
2013-02-21 13:26:35 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="ScheduledTasksWebSocketListener" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
2013-02-23 00:57:11 -07:00
/// <param name="taskManager">The task manager.</param>
public ScheduledTasksWebSocketListener(ILogger logger, ITaskManager taskManager)
2013-02-21 13:26:35 -07:00
: base(logger)
{
2013-02-23 00:57:11 -07:00
TaskManager = taskManager;
2013-02-21 13:26:35 -07:00
}
2014-03-24 05:47:39 -07:00
private bool _lastResponseHadTasksRunning = true;
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets the data to send.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>Task{IEnumerable{TaskInfo}}.</returns>
protected override Task<IEnumerable<TaskInfo>> GetDataToSend(object state)
{
2014-03-24 05:47:39 -07:00
var tasks = TaskManager.ScheduledTasks.ToList();
var anyRunning = tasks.Any(i => i.State != TaskState.Idle);
if (anyRunning)
{
_lastResponseHadTasksRunning = true;
}
else
{
if (!_lastResponseHadTasksRunning)
{
return Task.FromResult<IEnumerable<TaskInfo>>(null);
}
_lastResponseHadTasksRunning = false;
}
return Task.FromResult(tasks
.OrderBy(i => i.Name)
.Select(ScheduledTaskHelpers.GetTaskInfo)
.Where(i => !i.IsHidden));
2013-02-20 18:33:05 -07:00
}
}
}