pull/702/head
Luke Pulverenti 10 years ago
commit a23c23f27b

@ -313,7 +313,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
/// </summary> /// </summary>
/// <param name="sender">The source of the event.</param> /// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
async void trigger_Triggered(object sender, EventArgs e) async void trigger_Triggered(object sender, GenericEventArgs<TaskExecutionOptions> e)
{ {
var trigger = (ITaskTrigger)sender; var trigger = (ITaskTrigger)sender;
@ -340,11 +340,12 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
/// <summary> /// <summary>
/// Executes the task /// Executes the task
/// </summary> /// </summary>
/// <param name="options">Task options.</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
/// <exception cref="System.InvalidOperationException">Cannot execute a Task that is already running</exception> /// <exception cref="System.InvalidOperationException">Cannot execute a Task that is already running</exception>
public async Task Execute() public async Task Execute(TaskExecutionOptions options)
{ {
var task = ExecuteInternal(); var task = ExecuteInternal(options);
_currentTask = task; _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 // Cancel the current execution, if any
if (CurrentCancellationTokenSource != null) if (CurrentCancellationTokenSource != null)
@ -383,7 +384,14 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
try 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; status = TaskCompletionStatus.Completed;
} }

@ -29,7 +29,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
/// <summary> /// <summary>
/// The _task queue /// The _task queue
/// </summary> /// </summary>
private readonly List<Type> _taskQueue = new List<Type>(); private readonly SortedDictionary<Type, TaskExecutionOptions> _taskQueue = new SortedDictionary<Type, TaskExecutionOptions>();
/// <summary> /// <summary>
/// Gets or sets the json serializer. /// Gets or sets the json serializer.
@ -69,13 +69,14 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
/// Cancels if running and queue. /// Cancels if running and queue.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public void CancelIfRunningAndQueue<T>() /// <param name="options">Task options.</param>
public void CancelIfRunningAndQueue<T>(TaskExecutionOptions options)
where T : IScheduledTask where T : IScheduledTask
{ {
var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)); var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
((ScheduledTaskWorker)task).CancelIfRunning(); ((ScheduledTaskWorker)task).CancelIfRunning();
QueueScheduledTask<T>(); QueueScheduledTask<T>(options);
} }
/// <summary> /// <summary>
@ -93,30 +94,33 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
/// Queues the scheduled task. /// Queues the scheduled task.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public void QueueScheduledTask<T>() /// <param name="options">Task options</param>
public void QueueScheduledTask<T>(TaskExecutionOptions options)
where T : IScheduledTask where T : IScheduledTask
{ {
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)); var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
QueueScheduledTask(scheduledTask); QueueScheduledTask(scheduledTask, options);
} }
/// <summary> /// <summary>
/// Queues the scheduled task. /// Queues the scheduled task.
/// </summary> /// </summary>
/// <param name="task">The task.</param> /// <param name="task">The task.</param>
public void QueueScheduledTask(IScheduledTask task) /// <param name="options">The task options.</param>
public void QueueScheduledTask(IScheduledTask task, TaskExecutionOptions options)
{ {
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType()); var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == task.GetType());
QueueScheduledTask(scheduledTask); QueueScheduledTask(scheduledTask, options);
} }
/// <summary> /// <summary>
/// Queues the scheduled task. /// Queues the scheduled task.
/// </summary> /// </summary>
/// <param name="task">The task.</param> /// <param name="task">The task.</param>
private void QueueScheduledTask(IScheduledTaskWorker task) /// <param name="options">The task options.</param>
private void QueueScheduledTask(IScheduledTaskWorker task, TaskExecutionOptions options)
{ {
var type = task.ScheduledTask.GetType(); var type = task.ScheduledTask.GetType();
@ -125,17 +129,18 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
// If it's idle just execute immediately // If it's idle just execute immediately
if (task.State == TaskState.Idle) if (task.State == TaskState.Idle)
{ {
Execute(task); Execute(task, options);
return; return;
} }
if (!_taskQueue.Contains(type)) if (!_taskQueue.ContainsKey(type))
{ {
Logger.Info("Queueing task {0}", type.Name); Logger.Info("Queueing task {0}", type.Name);
_taskQueue.Add(type); _taskQueue.Add(type, options);
} }
else else
{ {
_taskQueue[type] = options;
Logger.Info("Task already queued: {0}", type.Name); Logger.Info("Task already queued: {0}", type.Name);
} }
} }
@ -181,9 +186,9 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
((ScheduledTaskWorker)task).Cancel(); ((ScheduledTaskWorker)task).Cancel();
} }
public Task Execute(IScheduledTaskWorker task) public Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options)
{ {
return ((ScheduledTaskWorker)task).Execute(); return ((ScheduledTaskWorker)task).Execute(options);
} }
/// <summary> /// <summary>
@ -224,15 +229,15 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
// Execute queued tasks // Execute queued tasks
lock (_taskQueue) 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) if (scheduledTask.State == TaskState.Idle)
{ {
Execute(scheduledTask); Execute(scheduledTask, enqueuedType.Value);
_taskQueue.Remove(type); _taskQueue.Remove(enqueuedType.Key);
} }
} }
} }

@ -86,6 +86,7 @@
<Compile Include="ScheduledTasks\DailyTrigger.cs" /> <Compile Include="ScheduledTasks\DailyTrigger.cs" />
<Compile Include="ScheduledTasks\IntervalTrigger.cs" /> <Compile Include="ScheduledTasks\IntervalTrigger.cs" />
<Compile Include="ScheduledTasks\TaskCompletionEventArgs.cs" /> <Compile Include="ScheduledTasks\TaskCompletionEventArgs.cs" />
<Compile Include="ScheduledTasks\TaskExecutionOptions.cs" />
<Compile Include="ScheduledTasks\WeeklyTrigger.cs" /> <Compile Include="ScheduledTasks\WeeklyTrigger.cs" />
<Compile Include="Security\IRequiresRegistration.cs" /> <Compile Include="Security\IRequiresRegistration.cs" />
<Compile Include="Security\ISecurityManager.cs" /> <Compile Include="Security\ISecurityManager.cs" />

@ -1,5 +1,6 @@
using System; using System;
using System.Threading; using System.Threading;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks namespace MediaBrowser.Common.ScheduledTasks
{ {
@ -20,6 +21,14 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <value>The timer.</value> /// <value>The timer.</value>
private Timer Timer { get; set; } private Timer Timer { get; set; }
/// <summary>
/// Gets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
public TaskExecutionOptions TaskOptions { get; set; }
/// <summary> /// <summary>
/// Stars waiting for the trigger action /// Stars waiting for the trigger action
/// </summary> /// </summary>
@ -58,7 +67,7 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary> /// <summary>
/// Occurs when [triggered]. /// Occurs when [triggered].
/// </summary> /// </summary>
public event EventHandler<EventArgs> Triggered; public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
/// <summary> /// <summary>
/// Called when [triggered]. /// Called when [triggered].
@ -67,7 +76,7 @@ namespace MediaBrowser.Common.ScheduledTasks
{ {
if (Triggered != null) if (Triggered != null)
{ {
Triggered(this, EventArgs.Empty); Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
} }
} }
} }

@ -17,7 +17,8 @@ namespace MediaBrowser.Common.ScheduledTasks
/// Cancels if running and queue. /// Cancels if running and queue.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
void CancelIfRunningAndQueue<T>() /// <param name="options">Task options.</param>
void CancelIfRunningAndQueue<T>(TaskExecutionOptions options = null)
where T : IScheduledTask; where T : IScheduledTask;
/// <summary> /// <summary>
@ -31,14 +32,16 @@ namespace MediaBrowser.Common.ScheduledTasks
/// Queues the scheduled task. /// Queues the scheduled task.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
void QueueScheduledTask<T>() /// <param name="options">Task options.</param>
void QueueScheduledTask<T>(TaskExecutionOptions options = null)
where T : IScheduledTask; where T : IScheduledTask;
/// <summary> /// <summary>
/// Queues the scheduled task. /// Queues the scheduled task.
/// </summary> /// </summary>
/// <param name="task">The task.</param> /// <param name="task">The task.</param>
void QueueScheduledTask(IScheduledTask task); /// <param name="options">The task run options.</param>
void QueueScheduledTask(IScheduledTask task, TaskExecutionOptions options = null);
/// <summary> /// <summary>
/// Adds the tasks. /// Adds the tasks.
@ -47,7 +50,7 @@ namespace MediaBrowser.Common.ScheduledTasks
void AddTasks(IEnumerable<IScheduledTask> tasks); void AddTasks(IEnumerable<IScheduledTask> tasks);
void Cancel(IScheduledTaskWorker task); void Cancel(IScheduledTaskWorker task);
Task Execute(IScheduledTaskWorker task); Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options = null);
event EventHandler<GenericEventArgs<IScheduledTaskWorker>> TaskExecuting; event EventHandler<GenericEventArgs<IScheduledTaskWorker>> TaskExecuting;
event EventHandler<TaskCompletionEventArgs> TaskCompleted; event EventHandler<TaskCompletionEventArgs> TaskCompleted;

@ -1,4 +1,5 @@
using System; using System;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks namespace MediaBrowser.Common.ScheduledTasks
{ {
@ -10,7 +11,7 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary> /// <summary>
/// Fires when the trigger condition is satisfied and the task should run /// Fires when the trigger condition is satisfied and the task should run
/// </summary> /// </summary>
event EventHandler<EventArgs> Triggered; event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
/// <summary> /// <summary>
/// Stars waiting for the trigger action /// Stars waiting for the trigger action
@ -22,5 +23,13 @@ namespace MediaBrowser.Common.ScheduledTasks
/// Stops waiting for the trigger action /// Stops waiting for the trigger action
/// </summary> /// </summary>
void Stop(); void Stop();
/// <summary>
/// Gets or sets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
TaskExecutionOptions TaskOptions { get; set; }
} }
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Threading; using System.Threading;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks namespace MediaBrowser.Common.ScheduledTasks
{ {
@ -20,6 +21,14 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <value>The timer.</value> /// <value>The timer.</value>
private Timer Timer { get; set; } private Timer Timer { get; set; }
/// <summary>
/// Gets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
public TaskExecutionOptions TaskOptions { get; set; }
/// <summary> /// <summary>
/// Stars waiting for the trigger action /// Stars waiting for the trigger action
/// </summary> /// </summary>
@ -53,7 +62,7 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary> /// <summary>
/// Occurs when [triggered]. /// Occurs when [triggered].
/// </summary> /// </summary>
public event EventHandler<EventArgs> Triggered; public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
/// <summary> /// <summary>
/// Called when [triggered]. /// Called when [triggered].
@ -62,7 +71,7 @@ namespace MediaBrowser.Common.ScheduledTasks
{ {
if (Triggered != null) if (Triggered != null)
{ {
Triggered(this, EventArgs.Empty); Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
} }
} }
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks namespace MediaBrowser.Common.ScheduledTasks
{ {
@ -10,6 +11,14 @@ namespace MediaBrowser.Common.ScheduledTasks
{ {
public int DelayMs { get; set; } public int DelayMs { get; set; }
/// <summary>
/// Gets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
public TaskExecutionOptions TaskOptions { get; set; }
public StartupTrigger() public StartupTrigger()
{ {
DelayMs = 3000; DelayMs = 3000;
@ -39,7 +48,7 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary> /// <summary>
/// Occurs when [triggered]. /// Occurs when [triggered].
/// </summary> /// </summary>
public event EventHandler<EventArgs> Triggered; public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
/// <summary> /// <summary>
/// Called when [triggered]. /// Called when [triggered].
@ -48,7 +57,7 @@ namespace MediaBrowser.Common.ScheduledTasks
{ {
if (Triggered != null) if (Triggered != null)
{ {
Triggered(this, EventArgs.Empty); Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
} }
} }
} }

@ -2,6 +2,7 @@
using Microsoft.Win32; using Microsoft.Win32;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks namespace MediaBrowser.Common.ScheduledTasks
{ {
@ -16,6 +17,14 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <value>The system event.</value> /// <value>The system event.</value>
public SystemEvent SystemEvent { get; set; } public SystemEvent SystemEvent { get; set; }
/// <summary>
/// Gets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
public TaskExecutionOptions TaskOptions { get; set; }
/// <summary> /// <summary>
/// Stars waiting for the trigger action /// Stars waiting for the trigger action
/// </summary> /// </summary>
@ -57,7 +66,7 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary> /// <summary>
/// Occurs when [triggered]. /// Occurs when [triggered].
/// </summary> /// </summary>
public event EventHandler<EventArgs> Triggered; public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
/// <summary> /// <summary>
/// Called when [triggered]. /// Called when [triggered].
@ -66,7 +75,7 @@ namespace MediaBrowser.Common.ScheduledTasks
{ {
if (Triggered != null) if (Triggered != null)
{ {
Triggered(this, EventArgs.Empty); Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
} }
} }
} }

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MediaBrowser.Common.ScheduledTasks
{
/// <summary>
/// A class that encomposases all common task run properties.
/// </summary>
public class TaskExecutionOptions
{
public int? MaxRuntimeMs { get; set; }
}
}

@ -1,5 +1,6 @@
using System; using System;
using System.Threading; using System.Threading;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks namespace MediaBrowser.Common.ScheduledTasks
{ {
@ -20,6 +21,14 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <value>The day of week.</value> /// <value>The day of week.</value>
public DayOfWeek DayOfWeek { get; set; } public DayOfWeek DayOfWeek { get; set; }
/// <summary>
/// Gets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
public TaskExecutionOptions TaskOptions { get; set; }
/// <summary> /// <summary>
/// Gets or sets the timer. /// Gets or sets the timer.
/// </summary> /// </summary>
@ -88,7 +97,7 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary> /// <summary>
/// Occurs when [triggered]. /// Occurs when [triggered].
/// </summary> /// </summary>
public event EventHandler<EventArgs> Triggered; public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
/// <summary> /// <summary>
/// Called when [triggered]. /// Called when [triggered].
@ -97,7 +106,7 @@ namespace MediaBrowser.Common.ScheduledTasks
{ {
if (Triggered != null) if (Triggered != null)
{ {
Triggered(this, EventArgs.Empty); Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
} }
} }
} }

Loading…
Cancel
Save