From 349f48aa2dcb65a3619aaa21130005519fb54444 Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 8 Jan 2019 20:49:21 +0100 Subject: [PATCH] Refactor task runner maindom hooks --- .../Scheduling/BackgroundTaskRunner.cs | 91 +++++++++---------- 1 file changed, 44 insertions(+), 47 deletions(-) diff --git a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs index a73734fab8..528c87b2bb 100644 --- a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs +++ b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs @@ -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 /// /// This class exists for logging purposes - the one you want to use is BackgroundTaskRunner{T}. public abstract class BackgroundTaskRunner - { } + { + /// + /// Creates a hook, to hook the task runner into the main domain. + /// + /// The object. + /// A method to execute when hooking into the main domain. + /// A method to execute when the main domain releases. + /// + 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; + } + } + } /// /// Manages a queue of tasks of type and runs them in the background. @@ -55,42 +90,6 @@ namespace Umbraco.Web.Scheduling private bool _terminated; // remember we've terminated private readonly TaskCompletionSource _terminatedSource = new TaskCompletionSource(); // 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(), 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; - } - } - /// /// Initializes a new instance of the class. /// @@ -129,11 +128,9 @@ namespace Umbraco.Web.Scheduling /// An optional main domain hook. 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 } /// - /// Shuts the taks runner down. + /// Shuts the tasks runner down. /// /// True for force the runner to stop. /// True to wait until the runner has stopped. @@ -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 /// - /// Requests a registered object to unregister. + /// Requests a registered object to un-register. /// - /// true to indicate the registered object should unregister from the hosting + /// true to indicate the registered object should un-register from the hosting /// environment before returning; otherwise, false. /// /// "When the application manager needs to stop a registered object, it will call the Stop method." - /// The application manager will call the Stop method to ask a registered object to unregister. During + /// 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. /// public void Stop(bool immediate)