aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common.Implementations/ScheduledTasks
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Common.Implementations/ScheduledTasks')
-rw-r--r--MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs18
-rw-r--r--MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs51
-rw-r--r--MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs14
3 files changed, 61 insertions, 22 deletions
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
/// </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;
}
diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs
index 5a3ac53dc..4f84652c6 100644
--- a/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs
+++ b/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs
@@ -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,20 @@ 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);
+ }
+
+ public void CancelIfRunningAndQueue<T>()
+ where T : IScheduledTask
+ {
+ CancelIfRunningAndQueue<T>(new TaskExecutionOptions());
}
/// <summary>
@@ -93,30 +100,39 @@ 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);
}
+ public void QueueScheduledTask<T>()
+ where T : IScheduledTask
+ {
+ QueueScheduledTask<T>(new TaskExecutionOptions());
+ }
+
/// <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 +141,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 +198,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 +241,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.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs
index 779f992d3..eb2b46c22 100644
--- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs
+++ b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs
@@ -118,9 +118,23 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
index++;
}
+ DeleteEmptyFolders(directory);
+
progress.Report(100);
}
+ private static void DeleteEmptyFolders(string parent)
+ {
+ foreach (var directory in Directory.GetDirectories(parent))
+ {
+ DeleteEmptyFolders(directory);
+ if (!Directory.EnumerateFileSystemEntries(directory).Any())
+ {
+ Directory.Delete(directory, false);
+ }
+ }
+ }
+
private void DeleteFile(string path)
{
try