Giter Club home page Giter Club logo

terminet's Introduction

TermiNet

Libary that can be used to properly terminate an application on Windows, Linux, FreeBSD, and OSX.

NuGet: https://www.nuget.org/packages/TermiNet/

Why?

Today, .NET Core runs on Linux, FreeBSD, OSX, and Windows. There are big differences between *nix and Windows based operating systems on how error codes are interpreted. On Linux for example a clean application termination has error code 0. This signals the OS that the application terminated in a clean state. On Windows a clean exit also is 0. But on Linux an error code has to be in the range of 0 to 254. Everything that is out of this range will become 255. Now, having an application that on Windows perfectly returns 2711 or 9283 to indicate a problem, on Linux both melt down to 255. This does not really allow for proper exit code analysis anymore.

Advantages

TermiNet allows for configuration of exit codes in various ways. You can register exceptions and assign codes or provide an exit code directly. Also TermiNet allows to register an pre-termination event and an action, both are called before the application terminates. Moreover, using System.Exit(0) can cause trouble when testing the application. TermiNet provides an interface that can be used during testing. Another idea is using TermiNet with docker or in a service. An application can implement TermiNet and exit with a specific exit code on a certain event, e.g. when a referred service in another docker container (or service) does not respond. Docker (or the service manager) can then evaluate the exit code and be configured to spwan the referred docker application (or service) first, before it spawns your application again.

Examples

The following sections show some examples, of course they can also be combined.

Simple termination

public static void Run()
{
    Console.WriteLine("Hello World!");

    var builder = TerminatorBuilder.CreateBuilder().Build();
    var terminator = builder.Build();

    terminator.Terminate(55, "Exiting with exit code 55.");
}

SIGINT, Exception, Pre-Termination action

public static void Run()
{
    Console.WriteLine("Hello World!");

    var builder = TerminatorBuilder.CreateBuilder()
        .RegisterCtrlC()
        .RegisterPreTerminationAction(() => { Console.WriteLine("Pre Termination Action"); })
        .Register<ArgumentException>(100, "Optional example message");
    builder.TerminateEventHandler += Exit_Terminating;

    var terminator = builder.Build();

    try
    {
        throw new ArgumentException("test");
    }
    catch (Exception e)
    {
        terminator.Terminate(e);
    }
}

private static void Exit_Terminating(object? sender, TerminateEventArgs e)
{
    Console.WriteLine($"Exiting: {e}");
}

Using CancellationToken

public static void Run()
{
    // Create cancellation token
    var cts = new CancellationTokenSource();

    // Setup termintator
    var builder = TerminatorBuilder.CreateBuilder()
        .RegisterCancellationToken(cts.Token, 20, "Terminated by CTS");
    
    var terminator = builder.Build();

    // Display something on console
    var x = Task.Run(async () =>
    {
        while (true)
        {
            Console.WriteLine("Hello");
            await Task.Delay(2000).ConfigureAwait(false);                    
        }
    });

    Console.WriteLine("Press a key");
    Console.ReadKey();

    // Cancel termination
    cts.Cancel();

    // Application has exited before this line is executed
    Console.WriteLine("I should not run...");
}

terminet's People

Contributors

asymetrixs avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.