Publicize more IBackgroundTask plumbing

This commit is contained in:
Stephan
2017-04-26 09:07:48 +02:00
parent ca3dc030bc
commit ba489afe9a
9 changed files with 35 additions and 66 deletions

View File

@@ -5,7 +5,14 @@ using Umbraco.Core;
namespace Umbraco.Web.Scheduling
{
internal abstract class LatchedBackgroundTaskBase : DisposableObject, ILatchedBackgroundTask
/// <summary>
/// Provides a base class for latched background tasks.
/// </summary>
/// <remarks>Implement by overriding Run or RunAsync and then IsAsync accordingly,
/// depending on whether the task is implemented as a sync or async method, and then
/// optionnally overriding RunsOnShutdown, to indicate whether the latched task should run
/// immediately on shutdown, or just be abandonned (default).</remarks>
public abstract class LatchedBackgroundTaskBase : DisposableObject, ILatchedBackgroundTask
{
private TaskCompletionSource<bool> _latch;
@@ -17,12 +24,18 @@ namespace Umbraco.Web.Scheduling
/// <summary>
/// Implements IBackgroundTask.Run().
/// </summary>
public abstract void Run();
public virtual void Run()
{
throw new NotSupportedException("This task cannot run synchronously.");
}
/// <summary>
/// Implements IBackgroundTask.RunAsync().
/// </summary>
public abstract Task RunAsync(CancellationToken token);
public virtual Task RunAsync(CancellationToken token)
{
throw new NotSupportedException("This task cannot run asynchronously.");
}
/// <summary>
/// Indicates whether the background task can run asynchronously.
@@ -49,7 +62,7 @@ namespace Umbraco.Web.Scheduling
_latch = new TaskCompletionSource<bool>();
}
public abstract bool RunsOnShutdown { get; }
public virtual bool RunsOnShutdown { get { return false; } }
// the task is going to be disposed after execution,
// unless it is latched again, thus indicating it wants to