ScheduledTasksController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.Tasks;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.ModelBinding;
  10. namespace Jellyfin.Api.Controllers
  11. {
  12. /// <summary>
  13. /// Scheduled Tasks Controller.
  14. /// </summary>
  15. // [Authenticated]
  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<IScheduledTaskWorker> GetTasks(
  37. [FromQuery] bool? isHidden = false,
  38. [FromQuery] bool? isEnabled = false)
  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 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] 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. var result = ScheduledTaskHelpers.GetTaskInfo(task);
  76. return Ok(result);
  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. }