| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.ComponentModel.DataAnnotations; |
| | 4 | | using System.Linq; |
| | 5 | | using Jellyfin.Api.Constants; |
| | 6 | | using MediaBrowser.Common.Api; |
| | 7 | | using MediaBrowser.Model.Tasks; |
| | 8 | | using Microsoft.AspNetCore.Authorization; |
| | 9 | | using Microsoft.AspNetCore.Http; |
| | 10 | | using Microsoft.AspNetCore.Mvc; |
| | 11 | |
|
| | 12 | | namespace Jellyfin.Api.Controllers; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Scheduled Tasks Controller. |
| | 16 | | /// </summary> |
| | 17 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | 18 | | public class ScheduledTasksController : BaseJellyfinApiController |
| | 19 | | { |
| | 20 | | private readonly ITaskManager _taskManager; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="ScheduledTasksController"/> class. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="taskManager">Instance of the <see cref="ITaskManager"/> interface.</param> |
| 0 | 26 | | public ScheduledTasksController(ITaskManager taskManager) |
| | 27 | | { |
| 0 | 28 | | _taskManager = taskManager; |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Get tasks. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="isHidden">Optional filter tasks that are hidden, or not.</param> |
| | 35 | | /// <param name="isEnabled">Optional filter tasks that are enabled, or not.</param> |
| | 36 | | /// <response code="200">Scheduled tasks retrieved.</response> |
| | 37 | | /// <returns>The list of scheduled tasks.</returns> |
| | 38 | | [HttpGet] |
| | 39 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 40 | | public IEnumerable<TaskInfo> GetTasks( |
| | 41 | | [FromQuery] bool? isHidden, |
| | 42 | | [FromQuery] bool? isEnabled) |
| | 43 | | { |
| | 44 | | IEnumerable<IScheduledTaskWorker> tasks = _taskManager.ScheduledTasks.OrderBy(o => o.Name); |
| | 45 | |
|
| | 46 | | foreach (var task in tasks) |
| | 47 | | { |
| | 48 | | if (task.ScheduledTask is IConfigurableScheduledTask scheduledTask) |
| | 49 | | { |
| | 50 | | if (isHidden.HasValue && isHidden.Value != scheduledTask.IsHidden) |
| | 51 | | { |
| | 52 | | continue; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | if (isEnabled.HasValue && isEnabled.Value != scheduledTask.IsEnabled) |
| | 56 | | { |
| | 57 | | continue; |
| | 58 | | } |
| | 59 | | } |
| | 60 | |
|
| | 61 | | yield return ScheduledTaskHelpers.GetTaskInfo(task); |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Get task by id. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="taskId">Task Id.</param> |
| | 69 | | /// <response code="200">Task retrieved.</response> |
| | 70 | | /// <response code="404">Task not found.</response> |
| | 71 | | /// <returns>An <see cref="OkResult"/> containing the task on success, or a <see cref="NotFoundResult"/> if the task |
| | 72 | | [HttpGet("{taskId}")] |
| | 73 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 74 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 75 | | public ActionResult<TaskInfo> GetTask([FromRoute, Required] string taskId) |
| | 76 | | { |
| 0 | 77 | | var task = _taskManager.ScheduledTasks.FirstOrDefault(i => |
| 0 | 78 | | string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase)); |
| | 79 | |
|
| 0 | 80 | | if (task is null) |
| | 81 | | { |
| 0 | 82 | | return NotFound(); |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | return ScheduledTaskHelpers.GetTaskInfo(task); |
| | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Start specified task. |
| | 90 | | /// </summary> |
| | 91 | | /// <param name="taskId">Task Id.</param> |
| | 92 | | /// <response code="204">Task started.</response> |
| | 93 | | /// <response code="404">Task not found.</response> |
| | 94 | | /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be |
| | 95 | | [HttpPost("Running/{taskId}")] |
| | 96 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 97 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 98 | | public ActionResult StartTask([FromRoute, Required] string taskId) |
| | 99 | | { |
| 0 | 100 | | var task = _taskManager.ScheduledTasks.FirstOrDefault(o => |
| 0 | 101 | | o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); |
| | 102 | |
|
| 0 | 103 | | if (task is null) |
| | 104 | | { |
| 0 | 105 | | return NotFound(); |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | _taskManager.Execute(task, new TaskOptions()); |
| 0 | 109 | | return NoContent(); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Stop specified task. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="taskId">Task Id.</param> |
| | 116 | | /// <response code="204">Task stopped.</response> |
| | 117 | | /// <response code="404">Task not found.</response> |
| | 118 | | /// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found. |
| | 119 | | [HttpDelete("Running/{taskId}")] |
| | 120 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 121 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 122 | | public ActionResult StopTask([FromRoute, Required] string taskId) |
| | 123 | | { |
| 0 | 124 | | var task = _taskManager.ScheduledTasks.FirstOrDefault(o => |
| 0 | 125 | | o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); |
| | 126 | |
|
| 0 | 127 | | if (task is null) |
| | 128 | | { |
| 0 | 129 | | return NotFound(); |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | _taskManager.Cancel(task); |
| 0 | 133 | | return NoContent(); |
| | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Update specified task triggers. |
| | 138 | | /// </summary> |
| | 139 | | /// <param name="taskId">Task Id.</param> |
| | 140 | | /// <param name="triggerInfos">Triggers.</param> |
| | 141 | | /// <response code="204">Task triggers updated.</response> |
| | 142 | | /// <response code="404">Task not found.</response> |
| | 143 | | /// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found. |
| | 144 | | [HttpPost("{taskId}/Triggers")] |
| | 145 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 146 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 147 | | public ActionResult UpdateTask( |
| | 148 | | [FromRoute, Required] string taskId, |
| | 149 | | [FromBody, Required] TaskTriggerInfo[] triggerInfos) |
| | 150 | | { |
| 0 | 151 | | var task = _taskManager.ScheduledTasks.FirstOrDefault(o => |
| 0 | 152 | | o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase)); |
| 0 | 153 | | if (task is null) |
| | 154 | | { |
| 0 | 155 | | return NotFound(); |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | task.Triggers = triggerInfos; |
| 0 | 159 | | return NoContent(); |
| | 160 | | } |
| | 161 | | } |