-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to stop the
Host
when one of its `BackgroundService…
…` instances errors (#50569) * Added background service exception behavior enum. Update HostOptions and Host accordingly. * Added corresponding unit tests for host when ignoring or stopping. * Call StopAsync rather than FailFast * Call `IApplicationLifetime.StopApplication()` instead of `StopAsync()`. * Moved enum out of abstractions. Encapsulate logging logic in extensions. Define new EventId. Simplify check, avoid null eval. Update triple dash comments per peer review. * Set the default as `StopHost` * Move ref bits from hosting.abstractions to hosting proper. * Address feedback from peer review * Added unit test to verify background service 1 gets started, and runs a bit - and other service can cause stop * Use explicitly controlled timing for unit test and remove args: ex * Convert from Fact, to Theory. Test both enum values. * Separate StartAsync and ExecuteTask, let StartAsync bubble. Adjust unit tests... * Update the background service async exceptions get logged unit test
- Loading branch information
1 parent
fc53ee7
commit bbe669c
Showing
10 changed files
with
237 additions
and
27 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
29 changes: 29 additions & 0 deletions
29
src/libraries/Microsoft.Extensions.Hosting/src/BackgroundServiceExceptionBehavior.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.Extensions.Hosting | ||
{ | ||
/// <summary> | ||
/// Specifies a behavior that the <see cref="IHost"/> will honor if an | ||
/// unhandled exception occurs in one of its <see cref="BackgroundService"/> instances. | ||
/// </summary> | ||
public enum BackgroundServiceExceptionBehavior | ||
{ | ||
/// <summary> | ||
/// Stops the <see cref="IHost"/> instance. | ||
/// </summary> | ||
/// <remarks> | ||
/// If a <see cref="BackgroundService"/> throws an exception, the <see cref="IHost"/> instance stops, and the process continues. | ||
/// </remarks> | ||
StopHost = 0, | ||
|
||
/// <summary> | ||
/// Ignore exceptions thrown in <see cref="BackgroundService"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// If a <see cref="BackgroundService"/> throws an exception, the <see cref="IHost"/> will log the error, but otherwise ignore it. | ||
/// The <see cref="BackgroundService"/> is not restarted. | ||
/// </remarks> | ||
Ignore = 1 | ||
} | ||
} |
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
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
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.