JohnKnoop.GracefulConsoleRunner 0.2.0

There is a newer version of this package available.
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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JohnKnoop.GracefulConsoleRunner" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JohnKnoop.GracefulConsoleRunner --version 0.2.0                
#r "nuget: JohnKnoop.GracefulConsoleRunner, 0.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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:

  1. A cancellation token for best-effort cancellation
  2. A method called BlockInterruption that returns an IDisposable. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,397 8/17/2018
0.4.0 1,016 5/3/2018
0.3.0 1,030 5/3/2018
0.2.0 988 4/26/2018
0.1.0 980 4/26/2018