using MediaBrowser.Common.Net;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Api.ScheduledTasks
{
///
/// Class ScheduledTasksWebSocketListener
///
public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener, object>
{
///
/// Gets or sets the task manager.
///
/// The task manager.
private ITaskManager TaskManager { get; set; }
///
/// Gets the name.
///
/// The name.
protected override string Name
{
get { return "ScheduledTasksInfo"; }
}
///
/// Initializes a new instance of the class.
///
/// The logger.
/// The task manager.
public ScheduledTasksWebSocketListener(ILogger logger, ITaskManager taskManager)
: base(logger)
{
TaskManager = taskManager;
}
private bool _lastResponseHadTasksRunning = true;
///
/// Gets the data to send.
///
/// The state.
/// Task{IEnumerable{TaskInfo}}.
protected override Task> GetDataToSend(object state)
{
var tasks = TaskManager.ScheduledTasks.ToList();
var anyRunning = tasks.Any(i => i.State != TaskState.Idle);
if (anyRunning)
{
_lastResponseHadTasksRunning = true;
}
else
{
if (!_lastResponseHadTasksRunning)
{
return Task.FromResult>(null);
}
_lastResponseHadTasksRunning = false;
}
return Task.FromResult(tasks
.OrderBy(i => i.Name)
.Select(ScheduledTaskHelpers.GetTaskInfo)
.Where(i => !i.IsHidden));
}
}
}