ScheduledTasksController.cs 6.1 KB

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