| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 | 
							- using System;
 
- using System.Collections.Concurrent;
 
- using System.Collections.Generic;
 
- using System.Linq;
 
- using System.Threading.Tasks;
 
- using MediaBrowser.Common.Configuration;
 
- using MediaBrowser.Common.Events;
 
- using MediaBrowser.Model.Events;
 
- using MediaBrowser.Model.IO;
 
- using MediaBrowser.Model.Logging;
 
- using MediaBrowser.Model.Serialization;
 
- using MediaBrowser.Model.System;
 
- using MediaBrowser.Model.Tasks;
 
- using System.IO;
 
- namespace Emby.Server.Implementations.ScheduledTasks
 
- {
 
-     /// <summary>
 
-     /// Class TaskManager
 
-     /// </summary>
 
-     public class TaskManager : ITaskManager
 
-     {
 
-         public event EventHandler<GenericEventArgs<IScheduledTaskWorker>> TaskExecuting;
 
-         public event EventHandler<TaskCompletionEventArgs> TaskCompleted;
 
-         /// <summary>
 
-         /// Gets the list of Scheduled Tasks
 
-         /// </summary>
 
-         /// <value>The scheduled tasks.</value>
 
-         public IScheduledTaskWorker[] ScheduledTasks { get; private set; }
 
-         /// <summary>
 
-         /// The _task queue
 
-         /// </summary>
 
-         private readonly ConcurrentQueue<Tuple<Type, TaskExecutionOptions>> _taskQueue =
 
-             new ConcurrentQueue<Tuple<Type, TaskExecutionOptions>>();
 
-         /// <summary>
 
-         /// Gets or sets the json serializer.
 
-         /// </summary>
 
-         /// <value>The json serializer.</value>
 
-         private IJsonSerializer JsonSerializer { get; set; }
 
-         /// <summary>
 
-         /// Gets or sets the application paths.
 
-         /// </summary>
 
-         /// <value>The application paths.</value>
 
-         private IApplicationPaths ApplicationPaths { get; set; }
 
-         private readonly ISystemEvents _systemEvents;
 
-         /// <summary>
 
-         /// Gets the logger.
 
-         /// </summary>
 
-         /// <value>The logger.</value>
 
-         private ILogger Logger { get; set; }
 
-         private readonly IFileSystem _fileSystem;
 
-         /// <summary>
 
-         /// Initializes a new instance of the <see cref="TaskManager" /> class.
 
-         /// </summary>
 
-         /// <param name="applicationPaths">The application paths.</param>
 
-         /// <param name="jsonSerializer">The json serializer.</param>
 
-         /// <param name="logger">The logger.</param>
 
-         /// <exception cref="System.ArgumentException">kernel</exception>
 
-         public TaskManager(IApplicationPaths applicationPaths, IJsonSerializer jsonSerializer, ILogger logger, IFileSystem fileSystem, ISystemEvents systemEvents)
 
-         {
 
-             ApplicationPaths = applicationPaths;
 
-             JsonSerializer = jsonSerializer;
 
-             Logger = logger;
 
-             _fileSystem = fileSystem;
 
-             _systemEvents = systemEvents;
 
-             ScheduledTasks = new IScheduledTaskWorker[] { };
 
-         }
 
-         private void BindToSystemEvent()
 
-         {
 
-             _systemEvents.Resume += _systemEvents_Resume;
 
-         }
 
-         private void _systemEvents_Resume(object sender, EventArgs e)
 
-         {
 
-             foreach (var task in ScheduledTasks)
 
-             {
 
-                 task.ReloadTriggerEvents();
 
-             }
 
-         }
 
-         public void RunTaskOnNextStartup(string key)
 
-         {
 
-             var path = Path.Combine(ApplicationPaths.CachePath, "startuptasks.txt");
 
-             List<string> lines;
 
-             try
 
-             {
 
-                 lines = _fileSystem.ReadAllLines(path).ToList() ;
 
-             }
 
-             catch
 
-             {
 
-                 lines = new List<string>();
 
-             }
 
-             if (!lines.Contains(key, StringComparer.OrdinalIgnoreCase))
 
-             {
 
-                 lines.Add(key);
 
-                 _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
 
-                 _fileSystem.WriteAllLines(path, lines);
 
-             }
 
-         }
 
-         private void RunStartupTasks()
 
-         {
 
-             var path = Path.Combine(ApplicationPaths.CachePath, "startuptasks.txt");
 
-             List<string> lines;
 
-             try
 
-             {
 
-                 lines = _fileSystem.ReadAllLines(path).Where(i => !string.IsNullOrWhiteSpace(i)).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
 
-                 foreach (var key in lines)
 
-                 {
 
-                     var task = ScheduledTasks.FirstOrDefault(i => string.Equals(i.ScheduledTask.Key, key, StringComparison.OrdinalIgnoreCase));
 
-                     if (task != null)
 
-                     {
 
-                         QueueScheduledTask(task, new TaskExecutionOptions());
 
-                     }
 
-                 }
 
-                 _fileSystem.DeleteFile(path);
 
-             }
 
-             catch
 
-             {
 
-                 return;
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Cancels if running and queue.
 
-         /// </summary>
 
-         /// <typeparam name="T"></typeparam>
 
-         /// <param name="options">Task options.</param>
 
-         public void CancelIfRunningAndQueue<T>(TaskExecutionOptions options)
 
-                  where T : IScheduledTask
 
-         {
 
-             var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
 
-             ((ScheduledTaskWorker)task).CancelIfRunning();
 
-             QueueScheduledTask<T>(options);
 
-         }
 
-         public void CancelIfRunningAndQueue<T>()
 
-                where T : IScheduledTask
 
-         {
 
-             CancelIfRunningAndQueue<T>(new TaskExecutionOptions());
 
-         }
 
-         /// <summary>
 
-         /// Cancels if running
 
-         /// </summary>
 
-         /// <typeparam name="T"></typeparam>
 
-         public void CancelIfRunning<T>()
 
-                  where T : IScheduledTask
 
-         {
 
-             var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
 
-             ((ScheduledTaskWorker)task).CancelIfRunning();
 
-         }
 
-         /// <summary>
 
-         /// Queues the scheduled task.
 
-         /// </summary>
 
-         /// <typeparam name="T"></typeparam>
 
-         /// <param name="options">Task options</param>
 
-         public void QueueScheduledTask<T>(TaskExecutionOptions options)
 
-             where T : IScheduledTask
 
-         {
 
-             var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == typeof(T));
 
-             if (scheduledTask == null)
 
-             {
 
-                 Logger.Error("Unable to find scheduled task of type {0} in QueueScheduledTask.", typeof(T).Name);
 
-             }
 
-             else
 
-             {
 
-                 QueueScheduledTask(scheduledTask, options);
 
-             }
 
-         }
 
-         public void QueueScheduledTask<T>()
 
-             where T : IScheduledTask
 
-         {
 
-             QueueScheduledTask<T>(new TaskExecutionOptions());
 
-         }
 
-         public void QueueIfNotRunning<T>()
 
-             where T : IScheduledTask
 
-         {
 
-             var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
 
-             if (task.State != TaskState.Running)
 
-             {
 
-                 QueueScheduledTask<T>(new TaskExecutionOptions());
 
-             }
 
-         }
 
-         public void Execute<T>()
 
-             where T : IScheduledTask
 
-         {
 
-             var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == typeof(T));
 
-             if (scheduledTask == null)
 
-             {
 
-                 Logger.Error("Unable to find scheduled task of type {0} in Execute.", typeof(T).Name);
 
-             }
 
-             else
 
-             {
 
-                 var type = scheduledTask.ScheduledTask.GetType();
 
-                 Logger.Info("Queueing task {0}", type.Name);
 
-                 lock (_taskQueue)
 
-                 {
 
-                     if (scheduledTask.State == TaskState.Idle)
 
-                     {
 
-                         Execute(scheduledTask, new TaskExecutionOptions());
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Queues the scheduled task.
 
-         /// </summary>
 
-         /// <param name="task">The task.</param>
 
-         /// <param name="options">The task options.</param>
 
-         public void QueueScheduledTask(IScheduledTask task, TaskExecutionOptions options)
 
-         {
 
-             var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == task.GetType());
 
-             if (scheduledTask == null)
 
-             {
 
-                 Logger.Error("Unable to find scheduled task of type {0} in QueueScheduledTask.", task.GetType().Name);
 
-             }
 
-             else
 
-             {
 
-                 QueueScheduledTask(scheduledTask, options);
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Queues the scheduled task.
 
-         /// </summary>
 
-         /// <param name="task">The task.</param>
 
-         /// <param name="options">The task options.</param>
 
-         private void QueueScheduledTask(IScheduledTaskWorker task, TaskExecutionOptions options)
 
-         {
 
-             var type = task.ScheduledTask.GetType();
 
-             Logger.Info("Queueing task {0}", type.Name);
 
-             lock (_taskQueue)
 
-             {
 
-                 if (task.State == TaskState.Idle)
 
-                 {
 
-                     Execute(task, options);
 
-                     return;
 
-                 }
 
-                 _taskQueue.Enqueue(new Tuple<Type, TaskExecutionOptions>(type, options));
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Adds the tasks.
 
-         /// </summary>
 
-         /// <param name="tasks">The tasks.</param>
 
-         public void AddTasks(IEnumerable<IScheduledTask> tasks)
 
-         {
 
-             var myTasks = ScheduledTasks.ToList();
 
-             var list = tasks.ToList();
 
-             myTasks.AddRange(list.Select(t => new ScheduledTaskWorker(t, ApplicationPaths, this, JsonSerializer, Logger, _fileSystem, _systemEvents)));
 
-             ScheduledTasks = myTasks.ToArray();
 
-             BindToSystemEvent();
 
-             RunStartupTasks();
 
-         }
 
-         /// <summary>
 
-         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 
-         /// </summary>
 
-         public void Dispose()
 
-         {
 
-             Dispose(true);
 
-             GC.SuppressFinalize(this);
 
-         }
 
-         /// <summary>
 
-         /// Releases unmanaged and - optionally - managed resources.
 
-         /// </summary>
 
-         /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 
-         protected virtual void Dispose(bool dispose)
 
-         {
 
-             foreach (var task in ScheduledTasks)
 
-             {
 
-                 task.Dispose();
 
-             }
 
-         }
 
-         public void Cancel(IScheduledTaskWorker task)
 
-         {
 
-             ((ScheduledTaskWorker)task).Cancel();
 
-         }
 
-         public Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options)
 
-         {
 
-             return ((ScheduledTaskWorker)task).Execute(options);
 
-         }
 
-         /// <summary>
 
-         /// Called when [task executing].
 
-         /// </summary>
 
-         /// <param name="task">The task.</param>
 
-         internal void OnTaskExecuting(IScheduledTaskWorker task)
 
-         {
 
-             EventHelper.FireEventIfNotNull(TaskExecuting, this, new GenericEventArgs<IScheduledTaskWorker>
 
-             {
 
-                 Argument = task
 
-             }, Logger);
 
-         }
 
-         /// <summary>
 
-         /// Called when [task completed].
 
-         /// </summary>
 
-         /// <param name="task">The task.</param>
 
-         /// <param name="result">The result.</param>
 
-         internal void OnTaskCompleted(IScheduledTaskWorker task, TaskResult result)
 
-         {
 
-             EventHelper.FireEventIfNotNull(TaskCompleted, task, new TaskCompletionEventArgs
 
-             {
 
-                 Result = result,
 
-                 Task = task
 
-             }, Logger);
 
-             ExecuteQueuedTasks();
 
-         }
 
-         /// <summary>
 
-         /// Executes the queued tasks.
 
-         /// </summary>
 
-         private void ExecuteQueuedTasks()
 
-         {
 
-             Logger.Info("ExecuteQueuedTasks");
 
-             // Execute queued tasks
 
-             lock (_taskQueue)
 
-             {
 
-                 var list = new List<Tuple<Type, TaskExecutionOptions>>();
 
-                 Tuple<Type, TaskExecutionOptions> item;
 
-                 while (_taskQueue.TryDequeue(out item))
 
-                 {
 
-                     if (list.All(i => i.Item1 != item.Item1))
 
-                     {
 
-                         list.Add(item);
 
-                     }
 
-                 }
 
-                 foreach (var enqueuedType in list)
 
-                 {
 
-                     var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == enqueuedType.Item1);
 
-                     if (scheduledTask.State == TaskState.Idle)
 
-                     {
 
-                         Execute(scheduledTask, enqueuedType.Item2);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |