-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
IAsyncComponent
to allow async initialize/terminate (#16536)
* Add IAsyncComponent * Rewrite to use IAsyncComposer * Add AsyncComponentBase and RuntimeAsyncComponentBase * Remove manual disposing of components on restart
- Loading branch information
1 parent
2270db6
commit cf6137d
Showing
13 changed files
with
221 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace Umbraco.Cms.Core.Composing; | ||
|
||
/// <inheritdoc /> | ||
/// <remarks> | ||
/// By default, the component will not execute if Umbraco is restarting. | ||
/// </remarks> | ||
public abstract class AsyncComponentBase : IAsyncComponent | ||
{ | ||
/// <inheritdoc /> | ||
public async Task InitializeAsync(bool isRestarting, CancellationToken cancellationToken) | ||
{ | ||
if (CanExecute(isRestarting)) | ||
{ | ||
await InitializeAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task TerminateAsync(bool isRestarting, CancellationToken cancellationToken) | ||
{ | ||
if (CanExecute(isRestarting)) | ||
{ | ||
await TerminateAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the component can execute. | ||
/// </summary> | ||
/// <param name="isRestarting">If set to <c>true</c> indicates Umbraco is restarting.</param> | ||
/// <returns> | ||
/// <c>true</c> if the component can execute; otherwise, <c>false</c>. | ||
/// </returns> | ||
protected virtual bool CanExecute(bool isRestarting) | ||
=> isRestarting is false; | ||
|
||
/// <summary> | ||
/// Initializes the component. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token. Cancellation indicates that the start process has been aborted.</param> | ||
/// <returns> | ||
/// A <see cref="Task" /> representing the asynchronous operation. | ||
/// </returns> | ||
protected abstract Task InitializeAsync(CancellationToken cancellationToken); | ||
|
||
/// <summary> | ||
/// Terminates the component. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token. Cancellation indicates that the shutdown process should no longer be graceful.</param> | ||
/// <returns> | ||
/// A <see cref="Task" /> representing the asynchronous operation. | ||
/// </returns> | ||
protected virtual Task TerminateAsync(CancellationToken cancellationToken) | ||
=> Task.CompletedTask; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
using Umbraco.Cms.Core.DependencyInjection; | ||
using Umbraco.Cms.Core.DependencyInjection; | ||
|
||
namespace Umbraco.Cms.Core.Composing; | ||
|
||
/// <summary> | ||
/// Provides a base class for composers which compose a component. | ||
/// Provides a composer that appends a component. | ||
/// </summary> | ||
/// <typeparam name="TComponent">The type of the component</typeparam> | ||
/// <typeparam name="TComponent">The type of the component.</typeparam> | ||
/// <remarks> | ||
/// Thanks to this class, a component that does not compose anything can be registered with one line: | ||
/// <code> | ||
/// <![CDATA[ | ||
/// public class MyComponentComposer : ComponentComposer<MyComponent> { } | ||
/// ]]> | ||
/// </code> | ||
/// </remarks> | ||
public abstract class ComponentComposer<TComponent> : IComposer | ||
where TComponent : IComponent | ||
where TComponent : IAsyncComponent | ||
{ | ||
/// <inheritdoc /> | ||
public virtual void Compose(IUmbracoBuilder builder) => builder.Components().Append<TComponent>(); | ||
|
||
// note: thanks to this class, a component that does not compose anything can be | ||
// registered with one line: | ||
// public class MyComponentComposer : ComponentComposer<MyComponent> { } | ||
public virtual void Compose(IUmbracoBuilder builder) | ||
=> builder.Components().Append<TComponent>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace Umbraco.Cms.Core.Composing; | ||
|
||
/// <summary> | ||
/// Represents a component. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// Components are created by DI and therefore must have a public constructor. | ||
/// </para> | ||
/// <para> | ||
/// All components are terminated in reverse order when Umbraco terminates, and disposable components are disposed. | ||
/// </para> | ||
/// </remarks> | ||
public interface IAsyncComponent | ||
{ | ||
/// <summary> | ||
/// Initializes the component. | ||
/// </summary> | ||
/// <param name="isRestarting">If set to <c>true</c> indicates Umbraco is restarting.</param> | ||
/// <param name="cancellationToken">The cancellation token. Cancellation indicates that the start process has been aborted.</param> | ||
/// <returns> | ||
/// A <see cref="Task" /> representing the asynchronous operation. | ||
/// </returns> | ||
Task InitializeAsync(bool isRestarting, CancellationToken cancellationToken); | ||
|
||
/// <summary> | ||
/// Terminates the component. | ||
/// </summary> | ||
/// <param name="isRestarting">If set to <c>true</c> indicates Umbraco is restarting.</param> | ||
/// <param name="cancellationToken">The cancellation token. Cancellation indicates that the shutdown process should no longer be graceful.</param> | ||
/// <returns> | ||
/// A <see cref="Task" /> representing the asynchronous operation. | ||
/// </returns> | ||
Task TerminateAsync(bool isRestarting, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
namespace Umbraco.Cms.Core.Composing; | ||
|
||
/// <summary> | ||
/// Represents a component. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para>Components are created by DI and therefore must have a public constructor.</para> | ||
/// <para> | ||
/// All components are terminated in reverse order when Umbraco terminates, and | ||
/// disposable components are disposed. | ||
/// </para> | ||
/// <para> | ||
/// The Dispose method may be invoked more than once, and components | ||
/// should ensure they support this. | ||
/// </para> | ||
/// </remarks> | ||
public interface IComponent | ||
/// <inheritdoc /> | ||
[Obsolete("Use IAsyncComponent instead. This interface will be removed in a future version.")] | ||
public interface IComponent : IAsyncComponent | ||
{ | ||
/// <summary> | ||
/// Initializes the component. | ||
/// Initializes the component. | ||
/// </summary> | ||
void Initialize(); | ||
|
||
/// <summary> | ||
/// Terminates the component. | ||
/// Terminates the component. | ||
/// </summary> | ||
void Terminate(); | ||
|
||
/// <inheritdoc /> | ||
Task IAsyncComponent.InitializeAsync(bool isRestarting, CancellationToken cancellationToken) | ||
{ | ||
Initialize(); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <inheritdoc /> | ||
Task IAsyncComponent.TerminateAsync(bool isRestarting, CancellationToken cancellationToken) | ||
{ | ||
Terminate(); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Umbraco.Cms.Core.Services; | ||
|
||
namespace Umbraco.Cms.Core.Composing; | ||
|
||
/// <inheritdoc /> | ||
/// <remarks> | ||
/// By default, the component will not execute if Umbraco is restarting or the runtime level is not <see cref="RuntimeLevel.Run" />. | ||
/// </remarks> | ||
public abstract class RuntimeAsyncComponentBase : AsyncComponentBase | ||
{ | ||
private readonly IRuntimeState _runtimeState; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RuntimeAsyncComponentBase" /> class. | ||
/// </summary> | ||
/// <param name="runtimeState">State of the Umbraco runtime.</param> | ||
protected RuntimeAsyncComponentBase(IRuntimeState runtimeState) | ||
=> _runtimeState = runtimeState; | ||
|
||
/// <inheritdoc /> | ||
protected override bool CanExecute(bool isRestarting) | ||
=> base.CanExecute(isRestarting) && _runtimeState.Level == RuntimeLevel.Run; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 14 additions & 16 deletions
30
src/Umbraco.Core/Notifications/UmbracoApplicationStartingNotification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.