ScheduledTaskService.cs 7.5 KB

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