JohnKnoop.GracefulConsoleRunner
0.2.0
See the version list below for details.
dotnet add package JohnKnoop.GracefulConsoleRunner --version 0.2.0
NuGet\Install-Package JohnKnoop.GracefulConsoleRunner -Version 0.2.0
<PackageReference Include="JohnKnoop.GracefulConsoleRunner" Version="0.2.0" />
paket add JohnKnoop.GracefulConsoleRunner --version 0.2.0
#r "nuget: JohnKnoop.GracefulConsoleRunner, 0.2.0"
// Install JohnKnoop.GracefulConsoleRunner as a Cake Addin #addin nuget:?package=JohnKnoop.GracefulConsoleRunner&version=0.2.0 // Install JohnKnoop.GracefulConsoleRunner as a Cake Tool #tool nuget:?package=JohnKnoop.GracefulConsoleRunner&version=0.2.0
Add (truly) graceful termination to your .NET console app!
Install
PM> Install-Package JohnKnoop.GracefulConsoleRunner
This is a .NET Standard 1.5 library.
What is it?
When your application logic is invoked by external events, such as a message bus or a scheduler, you'll want to control termination in two levels:
1. Best-effort cancellation
Passing a CancellationToken down the call hierarchy enables you opt-out of starting new work.
2. A hold-off mechanism to prevent interruption of important work
When termination is requested, you'll want a grace period between soft and hard termination, allowing your ongoing work to complete. The termination sequence looks like this:
+-----------------------+ Is work Yes +---------------------------+
| Termination requested |-----> being done? -----------> | Wait for work to complete |
+-----------------------+ +---------------------------+
| |
| No v
| +----------+ +------+
+--------------------------> | Clean up |----->| Exit |
+----------+ +------+
Paste this in your startup routine:
GracefulConsoleRunner.Run(runContext => {
// Your code here
});
The runContext
parameter offers two properties:
- A cancellation token for best-effort cancellation
- A method called
BlockInterruption
that returns anIDisposable
. Use this to wrap any code you want to protect from interruption.
Example
GracefulConsoleRunner.Run(
run: runContext =>
{
myTimer.Elapsed += (sender, e) => {
if (context.ApplicationTermination.IsCancellationRequested)
{
// Graceful termination has been requested. Don't process the message.
return;
}
// Once we've decided to proceed, we don't want to be interrupted until processing is complete
using (runContext.BlockInterruption())
{
// Your code here
}
};
},
cleanup: () =>
{
/*
* Any clean-up code you want to ensure being run before exit
*/
},
gracePeriodSeconds: 30);
In a message queue scenario, the pattern would be the same, where you first check for cancellation, and then wrap the message processing in a BlockInterruption()
to make sure the message is processed and acknowledged without interruption.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.5 is compatible. netstandard1.6 was computed. netstandard2.0 was computed. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 1.5
- NETStandard.Library (>= 1.6.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.