Prototype 2 of max task length for scheduled tasks.
This commit is contained in:
parent
17ea0217f5
commit
5b9999eccf
|
@ -313,7 +313,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|||
/// </summary>
|
||||
/// <param name="sender">The source of the event.</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;
|
||||
|
||||
|
@ -340,11 +340,12 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|||
/// <summary>
|
||||
/// Executes the task
|
||||
/// </summary>
|
||||
/// <param name="options">Task options.</param>
|
||||
/// <returns>Task.</returns>
|
||||
/// <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;
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|||
/// <summary>
|
||||
/// The _task queue
|
||||
/// </summary>
|
||||
private readonly List<Type> _taskQueue = new List<Type>();
|
||||
private readonly SortedDictionary<Type, TaskExecutionOptions> _taskQueue = new SortedDictionary<Type, TaskExecutionOptions>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the json serializer.
|
||||
|
@ -69,13 +69,14 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|||
/// Cancels if running and queue.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public void CancelIfRunningAndQueue<T>()
|
||||
/// <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>();
|
||||
QueueScheduledTask<T>(options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -93,30 +94,33 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
|
|||
/// Queues the scheduled task.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public void QueueScheduledTask<T>()
|
||||
/// <param name="options">Task options</param>
|
||||
public void QueueScheduledTask<T>(TaskExecutionOptions options)
|
||||
where T : IScheduledTask
|
||||
{
|
||||
var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T));
|
||||
|
||||
QueueScheduledTask(scheduledTask);
|
||||
QueueScheduledTask(scheduledTask, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues the scheduled task.
|
||||
/// </summary>
|
||||
/// <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());
|
||||
|
||||
QueueScheduledTask(scheduledTask);
|
||||
QueueScheduledTask(scheduledTask, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues the scheduled task.
|
||||
/// </summary>
|
||||
/// <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();
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -86,6 +86,7 @@
|
|||
<Compile Include="ScheduledTasks\DailyTrigger.cs" />
|
||||
<Compile Include="ScheduledTasks\IntervalTrigger.cs" />
|
||||
<Compile Include="ScheduledTasks\TaskCompletionEventArgs.cs" />
|
||||
<Compile Include="ScheduledTasks\TaskExecutionOptions.cs" />
|
||||
<Compile Include="ScheduledTasks\WeeklyTrigger.cs" />
|
||||
<Compile Include="Security\IRequiresRegistration.cs" />
|
||||
<Compile Include="Security\ISecurityManager.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
|
|||
/// <value>The timer.</value>
|
||||
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>
|
||||
/// Stars waiting for the trigger action
|
||||
/// </summary>
|
||||
|
@ -58,7 +67,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// <summary>
|
||||
/// Occurs when [triggered].
|
||||
/// </summary>
|
||||
public event EventHandler<EventArgs> Triggered;
|
||||
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
|
||||
|
||||
/// <summary>
|
||||
/// Called when [triggered].
|
||||
|
@ -67,7 +76,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
{
|
||||
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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
void CancelIfRunningAndQueue<T>()
|
||||
/// <param name="options">Task options.</param>
|
||||
void CancelIfRunningAndQueue<T>(TaskExecutionOptions options = null)
|
||||
where T : IScheduledTask;
|
||||
|
||||
/// <summary>
|
||||
|
@ -31,14 +32,16 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// Queues the scheduled task.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
void QueueScheduledTask<T>()
|
||||
/// <param name="options">Task options.</param>
|
||||
void QueueScheduledTask<T>(TaskExecutionOptions options = null)
|
||||
where T : IScheduledTask;
|
||||
|
||||
/// <summary>
|
||||
/// Queues the scheduled task.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// Adds the tasks.
|
||||
|
@ -47,7 +50,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
void AddTasks(IEnumerable<IScheduledTask> tasks);
|
||||
|
||||
void Cancel(IScheduledTaskWorker task);
|
||||
Task Execute(IScheduledTaskWorker task);
|
||||
Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options = null);
|
||||
|
||||
event EventHandler<GenericEventArgs<IScheduledTaskWorker>> TaskExecuting;
|
||||
event EventHandler<TaskCompletionEventArgs> TaskCompleted;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using MediaBrowser.Model.Events;
|
||||
|
||||
namespace MediaBrowser.Common.ScheduledTasks
|
||||
{
|
||||
|
@ -10,7 +11,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// <summary>
|
||||
/// Fires when the trigger condition is satisfied and the task should run
|
||||
/// </summary>
|
||||
event EventHandler<EventArgs> Triggered;
|
||||
event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
|
||||
|
||||
/// <summary>
|
||||
/// Stars waiting for the trigger action
|
||||
|
@ -22,5 +23,13 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// Stops waiting for the trigger action
|
||||
/// </summary>
|
||||
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.Threading;
|
||||
using MediaBrowser.Model.Events;
|
||||
|
||||
namespace MediaBrowser.Common.ScheduledTasks
|
||||
{
|
||||
|
@ -20,6 +21,14 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// <value>The timer.</value>
|
||||
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>
|
||||
/// Stars waiting for the trigger action
|
||||
/// </summary>
|
||||
|
@ -53,7 +62,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// <summary>
|
||||
/// Occurs when [triggered].
|
||||
/// </summary>
|
||||
public event EventHandler<EventArgs> Triggered;
|
||||
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
|
||||
|
||||
/// <summary>
|
||||
/// Called when [triggered].
|
||||
|
@ -62,7 +71,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
{
|
||||
if (Triggered != null)
|
||||
{
|
||||
Triggered(this, EventArgs.Empty);
|
||||
Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the execution properties of this task.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The execution properties of this task.
|
||||
/// </value>
|
||||
public TaskExecutionOptions TaskOptions { get; set; }
|
||||
|
||||
public StartupTrigger()
|
||||
{
|
||||
DelayMs = 3000;
|
||||
|
@ -39,7 +48,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// <summary>
|
||||
/// Occurs when [triggered].
|
||||
/// </summary>
|
||||
public event EventHandler<EventArgs> Triggered;
|
||||
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
|
||||
|
||||
/// <summary>
|
||||
/// Called when [triggered].
|
||||
|
@ -48,7 +57,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
{
|
||||
if (Triggered != null)
|
||||
{
|
||||
Triggered(this, EventArgs.Empty);
|
||||
Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
|||
/// <value>The system event.</value>
|
||||
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>
|
||||
/// Stars waiting for the trigger action
|
||||
/// </summary>
|
||||
|
@ -57,7 +66,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// <summary>
|
||||
/// Occurs when [triggered].
|
||||
/// </summary>
|
||||
public event EventHandler<EventArgs> Triggered;
|
||||
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
|
||||
|
||||
/// <summary>
|
||||
/// Called when [triggered].
|
||||
|
@ -66,7 +75,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
{
|
||||
if (Triggered != null)
|
||||
{
|
||||
Triggered(this, EventArgs.Empty);
|
||||
Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
16
MediaBrowser.Common/ScheduledTasks/TaskExecutionOptions.cs
Normal file
16
MediaBrowser.Common/ScheduledTasks/TaskExecutionOptions.cs
Normal file
|
@ -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.Threading;
|
||||
using MediaBrowser.Model.Events;
|
||||
|
||||
namespace MediaBrowser.Common.ScheduledTasks
|
||||
{
|
||||
|
@ -20,6 +21,14 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// <value>The day of week.</value>
|
||||
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>
|
||||
/// Gets or sets the timer.
|
||||
/// </summary>
|
||||
|
@ -88,7 +97,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
/// <summary>
|
||||
/// Occurs when [triggered].
|
||||
/// </summary>
|
||||
public event EventHandler<EventArgs> Triggered;
|
||||
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
|
||||
|
||||
/// <summary>
|
||||
/// Called when [triggered].
|
||||
|
@ -97,7 +106,7 @@ namespace MediaBrowser.Common.ScheduledTasks
|
|||
{
|
||||
if (Triggered != null)
|
||||
{
|
||||
Triggered(this, EventArgs.Empty);
|
||||
Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user