Skip to content

Commit

Permalink
ADD: FailOnSetupNotStarted, FailOnSetupUnsuccessful
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeago committed Dec 6, 2023
1 parent f25ed27 commit c1b719a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `AsyncServiceSetupResult.Unsuccessful` means that `SetupAsync` completed with a `false` result;
- `AsyncServiceSetupResult.Canceled` means that `SetupAsync` was canceled;
- `AsyncServiceSetupResult.Faulted` means that `SetupAsync` threw an exception.
- Two new boolean properties in `AsyncHostedService` lets subclasses decide whether `StartAsync` should fail when the service is stopped before starting (`FailOnSetupNotStarted`) or `SetupAsync` completes with `false` (`FailOnSetupUnsuccessful`).
The default value is `true` for both properties.

### Changes to existing features
### Changes to existing features

- **BREAKING CHANGE:** In class `Louis.Threading.AsyncService`, methods `StartAsync` and `StopAsync` have been renamed to `StartAndWaitAsync` and `StopAndWaitAsync`, respectively. The old names lead some users (and code analysis tools, e.g. ReSharper) to believe they were asynchronous versions of `Start` and `Stop`.

Expand Down
24 changes: 20 additions & 4 deletions src/Louis.Hosting/AsyncHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,33 @@ protected AsyncHostedService(ILogger logger)
_logger = logger;
}

/// <summary>
/// Gets a value indicating whether <see cref="IHostedService.StartAsync"/> will fail if
/// the result of <see cref="AsyncService.SetupAsync"/> is <see cref="AsyncServiceSetupResult.NotStarted"/>.
/// </summary>
protected virtual bool FailOnSetupNotStarted => true;

/// <summary>
/// Gets a value indicating whether <see cref="IHostedService.StartAsync"/> will fail if
/// the result of <see cref="AsyncService.SetupAsync"/> is <see cref="AsyncServiceSetupResult.Unsuccessful"/>.
/// </summary>
protected virtual bool FailOnSetupUnsuccessful => true;

/// <inheritdoc/>
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
{
LogHostedServiceStarting();
var setupResult = await StartAndWaitAsync(cancellationToken).ConfigureAwait(false);
if (setupResult == AsyncServiceSetupResult.Successful)
switch (setupResult)
{
return;
case AsyncServiceSetupResult.Successful:
case AsyncServiceSetupResult.NotStarted when !FailOnSetupNotStarted:
case AsyncServiceSetupResult.Unsuccessful when !FailOnSetupUnsuccessful:
break;
default:
ThrowHelper.ThrowInvalidOperationException($"Service start failed ({setupResult}).");
break;
}

ThrowHelper.ThrowInvalidOperationException($"Service start failed ({setupResult}).");
}

/// <inheritdoc/>
Expand Down
2 changes: 2 additions & 0 deletions src/Louis.Hosting/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable
*REMOVED*override sealed Louis.Hosting.AsyncHostedService.LogSetupCompleted() -> void
override sealed Louis.Hosting.AsyncHostedService.LogSetupCompleted(bool success) -> void
virtual Louis.Hosting.AsyncHostedService.FailOnSetupNotStarted.get -> bool
virtual Louis.Hosting.AsyncHostedService.FailOnSetupUnsuccessful.get -> bool

0 comments on commit c1b719a

Please sign in to comment.