From ba489afe9a37e6cb8b6e77424ecd575538d24894 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 26 Apr 2017 09:07:48 +0200 Subject: [PATCH] Publicize more IBackgroundTask plumbing --- .../XmlCacheFilePersister.cs | 5 ---- .../Scheduling/IBackgroundTaskRunner.cs | 2 +- src/Umbraco.Web/Scheduling/KeepAlive.cs | 10 -------- .../Scheduling/LatchedBackgroundTaskBase.cs | 21 +++++++++++++---- src/Umbraco.Web/Scheduling/LogScrubber.cs | 10 -------- .../Scheduling/RecurringTaskBase.cs | 23 ++++++++++++++----- .../Scheduling/ScheduledPublishing.cs | 10 -------- src/Umbraco.Web/Scheduling/ScheduledTasks.cs | 10 -------- .../ServerRegistrationEventHandler.cs | 10 -------- 9 files changed, 35 insertions(+), 66 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlCacheFilePersister.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlCacheFilePersister.cs index bdf02ba5ca..3b9f184d1c 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlCacheFilePersister.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlCacheFilePersister.cs @@ -148,11 +148,6 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache } } - public override Task RunAsync(CancellationToken token) - { - throw new NotImplementedException(); - } - public override bool IsAsync { get { return false; } diff --git a/src/Umbraco.Web/Scheduling/IBackgroundTaskRunner.cs b/src/Umbraco.Web/Scheduling/IBackgroundTaskRunner.cs index c4e2dab35d..8c0117541b 100644 --- a/src/Umbraco.Web/Scheduling/IBackgroundTaskRunner.cs +++ b/src/Umbraco.Web/Scheduling/IBackgroundTaskRunner.cs @@ -8,7 +8,7 @@ namespace Umbraco.Web.Scheduling /// /// The type of the managed tasks. /// The interface is not complete and exists only to have the contravariance on T. - internal interface IBackgroundTaskRunner : IDisposable, IRegisteredObject + public interface IBackgroundTaskRunner : IDisposable, IRegisteredObject where T : class, IBackgroundTask { bool IsCompleted { get; } diff --git a/src/Umbraco.Web/Scheduling/KeepAlive.cs b/src/Umbraco.Web/Scheduling/KeepAlive.cs index 380ae85401..763e28b608 100644 --- a/src/Umbraco.Web/Scheduling/KeepAlive.cs +++ b/src/Umbraco.Web/Scheduling/KeepAlive.cs @@ -20,11 +20,6 @@ namespace Umbraco.Web.Scheduling _appContext = appContext; } - public override bool PerformRun() - { - throw new NotImplementedException(); - } - public override async Task PerformRunAsync(CancellationToken token) { if (_appContext == null) return true; // repeat... @@ -69,10 +64,5 @@ namespace Umbraco.Web.Scheduling { get { return true; } } - - public override bool RunsOnShutdown - { - get { return false; } - } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs b/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs index cde9986483..e0cda08e47 100644 --- a/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs +++ b/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs @@ -5,7 +5,14 @@ using Umbraco.Core; namespace Umbraco.Web.Scheduling { - internal abstract class LatchedBackgroundTaskBase : DisposableObject, ILatchedBackgroundTask + /// + /// Provides a base class for latched background tasks. + /// + /// 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). + public abstract class LatchedBackgroundTaskBase : DisposableObject, ILatchedBackgroundTask { private TaskCompletionSource _latch; @@ -17,12 +24,18 @@ namespace Umbraco.Web.Scheduling /// /// Implements IBackgroundTask.Run(). /// - public abstract void Run(); + public virtual void Run() + { + throw new NotSupportedException("This task cannot run synchronously."); + } /// /// Implements IBackgroundTask.RunAsync(). /// - public abstract Task RunAsync(CancellationToken token); + public virtual Task RunAsync(CancellationToken token) + { + throw new NotSupportedException("This task cannot run asynchronously."); + } /// /// Indicates whether the background task can run asynchronously. @@ -49,7 +62,7 @@ namespace Umbraco.Web.Scheduling _latch = new TaskCompletionSource(); } - 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 diff --git a/src/Umbraco.Web/Scheduling/LogScrubber.cs b/src/Umbraco.Web/Scheduling/LogScrubber.cs index 760304574d..9000fc72cf 100644 --- a/src/Umbraco.Web/Scheduling/LogScrubber.cs +++ b/src/Umbraco.Web/Scheduling/LogScrubber.cs @@ -89,19 +89,9 @@ namespace Umbraco.Web.Scheduling return true; // repeat } - public override Task PerformRunAsync(CancellationToken token) - { - throw new NotImplementedException(); - } - public override bool IsAsync { get { return false; } } - - public override bool RunsOnShutdown - { - get { return false; } - } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs b/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs index 567f85f1f5..d9bd33dd30 100644 --- a/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs +++ b/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; @@ -6,12 +7,16 @@ namespace Umbraco.Web.Scheduling /// /// Provides a base class for recurring background tasks. /// - internal abstract class RecurringTaskBase : LatchedBackgroundTaskBase + /// Implement by overriding PerformRun or PerformRunAsync and then IsAsync accordingly, + /// depending on whether the task is implemented as a sync or async method. Run nor RunAsync are + /// sealed here as overriding them would break recurrence. And then optionnally override + /// RunsOnShutdown, in order to indicate whether the latched task should run immediately on + /// shutdown, or just be abandonned (default). + public abstract class RecurringTaskBase : LatchedBackgroundTaskBase { private readonly IBackgroundTaskRunner _runner; private readonly int _periodMilliseconds; private readonly Timer _timer; - private bool _disposed; /// /// Initializes a new instance of the class. @@ -37,7 +42,7 @@ namespace Umbraco.Web.Scheduling /// Implements IBackgroundTask.Run(). /// /// Classes inheriting from RecurringTaskBase must implement PerformRun. - public override void Run() + public sealed override void Run() { var shouldRepeat = PerformRun(); if (shouldRepeat) Repeat(); @@ -47,7 +52,7 @@ namespace Umbraco.Web.Scheduling /// Implements IBackgroundTask.RunAsync(). /// /// Classes inheriting from RecurringTaskBase must implement PerformRun. - public override async Task RunAsync(CancellationToken token) + public sealed override async Task RunAsync(CancellationToken token) { var shouldRepeat = await PerformRunAsync(token); if (shouldRepeat) Repeat(); @@ -74,7 +79,10 @@ namespace Umbraco.Web.Scheduling /// Runs the background task. /// /// A value indicating whether to repeat the task. - public abstract bool PerformRun(); + public virtual bool PerformRun() + { + throw new NotSupportedException("This task cannot run synchronously."); + } /// /// Runs the task asynchronously. @@ -82,7 +90,10 @@ namespace Umbraco.Web.Scheduling /// A cancellation token. /// A instance representing the execution of the background task, /// and returning a value indicating whether to repeat the task. - public abstract Task PerformRunAsync(CancellationToken token); + public virtual Task PerformRunAsync(CancellationToken token) + { + throw new NotSupportedException("This task cannot run asynchronously."); + } protected override void DisposeResources() { diff --git a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs index d7ed241275..bf49e335f6 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs @@ -23,11 +23,6 @@ namespace Umbraco.Web.Scheduling _settings = settings; } - public override bool PerformRun() - { - throw new NotImplementedException(); - } - public override async Task PerformRunAsync(CancellationToken token) { if (_appContext == null) return true; // repeat... @@ -108,10 +103,5 @@ namespace Umbraco.Web.Scheduling { get { return true; } } - - public override bool RunsOnShutdown - { - get { return false; } - } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Scheduling/ScheduledTasks.cs b/src/Umbraco.Web/Scheduling/ScheduledTasks.cs index 3f0a9f2a97..c69d42bd27 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledTasks.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledTasks.cs @@ -81,11 +81,6 @@ namespace Umbraco.Web.Scheduling } } - public override bool PerformRun() - { - throw new NotImplementedException(); - } - public override async Task PerformRunAsync(CancellationToken token) { if (_appContext == null) return true; // repeat... @@ -126,10 +121,5 @@ namespace Umbraco.Web.Scheduling { get { return true; } } - - public override bool RunsOnShutdown - { - get { return false; } - } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs b/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs index a25deac1d9..dff5c6730c 100644 --- a/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs +++ b/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs @@ -124,11 +124,6 @@ namespace Umbraco.Web.Strategies get { return false; } } - public override bool RunsOnShutdown - { - get { return false; } - } - /// /// Runs the background task. /// @@ -150,11 +145,6 @@ namespace Umbraco.Web.Strategies return false; // probably stop if we have an error } } - - public override Task PerformRunAsync(CancellationToken token) - { - throw new NotImplementedException(); - } } } }