Extensions.Hosting.Systemd.Watchdog 2.0.0

dotnet add package Extensions.Hosting.Systemd.Watchdog --version 2.0.0                
NuGet\Install-Package Extensions.Hosting.Systemd.Watchdog -Version 2.0.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="Extensions.Hosting.Systemd.Watchdog" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Extensions.Hosting.Systemd.Watchdog --version 2.0.0                
#r "nuget: Extensions.Hosting.Systemd.Watchdog, 2.0.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 Extensions.Hosting.Systemd.Watchdog as a Cake Addin
#addin nuget:?package=Extensions.Hosting.Systemd.Watchdog&version=2.0.0

// Install Extensions.Hosting.Systemd.Watchdog as a Cake Tool
#tool nuget:?package=Extensions.Hosting.Systemd.Watchdog&version=2.0.0                

Extensions.Hosting.Systemd.Watchdog

Adds support for Systemd Watchdog to Microsoft.Extensions.Hosting.Systemd

Usage

Installation

Add the packages Extensions.Hosting.Systemd.Watchdog which is hosted on NuGet.

Enabling watchdog:

Invoke the UseSystemd overload that takes a bool.

var host = new HostBuilder()
    .UseSystemd(enableWatchdog: true)
    .ConfigureServices((hostContext, services) =>
    {
        services.AddSingleton<IHealthCheck>(new SignalHealthCheck(TimeSpan.FromMinutes(5)));
    })

or when using the .net 8 HostApplicationBuilder:

var builder = Host.CreateApplicationBuilder(args);
builder.UseSystemd(enableWatchdog: true);
builder.Services.AddSingleton<IHealthCheck>(new SignalHealthCheck(TimeSpan.FromMinutes(5));

Register health checks

The watchdog needs an implementation for IHeathCheck and the results of its IsHealhty property is used to notify the state to systemd. Register at least 1 IHealthCheck implementation. The watchdog routing will probe all registered implementations.

var host = new HostBuilder()
    .UseSystemd(enableWatchdog: true)
    .ConfigureServices((hostContext, services) =>
    {
        services.AddSingleton<IHealthCheck>(new SignalHealthCheck(TimeSpan.FromMinutes(5)));
    })

Not registering at least one health check will result in a startup failure.

Example health check

The following example implementation relies on the application that a critical component will frequently "signal" that they are still healthy. For example, signaling after a certain task to complete successfully. The approach is very useful if the task can actually take a long time to complete to not interfere with the watchdog interval.

sealed class SignalHealthCheck : IHealthCheck
{
    readonly TimeSpan InactivityThreshold;

    DateTime healthExpiration;

    bool IHealthCheck.IsHealthy => DateTime.UtcNow < healthExpiration;

    public SignalHealthCheck(TimeSpan inactivityThreshold)
    {
        InactivityThreshold = inactivityThreshold;
        healthExpiration = DateTime.UtcNow + NoActivityAlertDuration;
    }

    public void Signal()
    {
        healthExpiration = DateTime.UtcNow + NoActivityAlertDuration;
    }
}

Background

A services was critical to a system but frequently stalls. I wanted to use the watchdog feature to ensure the service gets restarted by systemctl.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 is compatible.  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. 
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
2.0.0 1,023 5/18/2024
2.0.0-alpha.1 56 5/18/2024
1.0.0-alpha.1 1,029 7/9/2023