#nullable enable using System; using System.Collections.Generic; using System.Linq; using MediaBrowser.Controller.Net; using MediaBrowser.Model.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; namespace Jellyfin.Api.Controllers { /// /// Scheduled Tasks Controller. /// [Authenticated] public class ScheduledTasksController : BaseJellyfinApiController { private readonly ITaskManager _taskManager; /// /// Initializes a new instance of the class. /// /// Instance of the interface. public ScheduledTasksController(ITaskManager taskManager) { _taskManager = taskManager; } /// /// Get tasks. /// /// Optional filter tasks that are hidden, or not. /// Optional filter tasks that are enabled, or not. /// Task list. [HttpGet] [ProducesResponseType(typeof(TaskInfo[]), StatusCodes.Status200OK)] [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] public IActionResult GetTasks( [FromQuery] bool? isHidden = false, [FromQuery] bool? isEnabled = false) { try { IEnumerable tasks = _taskManager.ScheduledTasks.OrderBy(o => o.Name); if (isHidden.HasValue) { var hiddenValue = isHidden.Value; tasks = tasks.Where(o => { var itemIsHidden = false; if (o.ScheduledTask is IConfigurableScheduledTask configurableScheduledTask) { itemIsHidden = configurableScheduledTask.IsHidden; } return itemIsHidden == hiddenValue; }); } if (isEnabled.HasValue) { var enabledValue = isEnabled.Value; tasks = tasks.Where(o => { var itemIsEnabled = false; if (o.ScheduledTask is IConfigurableScheduledTask configurableScheduledTask) { itemIsEnabled = configurableScheduledTask.IsEnabled; } return itemIsEnabled == enabledValue; }); } var taskInfos = tasks.Select(ScheduledTaskHelpers.GetTaskInfo); return Ok(taskInfos); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } } /// /// Get task by id. /// /// Task Id. /// Task Info. [HttpGet("{TaskID}")] [ProducesResponseType(typeof(TaskInfo), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] public IActionResult GetTask([FromRoute] string taskId) { try { var task = _taskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase)); if (task == null) { return NotFound(); } var result = ScheduledTaskHelpers.GetTaskInfo(task); return Ok(result); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } } /// /// Start specified task. /// /// Task Id. /// Status. [HttpPost("Running/{TaskID}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] public IActionResult StartTask([FromRoute] string taskId) { try { var task = _taskManager.ScheduledTasks.FirstOrDefault(o => o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); if (task == null) { return NotFound(); } _taskManager.Execute(task, new TaskOptions()); return Ok(); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } } /// /// Stop specified task. /// /// Task Id. /// Status. [HttpDelete("Running/{TaskID}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] public IActionResult StopTask([FromRoute] string taskId) { try { var task = _taskManager.ScheduledTasks.FirstOrDefault(o => o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); if (task == null) { return NotFound(); } _taskManager.Cancel(task); return Ok(); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } } /// /// Update specified task triggers. /// /// Task Id. /// Triggers. /// Status. [HttpPost("{TaskID}/Triggers")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] public IActionResult UpdateTask([FromRoute] string taskId, [FromBody, BindRequired] TaskTriggerInfo[] triggerInfos) { try { var task = _taskManager.ScheduledTasks.FirstOrDefault(o => o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); if (task == null) { return NotFound(); } task.Triggers = triggerInfos; return Ok(); } catch (Exception e) { return StatusCode(StatusCodes.Status500InternalServerError, e.Message); } } } }