Refactor task runner maindom hooks

This commit is contained in:
Stephan
2019-01-08 20:49:21 +01:00
parent 2422000b3e
commit 349f48aa2d

View File

@@ -4,7 +4,6 @@ using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using System.Web.Hosting;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
@@ -15,7 +14,43 @@ namespace Umbraco.Web.Scheduling
/// </summary>
/// <remarks>This class exists for logging purposes - the one you want to use is BackgroundTaskRunner{T}.</remarks>
public abstract class BackgroundTaskRunner
{ }
{
/// <summary>
/// Creates a hook, to hook the task runner into the main domain.
/// </summary>
/// <param name="mainDom">The <see cref="IMainDom"/> object.</param>
/// <param name="install">A method to execute when hooking into the main domain.</param>
/// <param name="release">A method to execute when the main domain releases.</param>
/// <returns></returns>
public MainDomHook CreateMainDomHook(IMainDom mainDom, Action install, Action release)
{
return new MainDomHook(mainDom, install, release);
}
public class MainDomHook
{
public MainDomHook(IMainDom mainDom, Action install, Action release)
{
MainDom = mainDom;
Install = install;
Release = release;
}
public IMainDom MainDom { get; }
public Action Install { get; }
public Action Release { get; }
internal bool Register()
{
if (MainDom != null)
return MainDom.Register(Install, Release);
// tests
Install?.Invoke();
return true;
}
}
}
/// <summary>
/// Manages a queue of tasks of type <typeparamref name="T"/> and runs them in the background.
@@ -55,42 +90,6 @@ namespace Umbraco.Web.Scheduling
private bool _terminated; // remember we've terminated
private readonly TaskCompletionSource<int> _terminatedSource = new TaskCompletionSource<int>(); // enable awaiting termination
// fixme - this is temp
// at the moment MainDom is internal so we have to find a way to hook into it - temp
public class MainDomHook
{
private MainDomHook(MainDom mainDom, Action install, Action release)
{
MainDom = mainDom;
Install = install;
Release = release;
}
internal MainDom MainDom { get; }
public Action Install { get; }
public Action Release { get; }
public static MainDomHook Create(Action install, Action release)
{
return new MainDomHook(Core.Composing.Current.Factory.GetInstance<MainDom>(), install, release);
}
public static MainDomHook CreateForTest(Action install, Action release)
{
return new MainDomHook(null, install, release);
}
public bool Register()
{
if (MainDom != null)
return MainDom.Register(Install, Release);
// tests
Install?.Invoke();
return true;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="BackgroundTaskRunner{T}"/> class.
/// </summary>
@@ -129,11 +128,9 @@ namespace Umbraco.Web.Scheduling
/// <param name="hook">An optional main domain hook.</param>
public BackgroundTaskRunner(string name, BackgroundTaskRunnerOptions options, ILogger logger, MainDomHook hook = null)
{
if (options == null) throw new ArgumentNullException(nameof(options));
if (logger == null) throw new ArgumentNullException(nameof(logger));
_options = options;
_options = options ?? throw new ArgumentNullException(nameof(options));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_logPrefix = "[" + name + "] ";
_logger = logger;
if (options.Hosted)
HostingEnvironment.RegisterObject(this);
@@ -319,7 +316,7 @@ namespace Umbraco.Web.Scheduling
}
/// <summary>
/// Shuts the taks runner down.
/// Shuts the tasks runner down.
/// </summary>
/// <param name="force">True for force the runner to stop.</param>
/// <param name="wait">True to wait until the runner has stopped.</param>
@@ -355,7 +352,7 @@ namespace Umbraco.Web.Scheduling
// tasks in the queue will be executed...
if (wait == false) return;
_runningTask?.Wait(); // wait for whatever is running to end...
_runningTask?.Wait(CancellationToken.None); // wait for whatever is running to end...
}
private async Task Pump()
@@ -648,13 +645,13 @@ namespace Umbraco.Web.Scheduling
#endregion
/// <summary>
/// Requests a registered object to unregister.
/// Requests a registered object to un-register.
/// </summary>
/// <param name="immediate">true to indicate the registered object should unregister from the hosting
/// <param name="immediate">true to indicate the registered object should un-register from the hosting
/// environment before returning; otherwise, false.</param>
/// <remarks>
/// <para>"When the application manager needs to stop a registered object, it will call the Stop method."</para>
/// <para>The application manager will call the Stop method to ask a registered object to unregister. During
/// <para>The application manager will call the Stop method to ask a registered object to un-register. During
/// processing of the Stop method, the registered object must call the HostingEnvironment.UnregisterObject method.</para>
/// </remarks>
public void Stop(bool immediate)