TaskManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Model.Serialization;
  5. using MediaBrowser.Model.Tasks;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace MediaBrowser.Common.Implementations.ScheduledTasks
  10. {
  11. /// <summary>
  12. /// Class TaskManager
  13. /// </summary>
  14. public class TaskManager : ITaskManager
  15. {
  16. /// <summary>
  17. /// Gets the list of Scheduled Tasks
  18. /// </summary>
  19. /// <value>The scheduled tasks.</value>
  20. public IScheduledTaskWorker[] ScheduledTasks { get; private set; }
  21. /// <summary>
  22. /// The _task queue
  23. /// </summary>
  24. private readonly List<Type> _taskQueue = new List<Type>();
  25. /// <summary>
  26. /// Gets or sets the json serializer.
  27. /// </summary>
  28. /// <value>The json serializer.</value>
  29. private IJsonSerializer JsonSerializer { get; set; }
  30. /// <summary>
  31. /// Gets or sets the application paths.
  32. /// </summary>
  33. /// <value>The application paths.</value>
  34. private IApplicationPaths ApplicationPaths { get; set; }
  35. /// <summary>
  36. /// Gets the logger.
  37. /// </summary>
  38. /// <value>The logger.</value>
  39. private ILogger Logger { get; set; }
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="TaskManager" /> class.
  42. /// </summary>
  43. /// <param name="applicationPaths">The application paths.</param>
  44. /// <param name="jsonSerializer">The json serializer.</param>
  45. /// <param name="logger">The logger.</param>
  46. /// <exception cref="System.ArgumentException">kernel</exception>
  47. public TaskManager(IApplicationPaths applicationPaths, IJsonSerializer jsonSerializer, ILogger logger)
  48. {
  49. ApplicationPaths = applicationPaths;
  50. JsonSerializer = jsonSerializer;
  51. Logger = logger;
  52. ScheduledTasks = new IScheduledTaskWorker[] { };
  53. }
  54. /// <summary>
  55. /// Cancels if running and queue.
  56. /// </summary>
  57. /// <typeparam name="T"></typeparam>
  58. public void CancelIfRunningAndQueue<T>()
  59. where T : IScheduledTask
  60. {
  61. ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)).CancelIfRunning();
  62. QueueScheduledTask<T>();
  63. }
  64. /// <summary>
  65. /// Queues the scheduled task.
  66. /// </summary>
  67. /// <typeparam name="T"></typeparam>
  68. public void QueueScheduledTask<T>()
  69. where T : IScheduledTask
  70. {
  71. var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
  72. QueueScheduledTask(scheduledTask);
  73. }
  74. /// <summary>
  75. /// Queues the scheduled task.
  76. /// </summary>
  77. /// <param name="task">The task.</param>
  78. public void QueueScheduledTask(IScheduledTask task)
  79. {
  80. var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType());
  81. QueueScheduledTask(scheduledTask);
  82. }
  83. /// <summary>
  84. /// Queues the scheduled task.
  85. /// </summary>
  86. /// <param name="task">The task.</param>
  87. private void QueueScheduledTask(IScheduledTaskWorker task)
  88. {
  89. var type = task.GetType();
  90. lock (_taskQueue)
  91. {
  92. // If it's idle just execute immediately
  93. if (task.State == TaskState.Idle)
  94. {
  95. task.Execute();
  96. return;
  97. }
  98. if (!_taskQueue.Contains(type))
  99. {
  100. Logger.Info("Queueing task {0}", type.Name);
  101. _taskQueue.Add(type);
  102. }
  103. else
  104. {
  105. Logger.Info("Task already queued: {0}", type.Name);
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// Called when [task completed].
  111. /// </summary>
  112. /// <param name="task">The task.</param>
  113. public void OnTaskCompleted(IScheduledTask task)
  114. {
  115. // Execute queued tasks
  116. lock (_taskQueue)
  117. {
  118. var copy = _taskQueue.ToList();
  119. foreach (var type in copy)
  120. {
  121. var scheduledTask = ScheduledTasks.First(t => t.GetType() == type);
  122. if (scheduledTask.State == TaskState.Idle)
  123. {
  124. scheduledTask.Execute();
  125. _taskQueue.Remove(type);
  126. }
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Adds the tasks.
  132. /// </summary>
  133. /// <param name="tasks">The tasks.</param>
  134. public void AddTasks(IEnumerable<IScheduledTask> tasks)
  135. {
  136. var myTasks = ScheduledTasks.ToList();
  137. myTasks.AddRange(tasks.Select(t => new ScheduledTaskWorker(t, ApplicationPaths, this, JsonSerializer, Logger)));
  138. ScheduledTasks = myTasks.ToArray();
  139. }
  140. /// <summary>
  141. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  142. /// </summary>
  143. public void Dispose()
  144. {
  145. Dispose(true);
  146. GC.SuppressFinalize(this);
  147. }
  148. /// <summary>
  149. /// Releases unmanaged and - optionally - managed resources.
  150. /// </summary>
  151. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  152. protected virtual void Dispose(bool dispose)
  153. {
  154. foreach (var task in ScheduledTasks)
  155. {
  156. task.Dispose();
  157. }
  158. }
  159. }
  160. }