ScheduledTasksController.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using MediaBrowser.Model.Tasks;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.ModelBinding;
  9. namespace Jellyfin.Api.Controllers
  10. {
  11. /// <summary>
  12. /// Scheduled Tasks Controller.
  13. /// </summary>
  14. public class ScheduledTasksController : BaseJellyfinApiController
  15. {
  16. private readonly ITaskManager _taskManager;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="ScheduledTasksController"/> class.
  19. /// </summary>
  20. /// <param name="taskManager">Instance of the <see cref="ITaskManager"/> interface.</param>
  21. public ScheduledTasksController(ITaskManager taskManager)
  22. {
  23. _taskManager = taskManager;
  24. }
  25. /// <summary>
  26. /// Get tasks.
  27. /// </summary>
  28. /// <param name="isHidden">Optional filter tasks that are hidden, or not.</param>
  29. /// <param name="isEnabled">Optional filter tasks that are enabled, or not.</param>
  30. /// <returns>Task list.</returns>
  31. [HttpGet]
  32. [ProducesResponseType(typeof(TaskInfo[]), StatusCodes.Status200OK)]
  33. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  34. public IActionResult GetTasks(
  35. [FromQuery] bool? isHidden = false,
  36. [FromQuery] bool? isEnabled = false)
  37. {
  38. try
  39. {
  40. IEnumerable<IScheduledTaskWorker> tasks = _taskManager.ScheduledTasks.OrderBy(o => o.Name);
  41. if (isHidden.HasValue)
  42. {
  43. var hiddenValue = isHidden.Value;
  44. tasks = tasks.Where(o =>
  45. {
  46. var itemIsHidden = false;
  47. if (o.ScheduledTask is IConfigurableScheduledTask configurableScheduledTask)
  48. {
  49. itemIsHidden = configurableScheduledTask.IsHidden;
  50. }
  51. return itemIsHidden == hiddenValue;
  52. });
  53. }
  54. if (isEnabled.HasValue)
  55. {
  56. var enabledValue = isEnabled.Value;
  57. tasks = tasks.Where(o =>
  58. {
  59. var itemIsEnabled = false;
  60. if (o.ScheduledTask is IConfigurableScheduledTask configurableScheduledTask)
  61. {
  62. itemIsEnabled = configurableScheduledTask.IsEnabled;
  63. }
  64. return itemIsEnabled == enabledValue;
  65. });
  66. }
  67. var taskInfos = tasks.Select(ScheduledTaskHelpers.GetTaskInfo);
  68. return Ok(taskInfos);
  69. }
  70. catch (Exception e)
  71. {
  72. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  73. }
  74. }
  75. /// <summary>
  76. /// Get task by id.
  77. /// </summary>
  78. /// <param name="taskId">Task Id.</param>
  79. /// <returns>Task Info.</returns>
  80. [HttpGet("{TaskID}")]
  81. [ProducesResponseType(typeof(TaskInfo), StatusCodes.Status200OK)]
  82. [ProducesResponseType(StatusCodes.Status404NotFound)]
  83. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  84. public IActionResult GetTask([FromRoute] string taskId)
  85. {
  86. try
  87. {
  88. var task = _taskManager.ScheduledTasks.FirstOrDefault(i =>
  89. string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase));
  90. if (task == null)
  91. {
  92. return NotFound();
  93. }
  94. var result = ScheduledTaskHelpers.GetTaskInfo(task);
  95. return Ok(result);
  96. }
  97. catch (Exception e)
  98. {
  99. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  100. }
  101. }
  102. /// <summary>
  103. /// Start specified task.
  104. /// </summary>
  105. /// <param name="taskId">Task Id.</param>
  106. /// <returns>Status.</returns>
  107. [HttpPost("Running/{TaskID}")]
  108. [ProducesResponseType(StatusCodes.Status200OK)]
  109. [ProducesResponseType(StatusCodes.Status404NotFound)]
  110. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  111. public IActionResult StartTask([FromRoute] string taskId)
  112. {
  113. try
  114. {
  115. var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
  116. o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
  117. if (task == null)
  118. {
  119. return NotFound();
  120. }
  121. _taskManager.Execute(task, new TaskOptions());
  122. return Ok();
  123. }
  124. catch (Exception e)
  125. {
  126. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  127. }
  128. }
  129. /// <summary>
  130. /// Stop specified task.
  131. /// </summary>
  132. /// <param name="taskId">Task Id.</param>
  133. /// <returns>Status.</returns>
  134. [HttpDelete("Running/{TaskID}")]
  135. [ProducesResponseType(StatusCodes.Status200OK)]
  136. [ProducesResponseType(StatusCodes.Status404NotFound)]
  137. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  138. public IActionResult StopTask([FromRoute] string taskId)
  139. {
  140. try
  141. {
  142. var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
  143. o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
  144. if (task == null)
  145. {
  146. return NotFound();
  147. }
  148. _taskManager.Cancel(task);
  149. return Ok();
  150. }
  151. catch (Exception e)
  152. {
  153. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  154. }
  155. }
  156. /// <summary>
  157. /// Update specified task triggers.
  158. /// </summary>
  159. /// <param name="taskId">Task Id.</param>
  160. /// <param name="triggerInfos">Triggers.</param>
  161. /// <returns>Status.</returns>
  162. [HttpPost("{TaskID}/Triggers")]
  163. [ProducesResponseType(StatusCodes.Status200OK)]
  164. [ProducesResponseType(StatusCodes.Status404NotFound)]
  165. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  166. public IActionResult UpdateTask([FromRoute] string taskId, [FromBody] TaskTriggerInfo[] triggerInfos)
  167. {
  168. try
  169. {
  170. var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
  171. o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
  172. if (task == null)
  173. {
  174. return NotFound();
  175. }
  176. task.Triggers = triggerInfos;
  177. return Ok();
  178. }
  179. catch (Exception e)
  180. {
  181. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  182. }
  183. }
  184. }
  185. }