diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs index 4d6cc1608..78dcea493 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs @@ -313,7 +313,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks /// /// The source of the event. /// The instance containing the event data. - async void trigger_Triggered(object sender, EventArgs e) + async void trigger_Triggered(object sender, GenericEventArgs e) { var trigger = (ITaskTrigger)sender; @@ -340,11 +340,12 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks /// /// Executes the task /// + /// Task options. /// Task. /// Cannot execute a Task that is already running - public async Task Execute() + public async Task Execute(TaskExecutionOptions options) { - var task = ExecuteInternal(); + var task = ExecuteInternal(options); _currentTask = task; @@ -358,7 +359,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks } } - private async Task ExecuteInternal() + private async Task ExecuteInternal(TaskExecutionOptions options) { // Cancel the current execution, if any if (CurrentCancellationTokenSource != null) @@ -383,7 +384,14 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks try { - await ExecuteTask(CurrentCancellationTokenSource.Token, progress).ConfigureAwait(false); + var localTask = ScheduledTask.Execute(CurrentCancellationTokenSource.Token, progress); + + if (options != null && options.MaxRuntimeMs.HasValue) + { + CurrentCancellationTokenSource.CancelAfter(options.MaxRuntimeMs.Value); + } + + await localTask.ConfigureAwait(false); status = TaskCompletionStatus.Completed; } diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs index 5a3ac53dc..a77efe855 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs @@ -29,7 +29,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks /// /// The _task queue /// - private readonly List _taskQueue = new List(); + private readonly SortedDictionary _taskQueue = new SortedDictionary(); /// /// Gets or sets the json serializer. @@ -69,13 +69,14 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks /// Cancels if running and queue. /// /// - public void CancelIfRunningAndQueue() + /// Task options. + public void CancelIfRunningAndQueue(TaskExecutionOptions options) where T : IScheduledTask { var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)); ((ScheduledTaskWorker)task).CancelIfRunning(); - QueueScheduledTask(); + QueueScheduledTask(options); } /// @@ -93,30 +94,33 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks /// Queues the scheduled task. /// /// - public void QueueScheduledTask() + /// Task options + public void QueueScheduledTask(TaskExecutionOptions options) where T : IScheduledTask { var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)); - QueueScheduledTask(scheduledTask); + QueueScheduledTask(scheduledTask, options); } /// /// Queues the scheduled task. /// /// The task. - public void QueueScheduledTask(IScheduledTask task) + /// The task options. + public void QueueScheduledTask(IScheduledTask task, TaskExecutionOptions options) { var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType()); - QueueScheduledTask(scheduledTask); + QueueScheduledTask(scheduledTask, options); } /// /// Queues the scheduled task. /// /// The task. - private void QueueScheduledTask(IScheduledTaskWorker task) + /// The task options. + private void QueueScheduledTask(IScheduledTaskWorker task, TaskExecutionOptions options) { var type = task.ScheduledTask.GetType(); @@ -125,17 +129,18 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks // If it's idle just execute immediately if (task.State == TaskState.Idle) { - Execute(task); + Execute(task, options); return; } - if (!_taskQueue.Contains(type)) + if (!_taskQueue.ContainsKey(type)) { Logger.Info("Queueing task {0}", type.Name); - _taskQueue.Add(type); + _taskQueue.Add(type, options); } else { + _taskQueue[type] = options; Logger.Info("Task already queued: {0}", type.Name); } } @@ -181,9 +186,9 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks ((ScheduledTaskWorker)task).Cancel(); } - public Task Execute(IScheduledTaskWorker task) + public Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options) { - return ((ScheduledTaskWorker)task).Execute(); + return ((ScheduledTaskWorker)task).Execute(options); } /// @@ -224,15 +229,15 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks // Execute queued tasks lock (_taskQueue) { - foreach (var type in _taskQueue.ToList()) + foreach (var enqueuedType in _taskQueue.ToList()) { - var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == type); + var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == enqueuedType.Key); if (scheduledTask.State == TaskState.Idle) { - Execute(scheduledTask); + Execute(scheduledTask, enqueuedType.Value); - _taskQueue.Remove(type); + _taskQueue.Remove(enqueuedType.Key); } } } diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index c46dd4a4f..ad0c0cecd 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -86,6 +86,7 @@ + diff --git a/MediaBrowser.Common/ScheduledTasks/DailyTrigger.cs b/MediaBrowser.Common/ScheduledTasks/DailyTrigger.cs index bfecadee7..2f935607b 100644 --- a/MediaBrowser.Common/ScheduledTasks/DailyTrigger.cs +++ b/MediaBrowser.Common/ScheduledTasks/DailyTrigger.cs @@ -1,5 +1,6 @@ using System; using System.Threading; +using MediaBrowser.Model.Events; namespace MediaBrowser.Common.ScheduledTasks { @@ -20,6 +21,14 @@ namespace MediaBrowser.Common.ScheduledTasks /// The timer. private Timer Timer { get; set; } + /// + /// Gets the execution properties of this task. + /// + /// + /// The execution properties of this task. + /// + public TaskExecutionOptions TaskOptions { get; set; } + /// /// Stars waiting for the trigger action /// @@ -58,7 +67,7 @@ namespace MediaBrowser.Common.ScheduledTasks /// /// Occurs when [triggered]. /// - public event EventHandler Triggered; + public event EventHandler> Triggered; /// /// Called when [triggered]. @@ -67,7 +76,7 @@ namespace MediaBrowser.Common.ScheduledTasks { if (Triggered != null) { - Triggered(this, EventArgs.Empty); + Triggered(this, new GenericEventArgs(TaskOptions)); } } } diff --git a/MediaBrowser.Common/ScheduledTasks/ITaskManager.cs b/MediaBrowser.Common/ScheduledTasks/ITaskManager.cs index 01dc355c3..9b3f18274 100644 --- a/MediaBrowser.Common/ScheduledTasks/ITaskManager.cs +++ b/MediaBrowser.Common/ScheduledTasks/ITaskManager.cs @@ -17,7 +17,8 @@ namespace MediaBrowser.Common.ScheduledTasks /// Cancels if running and queue. /// /// - void CancelIfRunningAndQueue() + /// Task options. + void CancelIfRunningAndQueue(TaskExecutionOptions options = null) where T : IScheduledTask; /// @@ -31,14 +32,16 @@ namespace MediaBrowser.Common.ScheduledTasks /// Queues the scheduled task. /// /// - void QueueScheduledTask() + /// Task options. + void QueueScheduledTask(TaskExecutionOptions options = null) where T : IScheduledTask; /// /// Queues the scheduled task. /// /// The task. - void QueueScheduledTask(IScheduledTask task); + /// The task run options. + void QueueScheduledTask(IScheduledTask task, TaskExecutionOptions options = null); /// /// Adds the tasks. @@ -47,7 +50,7 @@ namespace MediaBrowser.Common.ScheduledTasks void AddTasks(IEnumerable tasks); void Cancel(IScheduledTaskWorker task); - Task Execute(IScheduledTaskWorker task); + Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options = null); event EventHandler> TaskExecuting; event EventHandler TaskCompleted; diff --git a/MediaBrowser.Common/ScheduledTasks/ITaskTrigger.cs b/MediaBrowser.Common/ScheduledTasks/ITaskTrigger.cs index 66701650e..d30111316 100644 --- a/MediaBrowser.Common/ScheduledTasks/ITaskTrigger.cs +++ b/MediaBrowser.Common/ScheduledTasks/ITaskTrigger.cs @@ -1,4 +1,5 @@ using System; +using MediaBrowser.Model.Events; namespace MediaBrowser.Common.ScheduledTasks { @@ -10,7 +11,7 @@ namespace MediaBrowser.Common.ScheduledTasks /// /// Fires when the trigger condition is satisfied and the task should run /// - event EventHandler Triggered; + event EventHandler> Triggered; /// /// Stars waiting for the trigger action @@ -22,5 +23,13 @@ namespace MediaBrowser.Common.ScheduledTasks /// Stops waiting for the trigger action /// void Stop(); + + /// + /// Gets or sets the execution properties of this task. + /// + /// + /// The execution properties of this task. + /// + TaskExecutionOptions TaskOptions { get; set; } } } \ No newline at end of file diff --git a/MediaBrowser.Common/ScheduledTasks/IntervalTrigger.cs b/MediaBrowser.Common/ScheduledTasks/IntervalTrigger.cs index cac6d1fa3..455a70d7e 100644 --- a/MediaBrowser.Common/ScheduledTasks/IntervalTrigger.cs +++ b/MediaBrowser.Common/ScheduledTasks/IntervalTrigger.cs @@ -1,5 +1,6 @@ using System; using System.Threading; +using MediaBrowser.Model.Events; namespace MediaBrowser.Common.ScheduledTasks { @@ -20,6 +21,14 @@ namespace MediaBrowser.Common.ScheduledTasks /// The timer. private Timer Timer { get; set; } + /// + /// Gets the execution properties of this task. + /// + /// + /// The execution properties of this task. + /// + public TaskExecutionOptions TaskOptions { get; set; } + /// /// Stars waiting for the trigger action /// @@ -53,7 +62,7 @@ namespace MediaBrowser.Common.ScheduledTasks /// /// Occurs when [triggered]. /// - public event EventHandler Triggered; + public event EventHandler> Triggered; /// /// Called when [triggered]. @@ -62,7 +71,7 @@ namespace MediaBrowser.Common.ScheduledTasks { if (Triggered != null) { - Triggered(this, EventArgs.Empty); + Triggered(this, new GenericEventArgs(TaskOptions)); } } } diff --git a/MediaBrowser.Common/ScheduledTasks/StartupTrigger.cs b/MediaBrowser.Common/ScheduledTasks/StartupTrigger.cs index 17e4628d0..a58fa22b9 100644 --- a/MediaBrowser.Common/ScheduledTasks/StartupTrigger.cs +++ b/MediaBrowser.Common/ScheduledTasks/StartupTrigger.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using MediaBrowser.Model.Events; namespace MediaBrowser.Common.ScheduledTasks { @@ -10,6 +11,14 @@ namespace MediaBrowser.Common.ScheduledTasks { public int DelayMs { get; set; } + /// + /// Gets the execution properties of this task. + /// + /// + /// The execution properties of this task. + /// + public TaskExecutionOptions TaskOptions { get; set; } + public StartupTrigger() { DelayMs = 3000; @@ -39,7 +48,7 @@ namespace MediaBrowser.Common.ScheduledTasks /// /// Occurs when [triggered]. /// - public event EventHandler Triggered; + public event EventHandler> Triggered; /// /// Called when [triggered]. @@ -48,7 +57,7 @@ namespace MediaBrowser.Common.ScheduledTasks { if (Triggered != null) { - Triggered(this, EventArgs.Empty); + Triggered(this, new GenericEventArgs(TaskOptions)); } } } diff --git a/MediaBrowser.Common/ScheduledTasks/SystemEventTrigger.cs b/MediaBrowser.Common/ScheduledTasks/SystemEventTrigger.cs index 7f86a5a2f..a40dc6b5c 100644 --- a/MediaBrowser.Common/ScheduledTasks/SystemEventTrigger.cs +++ b/MediaBrowser.Common/ScheduledTasks/SystemEventTrigger.cs @@ -2,6 +2,7 @@ using Microsoft.Win32; using System; using System.Threading.Tasks; +using MediaBrowser.Model.Events; namespace MediaBrowser.Common.ScheduledTasks { @@ -16,6 +17,14 @@ namespace MediaBrowser.Common.ScheduledTasks /// The system event. public SystemEvent SystemEvent { get; set; } + /// + /// Gets the execution properties of this task. + /// + /// + /// The execution properties of this task. + /// + public TaskExecutionOptions TaskOptions { get; set; } + /// /// Stars waiting for the trigger action /// @@ -57,7 +66,7 @@ namespace MediaBrowser.Common.ScheduledTasks /// /// Occurs when [triggered]. /// - public event EventHandler Triggered; + public event EventHandler> Triggered; /// /// Called when [triggered]. @@ -66,7 +75,7 @@ namespace MediaBrowser.Common.ScheduledTasks { if (Triggered != null) { - Triggered(this, EventArgs.Empty); + Triggered(this, new GenericEventArgs(TaskOptions)); } } } diff --git a/MediaBrowser.Common/ScheduledTasks/TaskExecutionOptions.cs b/MediaBrowser.Common/ScheduledTasks/TaskExecutionOptions.cs new file mode 100644 index 000000000..830e55f08 --- /dev/null +++ b/MediaBrowser.Common/ScheduledTasks/TaskExecutionOptions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.ScheduledTasks +{ + /// + /// A class that encomposases all common task run properties. + /// + public class TaskExecutionOptions + { + public int? MaxRuntimeMs { get; set; } + } +} diff --git a/MediaBrowser.Common/ScheduledTasks/WeeklyTrigger.cs b/MediaBrowser.Common/ScheduledTasks/WeeklyTrigger.cs index cfb3f1fab..a3818f83f 100644 --- a/MediaBrowser.Common/ScheduledTasks/WeeklyTrigger.cs +++ b/MediaBrowser.Common/ScheduledTasks/WeeklyTrigger.cs @@ -1,5 +1,6 @@ using System; using System.Threading; +using MediaBrowser.Model.Events; namespace MediaBrowser.Common.ScheduledTasks { @@ -20,6 +21,14 @@ namespace MediaBrowser.Common.ScheduledTasks /// The day of week. public DayOfWeek DayOfWeek { get; set; } + /// + /// Gets the execution properties of this task. + /// + /// + /// The execution properties of this task. + /// + public TaskExecutionOptions TaskOptions { get; set; } + /// /// Gets or sets the timer. /// @@ -88,7 +97,7 @@ namespace MediaBrowser.Common.ScheduledTasks /// /// Occurs when [triggered]. /// - public event EventHandler Triggered; + public event EventHandler> Triggered; /// /// Called when [triggered]. @@ -97,7 +106,7 @@ namespace MediaBrowser.Common.ScheduledTasks { if (Triggered != null) { - Triggered(this, EventArgs.Empty); + Triggered(this, new GenericEventArgs(TaskOptions)); } } }