Updates BackgroundTaskRunner to ensure canceled or skipped tasks are disposed, updated tests to reflect

This commit is contained in:
Shannon
2015-02-04 15:12:37 +11:00
parent 912b01c9aa
commit bc068b201d
4 changed files with 43 additions and 29 deletions

View File

@@ -273,22 +273,25 @@ namespace Umbraco.Tests.Scheduling
{
}
public override void Run()
public override void PerformRun()
{
Thread.Sleep(500);
}
public override void Cancel()
{
}
}
public abstract class BaseTask : IBackgroundTask
{
public bool WasCancelled { get; set; }
public Guid UniqueId { get; protected set; }
public abstract void Run();
public abstract void PerformRun();
public void Run()
{
PerformRun();
Ended = DateTime.Now;
}
public Task RunAsync()
{
throw new NotImplementedException();
@@ -299,7 +302,10 @@ namespace Umbraco.Tests.Scheduling
get { return false; }
}
public abstract void Cancel();
public virtual void Cancel()
{
WasCancelled = true;
}
public DateTime Queued { get; set; }
public DateTime Started { get; set; }
@@ -307,7 +313,7 @@ namespace Umbraco.Tests.Scheduling
public virtual void Dispose()
{
Ended = DateTime.Now;
}
}

View File

@@ -8,26 +8,6 @@ using Umbraco.Core.Logging;
namespace Umbraco.Web.Scheduling
{
internal class BackgroundTaskRunnerOptions
{
public BackgroundTaskRunnerOptions()
{
DedicatedThread = false;
PersistentThread = false;
OnlyProcessLastItem = false;
}
public bool DedicatedThread { get; set; }
public bool PersistentThread { get; set; }
/// <summary>
/// If this is true, the task runner will skip over all items and only process the last/final
/// item registered
/// </summary>
public bool OnlyProcessLastItem { get; set; }
}
/// <summary>
/// This is used to create a background task runner which will stay alive in the background of and complete
/// any tasks that are queued. It is web aware and will ensure that it is shutdown correctly when the app domain
@@ -174,7 +154,8 @@ namespace Umbraco.Web.Scheduling
//skip if this is not the last
if (_options.OnlyProcessLastItem && _tasks.Count > 0)
{
//NOTE: don't raise canceled event, we're shutting down
//NOTE: don't raise canceled event, we're shutting down, just dispose
remainingTask.Dispose();
continue;
}
@@ -372,6 +353,9 @@ namespace Umbraco.Web.Scheduling
{
var handler = TaskCancelled;
if (handler != null) handler(this, e);
//dispose it
e.Task.Dispose();
}

View File

@@ -0,0 +1,23 @@
namespace Umbraco.Web.Scheduling
{
internal class BackgroundTaskRunnerOptions
{
//TODO: Could add options for using a stack vs queue if required
public BackgroundTaskRunnerOptions()
{
DedicatedThread = false;
PersistentThread = false;
OnlyProcessLastItem = false;
}
public bool DedicatedThread { get; set; }
public bool PersistentThread { get; set; }
/// <summary>
/// If this is true, the task runner will skip over all items and only process the last/final
/// item registered
/// </summary>
public bool OnlyProcessLastItem { get; set; }
}
}

View File

@@ -498,6 +498,7 @@
<Compile Include="Mvc\UmbracoVirtualNodeRouteHandler.cs" />
<Compile Include="Routing\CustomRouteUrlProvider.cs" />
<Compile Include="Routing\UrlProviderExtensions.cs" />
<Compile Include="Scheduling\BackgroundTaskRunnerOptions.cs" />
<Compile Include="Strategies\Migrations\ClearCsrfCookiesAfterUpgrade.cs" />
<Compile Include="Strategies\Migrations\ClearMediaXmlCacheForDeletedItemsAfterUpgrade.cs" />
<Compile Include="Strategies\Migrations\EnsureListViewDataTypeIsCreated.cs" />