ScheduledTaskService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Common.Extensions;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Model.Services;
  8. using MediaBrowser.Model.Tasks;
  9. using Microsoft.Extensions.Logging;
  10. namespace MediaBrowser.Api.ScheduledTasks
  11. {
  12. /// <summary>
  13. /// Class GetScheduledTask.
  14. /// </summary>
  15. [Route("/ScheduledTasks/{Id}", "GET", Summary = "Gets a scheduled task, by Id")]
  16. public class GetScheduledTask : IReturn<TaskInfo>
  17. {
  18. /// <summary>
  19. /// Gets or sets the id.
  20. /// </summary>
  21. /// <value>The id.</value>
  22. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  23. public string Id { get; set; }
  24. }
  25. /// <summary>
  26. /// Class GetScheduledTasks.
  27. /// </summary>
  28. [Route("/ScheduledTasks", "GET", Summary = "Gets scheduled tasks")]
  29. public class GetScheduledTasks : IReturn<TaskInfo[]>
  30. {
  31. [ApiMember(Name = "IsHidden", Description = "Optional filter tasks that are hidden, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  32. public bool? IsHidden { get; set; }
  33. [ApiMember(Name = "IsEnabled", Description = "Optional filter tasks that are enabled, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  34. public bool? IsEnabled { get; set; }
  35. }
  36. /// <summary>
  37. /// Class StartScheduledTask.
  38. /// </summary>
  39. [Route("/ScheduledTasks/Running/{Id}", "POST", Summary = "Starts a scheduled task")]
  40. public class StartScheduledTask : IReturnVoid
  41. {
  42. /// <summary>
  43. /// Gets or sets the id.
  44. /// </summary>
  45. /// <value>The id.</value>
  46. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  47. public string Id { get; set; }
  48. }
  49. /// <summary>
  50. /// Class StopScheduledTask.
  51. /// </summary>
  52. [Route("/ScheduledTasks/Running/{Id}", "DELETE", Summary = "Stops a scheduled task")]
  53. public class StopScheduledTask : IReturnVoid
  54. {
  55. /// <summary>
  56. /// Gets or sets the id.
  57. /// </summary>
  58. /// <value>The id.</value>
  59. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  60. public string Id { get; set; }
  61. }
  62. /// <summary>
  63. /// Class UpdateScheduledTaskTriggers.
  64. /// </summary>
  65. [Route("/ScheduledTasks/{Id}/Triggers", "POST", Summary = "Updates the triggers for a scheduled task")]
  66. public class UpdateScheduledTaskTriggers : List<TaskTriggerInfo>, IReturnVoid
  67. {
  68. /// <summary>
  69. /// Gets or sets the task id.
  70. /// </summary>
  71. /// <value>The task id.</value>
  72. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  73. public string Id { get; set; }
  74. }
  75. /// <summary>
  76. /// Class ScheduledTasksService.
  77. /// </summary>
  78. [Authenticated(Roles = "Admin")]
  79. public class ScheduledTaskService : BaseApiService
  80. {
  81. /// <summary>
  82. /// The task manager.
  83. /// </summary>
  84. private readonly ITaskManager _taskManager;
  85. /// <summary>
  86. /// Initializes a new instance of the <see cref="ScheduledTaskService" /> class.
  87. /// </summary>
  88. /// <param name="taskManager">The task manager.</param>
  89. /// <exception cref="ArgumentNullException">taskManager</exception>
  90. public ScheduledTaskService(
  91. ILogger<ScheduledTaskService> logger,
  92. IServerConfigurationManager serverConfigurationManager,
  93. IHttpResultFactory httpResultFactory,
  94. ITaskManager taskManager)
  95. : base(logger, serverConfigurationManager, httpResultFactory)
  96. {
  97. _taskManager = taskManager;
  98. }
  99. /// <summary>
  100. /// Gets the specified request.
  101. /// </summary>
  102. /// <param name="request">The request.</param>
  103. /// <returns>IEnumerable{TaskInfo}.</returns>
  104. public object Get(GetScheduledTasks request)
  105. {
  106. IEnumerable<IScheduledTaskWorker> result = _taskManager.ScheduledTasks
  107. .OrderBy(i => i.Name);
  108. if (request.IsHidden.HasValue)
  109. {
  110. var val = request.IsHidden.Value;
  111. result = result.Where(i =>
  112. {
  113. var isHidden = false;
  114. if (i.ScheduledTask is IConfigurableScheduledTask configurableTask)
  115. {
  116. isHidden = configurableTask.IsHidden;
  117. }
  118. return isHidden == val;
  119. });
  120. }
  121. if (request.IsEnabled.HasValue)
  122. {
  123. var val = request.IsEnabled.Value;
  124. result = result.Where(i =>
  125. {
  126. var isEnabled = true;
  127. if (i.ScheduledTask is IConfigurableScheduledTask configurableTask)
  128. {
  129. isEnabled = configurableTask.IsEnabled;
  130. }
  131. return isEnabled == val;
  132. });
  133. }
  134. var infos = result
  135. .Select(ScheduledTaskHelpers.GetTaskInfo)
  136. .ToArray();
  137. return ToOptimizedResult(infos);
  138. }
  139. /// <summary>
  140. /// Gets the specified request.
  141. /// </summary>
  142. /// <param name="request">The request.</param>
  143. /// <returns>IEnumerable{TaskInfo}.</returns>
  144. /// <exception cref="ResourceNotFoundException">Task not found</exception>
  145. public object Get(GetScheduledTask request)
  146. {
  147. var task = _taskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  148. if (task == null)
  149. {
  150. throw new ResourceNotFoundException("Task not found");
  151. }
  152. var result = ScheduledTaskHelpers.GetTaskInfo(task);
  153. return ToOptimizedResult(result);
  154. }
  155. /// <summary>
  156. /// Posts the specified request.
  157. /// </summary>
  158. /// <param name="request">The request.</param>
  159. /// <exception cref="ResourceNotFoundException">Task not found</exception>
  160. public void Post(StartScheduledTask request)
  161. {
  162. var task = _taskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  163. if (task == null)
  164. {
  165. throw new ResourceNotFoundException("Task not found");
  166. }
  167. _taskManager.Execute(task, new TaskOptions());
  168. }
  169. /// <summary>
  170. /// Posts the specified request.
  171. /// </summary>
  172. /// <param name="request">The request.</param>
  173. /// <exception cref="ResourceNotFoundException">Task not found</exception>
  174. public void Delete(StopScheduledTask request)
  175. {
  176. var task = _taskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  177. if (task == null)
  178. {
  179. throw new ResourceNotFoundException("Task not found");
  180. }
  181. _taskManager.Cancel(task);
  182. }
  183. /// <summary>
  184. /// Posts the specified request.
  185. /// </summary>
  186. /// <param name="request">The request.</param>
  187. /// <exception cref="ResourceNotFoundException">Task not found</exception>
  188. public void Post(UpdateScheduledTaskTriggers request)
  189. {
  190. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  191. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  192. var id = GetPathValue(1).ToString();
  193. var task = _taskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.Ordinal));
  194. if (task == null)
  195. {
  196. throw new ResourceNotFoundException("Task not found");
  197. }
  198. task.Triggers = request.ToArray();
  199. }
  200. }
  201. }