ScheduledTaskService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using MediaBrowser.Model.Tasks;
  4. using ServiceStack.ServiceHost;
  5. using ServiceStack.Text.Controller;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  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. }
  33. /// <summary>
  34. /// Class StartScheduledTask
  35. /// </summary>
  36. [Route("/ScheduledTasks/Running/{Id}", "POST")]
  37. [Api(Description = "Starts a scheduled task")]
  38. public class StartScheduledTask : IReturnVoid
  39. {
  40. /// <summary>
  41. /// Gets or sets the id.
  42. /// </summary>
  43. /// <value>The id.</value>
  44. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  45. public Guid Id { get; set; }
  46. }
  47. /// <summary>
  48. /// Class StopScheduledTask
  49. /// </summary>
  50. [Route("/ScheduledTasks/Running/{Id}", "DELETE")]
  51. [Api(Description = "Stops a scheduled task")]
  52. public class StopScheduledTask : IReturnVoid
  53. {
  54. /// <summary>
  55. /// Gets or sets the id.
  56. /// </summary>
  57. /// <value>The id.</value>
  58. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  59. public Guid Id { get; set; }
  60. }
  61. /// <summary>
  62. /// Class UpdateScheduledTaskTriggers
  63. /// </summary>
  64. [Route("/ScheduledTasks/{Id}/Triggers", "POST")]
  65. [Api(Description = "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 Guid Id { get; set; }
  74. }
  75. /// <summary>
  76. /// Class ScheduledTasksService
  77. /// </summary>
  78. public class ScheduledTaskService : BaseApiService
  79. {
  80. /// <summary>
  81. /// Gets or sets the task manager.
  82. /// </summary>
  83. /// <value>The task manager.</value>
  84. private ITaskManager TaskManager { get; set; }
  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="System.ArgumentNullException">taskManager</exception>
  90. public ScheduledTaskService(ITaskManager taskManager)
  91. {
  92. if (taskManager == null)
  93. {
  94. throw new ArgumentNullException("taskManager");
  95. }
  96. TaskManager = taskManager;
  97. }
  98. /// <summary>
  99. /// Gets the specified request.
  100. /// </summary>
  101. /// <param name="request">The request.</param>
  102. /// <returns>IEnumerable{TaskInfo}.</returns>
  103. public object Get(GetScheduledTasks request)
  104. {
  105. var result = TaskManager.ScheduledTasks.OrderBy(i => i.Name)
  106. .Select(ScheduledTaskHelpers.GetTaskInfo).ToList();
  107. return ToOptimizedResult(result);
  108. }
  109. /// <summary>
  110. /// Gets the specified request.
  111. /// </summary>
  112. /// <param name="request">The request.</param>
  113. /// <returns>IEnumerable{TaskInfo}.</returns>
  114. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  115. public object Get(GetScheduledTask request)
  116. {
  117. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => i.Id == request.Id);
  118. if (task == null)
  119. {
  120. throw new ResourceNotFoundException("Task not found");
  121. }
  122. var result = ScheduledTaskHelpers.GetTaskInfo(task);
  123. return ToOptimizedResult(result);
  124. }
  125. /// <summary>
  126. /// Posts the specified request.
  127. /// </summary>
  128. /// <param name="request">The request.</param>
  129. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  130. public void Post(StartScheduledTask request)
  131. {
  132. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => i.Id == request.Id);
  133. if (task == null)
  134. {
  135. throw new ResourceNotFoundException("Task not found");
  136. }
  137. TaskManager.Execute(task);
  138. }
  139. /// <summary>
  140. /// Posts the specified request.
  141. /// </summary>
  142. /// <param name="request">The request.</param>
  143. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  144. public void Delete(StopScheduledTask request)
  145. {
  146. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => i.Id == request.Id);
  147. if (task == null)
  148. {
  149. throw new ResourceNotFoundException("Task not found");
  150. }
  151. TaskManager.Cancel(task);
  152. }
  153. /// <summary>
  154. /// Posts the specified request.
  155. /// </summary>
  156. /// <param name="request">The request.</param>
  157. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  158. public void Post(UpdateScheduledTaskTriggers request)
  159. {
  160. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  161. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  162. var pathInfo = PathInfo.Parse(RequestContext.PathInfo);
  163. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  164. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => i.Id == id);
  165. if (task == null)
  166. {
  167. throw new ResourceNotFoundException("Task not found");
  168. }
  169. var triggerInfos = request;
  170. task.Triggers = triggerInfos.Select(ScheduledTaskHelpers.GetTrigger);
  171. }
  172. }
  173. }