ScheduledTasksController.cs 6.0 KB

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