ScheduledTaskService.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. namespace MediaBrowser.Api.ScheduledTasks
  10. {
  11. /// <summary>
  12. /// Class GetScheduledTask
  13. /// </summary>
  14. [Route("/ScheduledTasks/{Id}", "GET", Summary = "Gets a scheduled task, by Id")]
  15. public class GetScheduledTask : IReturn<TaskInfo>
  16. {
  17. /// <summary>
  18. /// Gets or sets the id.
  19. /// </summary>
  20. /// <value>The id.</value>
  21. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  22. public string Id { get; set; }
  23. }
  24. /// <summary>
  25. /// Class GetScheduledTasks
  26. /// </summary>
  27. [Route("/ScheduledTasks", "GET", Summary = "Gets scheduled tasks")]
  28. public class GetScheduledTasks : IReturn<TaskInfo[]>
  29. {
  30. [ApiMember(Name = "IsHidden", Description = "Optional filter tasks that are hidden, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  31. public bool? IsHidden { get; set; }
  32. [ApiMember(Name = "IsEnabled", Description = "Optional filter tasks that are enabled, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  33. public bool? IsEnabled { get; set; }
  34. }
  35. /// <summary>
  36. /// Class StartScheduledTask
  37. /// </summary>
  38. [Route("/ScheduledTasks/Running/{Id}", "POST", Summary = "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 string Id { get; set; }
  47. }
  48. /// <summary>
  49. /// Class StopScheduledTask
  50. /// </summary>
  51. [Route("/ScheduledTasks/Running/{Id}", "DELETE", Summary = "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 string Id { get; set; }
  60. }
  61. /// <summary>
  62. /// Class UpdateScheduledTaskTriggers
  63. /// </summary>
  64. [Route("/ScheduledTasks/{Id}/Triggers", "POST", Summary = "Updates the triggers for a scheduled task")]
  65. public class UpdateScheduledTaskTriggers : List<TaskTriggerInfo>, IReturnVoid
  66. {
  67. /// <summary>
  68. /// Gets or sets the task id.
  69. /// </summary>
  70. /// <value>The task id.</value>
  71. [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  72. public string Id { get; set; }
  73. }
  74. /// <summary>
  75. /// Class ScheduledTasksService
  76. /// </summary>
  77. [Authenticated(Roles = "Admin")]
  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. private readonly IServerConfigurationManager _config;
  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="ArgumentNullException">taskManager</exception>
  91. public ScheduledTaskService(ITaskManager taskManager, IServerConfigurationManager config)
  92. {
  93. if (taskManager == null)
  94. {
  95. throw new ArgumentNullException(nameof(taskManager));
  96. }
  97. TaskManager = taskManager;
  98. _config = config;
  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. if (request.IsEnabled.HasValue)
  124. {
  125. var val = request.IsEnabled.Value;
  126. result = result.Where(i =>
  127. {
  128. var isEnabled = true;
  129. var configurableTask = i.ScheduledTask as IConfigurableScheduledTask;
  130. if (configurableTask != null)
  131. {
  132. isEnabled = configurableTask.IsEnabled;
  133. }
  134. return isEnabled == val;
  135. });
  136. }
  137. var infos = result
  138. .Select(ScheduledTaskHelpers.GetTaskInfo)
  139. .ToArray();
  140. return ToOptimizedResult(infos);
  141. }
  142. /// <summary>
  143. /// Gets the specified request.
  144. /// </summary>
  145. /// <param name="request">The request.</param>
  146. /// <returns>IEnumerable{TaskInfo}.</returns>
  147. /// <exception cref="ResourceNotFoundException">Task not found</exception>
  148. public object Get(GetScheduledTask request)
  149. {
  150. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  151. if (task == null)
  152. {
  153. throw new ResourceNotFoundException("Task not found");
  154. }
  155. var result = ScheduledTaskHelpers.GetTaskInfo(task);
  156. return ToOptimizedResult(result);
  157. }
  158. /// <summary>
  159. /// Posts the specified request.
  160. /// </summary>
  161. /// <param name="request">The request.</param>
  162. /// <exception cref="ResourceNotFoundException">Task not found</exception>
  163. public void Post(StartScheduledTask request)
  164. {
  165. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  166. if (task == null)
  167. {
  168. throw new ResourceNotFoundException("Task not found");
  169. }
  170. if (string.Equals(task.ScheduledTask.Key, "SystemUpdateTask", StringComparison.OrdinalIgnoreCase))
  171. {
  172. // This is a hack for now just to get the update application function to work when auto-update is disabled
  173. if (!_config.Configuration.EnableAutoUpdate)
  174. {
  175. _config.Configuration.EnableAutoUpdate = true;
  176. _config.SaveConfiguration();
  177. }
  178. }
  179. TaskManager.Execute(task, new TaskOptions());
  180. }
  181. /// <summary>
  182. /// Posts the specified request.
  183. /// </summary>
  184. /// <param name="request">The request.</param>
  185. /// <exception cref="ResourceNotFoundException">Task not found</exception>
  186. public void Delete(StopScheduledTask request)
  187. {
  188. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, request.Id));
  189. if (task == null)
  190. {
  191. throw new ResourceNotFoundException("Task not found");
  192. }
  193. TaskManager.Cancel(task);
  194. }
  195. /// <summary>
  196. /// Posts the specified request.
  197. /// </summary>
  198. /// <param name="request">The request.</param>
  199. /// <exception cref="ResourceNotFoundException">Task not found</exception>
  200. public void Post(UpdateScheduledTaskTriggers request)
  201. {
  202. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  203. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  204. var id = GetPathValue(1);
  205. var task = TaskManager.ScheduledTasks.FirstOrDefault(i => string.Equals(i.Id, id));
  206. if (task == null)
  207. {
  208. throw new ResourceNotFoundException("Task not found");
  209. }
  210. var triggerInfos = request;
  211. task.Triggers = triggerInfos.ToArray();
  212. }
  213. }
  214. }