ScheduledTasksController.cs 5.6 KB

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