ScheduledTaskService.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Model.Tasks;
  5. using ServiceStack;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using MediaBrowser.Controller.Configuration;
  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<List<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. /// Gets or sets the task manager.
  83. /// </summary>
  84. /// <value>The task manager.</value>
  85. private ITaskManager TaskManager { get; set; }
  86. private readonly IServerConfigurationManager _config;
  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="ArgumentNullException">taskManager</exception>
  92. public ScheduledTaskService(ITaskManager taskManager, IServerConfigurationManager config)
  93. {
  94. if (taskManager == null)
  95. {
  96. throw new ArgumentNullException("taskManager");
  97. }
  98. TaskManager = taskManager;
  99. _config = config;
  100. }
  101. /// <summary>
  102. /// Gets the specified request.
  103. /// </summary>
  104. /// <param name="request">The request.</param>
  105. /// <returns>IEnumerable{TaskInfo}.</returns>
  106. public object Get(GetScheduledTasks request)
  107. {
  108. IEnumerable<IScheduledTaskWorker> result = TaskManager.ScheduledTasks
  109. .OrderBy(i => i.Name);
  110. if (request.IsHidden.HasValue)
  111. {
  112. var val = request.IsHidden.Value;
  113. result = result.Where(i =>
  114. {
  115. var isHidden = false;
  116. var configurableTask = i.ScheduledTask as IConfigurableScheduledTask;
  117. if (configurableTask != null)
  118. {
  119. isHidden = configurableTask.IsHidden;
  120. }
  121. return isHidden == val;
  122. });
  123. }
  124. if (request.IsEnabled.HasValue)
  125. {
  126. var val = request.IsEnabled.Value;
  127. result = result.Where(i =>
  128. {
  129. var isEnabled = true;
  130. var configurableTask = i.ScheduledTask as IConfigurableScheduledTask;
  131. if (configurableTask != null)
  132. {
  133. isEnabled = configurableTask.IsEnabled;
  134. }
  135. return isEnabled == val;
  136. });
  137. }
  138. var infos = result
  139. .Select(ScheduledTaskHelpers.GetTaskInfo)
  140. .ToList();
  141. return ToOptimizedResult(infos);
  142. }
  143. /// <summary>
  144. /// Gets the specified request.
  145. /// </summary>
  146. /// <param name="request">The request.</param>
  147. /// <returns>IEnumerable{TaskInfo}.</returns>
  148. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  149. public object Get(GetScheduledTask request)
  150. {
  151. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  152. if (task == null)
  153. {
  154. throw new ResourceNotFoundException("Task not found");
  155. }
  156. var result = ScheduledTaskHelpers.GetTaskInfo(task);
  157. return ToOptimizedResult(result);
  158. }
  159. /// <summary>
  160. /// Posts the specified request.
  161. /// </summary>
  162. /// <param name="request">The request.</param>
  163. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  164. public void Post(StartScheduledTask request)
  165. {
  166. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  167. if (task == null)
  168. {
  169. throw new ResourceNotFoundException("Task not found");
  170. }
  171. var hasKey = task.ScheduledTask as IHasKey;
  172. if (hasKey != null)
  173. {
  174. if (string.Equals(hasKey.Key, "SystemUpdateTask", StringComparison.OrdinalIgnoreCase))
  175. {
  176. // This is a hack for now just to get the update application function to work when auto-update is disabled
  177. if (!_config.Configuration.EnableAutoUpdate)
  178. {
  179. _config.Configuration.EnableAutoUpdate = true;
  180. _config.SaveConfiguration();
  181. }
  182. }
  183. }
  184. TaskManager.Execute(task);
  185. }
  186. /// <summary>
  187. /// Posts the specified request.
  188. /// </summary>
  189. /// <param name="request">The request.</param>
  190. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  191. public void Delete(StopScheduledTask request)
  192. {
  193. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  194. if (task == null)
  195. {
  196. throw new ResourceNotFoundException("Task not found");
  197. }
  198. TaskManager.Cancel(task);
  199. }
  200. /// <summary>
  201. /// Posts the specified request.
  202. /// </summary>
  203. /// <param name="request">The request.</param>
  204. /// <exception cref="MediaBrowser.Common.Extensions.ResourceNotFoundException">Task not found</exception>
  205. public void Post(UpdateScheduledTaskTriggers request)
  206. {
  207. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  208. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  209. var id = GetPathValue(1);
  210. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, id));
  211. if (task == null)
  212. {
  213. throw new ResourceNotFoundException("Task not found");
  214. }
  215. var triggerInfos = request;
  216. task.Triggers = triggerInfos.Select(ScheduledTaskHelpers.GetTrigger);
  217. }
  218. }
  219. }