diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2015-02-08 13:21:24 -0500 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2015-02-08 13:21:24 -0500 |
| commit | ac68e0ba413e833ede14034cf3367e33b94b9eb3 (patch) | |
| tree | 3425515d8a478c51c292a2f2fd83b81f001f4172 /MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs | |
| parent | 067b18d067563c9ddde8910a8911be8d787abfe7 (diff) | |
sync updates
Diffstat (limited to 'MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs')
| -rw-r--r-- | MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs index b09cb7105..4d6cc1608 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs @@ -335,6 +335,8 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks trigger.Start(false); } + private Task _currentTask; + /// <summary> /// Executes the task /// </summary> @@ -342,6 +344,22 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks /// <exception cref="System.InvalidOperationException">Cannot execute a Task that is already running</exception> public async Task Execute() { + var task = ExecuteInternal(); + + _currentTask = task; + + try + { + await task.ConfigureAwait(false); + } + finally + { + _currentTask = null; + } + } + + private async Task ExecuteInternal() + { // Cancel the current execution, if any if (CurrentCancellationTokenSource != null) { @@ -585,14 +603,60 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks { DisposeTriggers(); - if (State == TaskState.Running) + var wassRunning = State == TaskState.Running; + var startTime = CurrentExecutionStartTime; + + var token = CurrentCancellationTokenSource; + if (token != null) + { + try + { + Logger.Debug(Name + ": Cancelling"); + token.Cancel(); + } + catch (Exception ex) + { + Logger.ErrorException("Error calling CancellationToken.Cancel();", ex); + } + } + var task = _currentTask; + if (task != null) { - OnTaskCompleted(CurrentExecutionStartTime, DateTime.UtcNow, TaskCompletionStatus.Aborted, null); + try + { + Logger.Debug(Name + ": Waiting on Task"); + var exited = Task.WaitAll(new[] { task }, 2000); + + if (exited) + { + Logger.Debug(Name + ": Task exited"); + } + else + { + Logger.Debug(Name + ": Timed out waiting for task to stop"); + } + } + catch (Exception ex) + { + Logger.ErrorException("Error calling Task.WaitAll();", ex); + } } - if (CurrentCancellationTokenSource != null) + if (token != null) + { + try + { + Logger.Debug(Name + ": Disposing CancellationToken"); + token.Dispose(); + } + catch (Exception ex) + { + Logger.ErrorException("Error calling CancellationToken.Dispose();", ex); + } + } + if (wassRunning) { - CurrentCancellationTokenSource.Dispose(); + OnTaskCompleted(startTime, DateTime.UtcNow, TaskCompletionStatus.Aborted, null); } } } |
