ScheduledTaskService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using MediaBrowser.Model.Tasks;
  4. using MediaBrowser.Server.Implementations.HttpServer;
  5. using ServiceStack.ServiceHost;
  6. using ServiceStack.Text.Controller;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. namespace MediaBrowser.Api.ScheduledTasks
  11. {
  12. /// <summary>
  13. /// Class GetScheduledTask
  14. /// </summary>
  15. [Route("/ScheduledTasks/{Id}", "GET")]
  16. [ServiceStack.ServiceHost.Api(Description = "Gets a scheduled task, by Id")]
  17. public class GetScheduledTask : IReturn<TaskInfo>
  18. {
  19. /// <summary>
  20. /// Gets or sets the id.
  21. /// </summary>
  22. /// <value>The id.</value>
  23. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  24. public Guid Id { get; set; }
  25. }
  26. /// <summary>
  27. /// Class GetScheduledTasks
  28. /// </summary>
  29. [Route("/ScheduledTasks", "GET")]
  30. [ServiceStack.ServiceHost.Api(Description = "Gets scheduled tasks")]
  31. public class GetScheduledTasks : IReturn<List<TaskInfo>>
  32. {
  33. }
  34. /// <summary>
  35. /// Class StartScheduledTask
  36. /// </summary>
  37. [Route("/ScheduledTasks/Running/{Id}", "POST")]
  38. [ServiceStack.ServiceHost.Api(Description = "Starts a scheduled task")]
  39. public class StartScheduledTask : IReturnVoid
  40. {
  41. /// <summary>
  42. /// Gets or sets the id.
  43. /// </summary>
  44. /// <value>The id.</value>
  45. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  46. public Guid Id { get; set; }
  47. }
  48. /// <summary>
  49. /// Class StopScheduledTask
  50. /// </summary>
  51. [Route("/ScheduledTasks/Running/{Id}", "DELETE")]
  52. [ServiceStack.ServiceHost.Api(Description = "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 Guid Id { get; set; }
  61. }
  62. /// <summary>
  63. /// Class UpdateScheduledTaskTriggers
  64. /// </summary>
  65. [Route("/ScheduledTasks/{Id}/Triggers", "POST")]
  66. [ServiceStack.ServiceHost.Api(Description = "Updates the triggers for a scheduled task")]
  67. public class UpdateScheduledTaskTriggers : List<TaskTriggerInfo>, IReturnVoid
  68. {
  69. /// <summary>
  70. /// Gets or sets the task id.
  71. /// </summary>
  72. /// <value>The task id.</value>
  73. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  74. public Guid Id { get; set; }
  75. }
  76. /// <summary>
  77. /// Class ScheduledTasksService
  78. /// </summary>
  79. public class ScheduledTaskService : BaseApiService
  80. {
  81. /// <summary>
  82. /// Gets or sets the task manager.
  83. /// </summary>
  84. /// <value>The task manager.</value>
  85. private ITaskManager TaskManager { get; set; }
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="ScheduledTaskService" /> class.
  88. /// </summary>
  89. /// <param name="taskManager">The task manager.</param>
  90. /// <exception cref="System.ArgumentNullException">taskManager</exception>
  91. public ScheduledTaskService(ITaskManager taskManager)
  92. {
  93. if (taskManager == null)
  94. {
  95. throw new ArgumentNullException("taskManager");
  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. var result = TaskManager.ScheduledTasks.OrderBy(i => i.Name)
  107. .Select(ScheduledTaskHelpers.GetTaskInfo).ToList();
  108. return ToOptimizedResult(result);
  109. }
  110. /// <summary>
  111. /// Gets the specified request.
  112. /// </summary>
  113. /// <param name="request">The request.</param>
  114. /// <returns>IEnumerable{TaskInfo}.</returns>
  115. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  116. public object Get(GetScheduledTask request)
  117. {
  118. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => i.Id == request.Id);
  119. if (task == null)
  120. {
  121. throw new ResourceNotFoundException("Task not found");
  122. }
  123. var result = ScheduledTaskHelpers.GetTaskInfo(task);
  124. return ToOptimizedResult(result);
  125. }
  126. /// <summary>
  127. /// Posts the specified request.
  128. /// </summary>
  129. /// <param name="request">The request.</param>
  130. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  131. public void Post(StartScheduledTask request)
  132. {
  133. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => i.Id == request.Id);
  134. if (task == null)
  135. {
  136. throw new ResourceNotFoundException("Task not found");
  137. }
  138. TaskManager.Execute(task);
  139. }
  140. /// <summary>
  141. /// Posts the specified request.
  142. /// </summary>
  143. /// <param name="request">The request.</param>
  144. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  145. public void Delete(StopScheduledTask request)
  146. {
  147. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => i.Id == request.Id);
  148. if (task == null)
  149. {
  150. throw new ResourceNotFoundException("Task not found");
  151. }
  152. TaskManager.Cancel(task);
  153. }
  154. /// <summary>
  155. /// Posts the specified request.
  156. /// </summary>
  157. /// <param name="request">The request.</param>
  158. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  159. public void Post(UpdateScheduledTaskTriggers request)
  160. {
  161. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  162. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  163. var pathInfo = PathInfo.Parse(Request.PathInfo);
  164. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  165. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => i.Id == id);
  166. if (task == null)
  167. {
  168. throw new ResourceNotFoundException("Task not found");
  169. }
  170. var triggerInfos = request;
  171. task.Triggers = triggerInfos.Select(ScheduledTaskHelpers.GetTrigger);
  172. }
  173. }
  174. }