Skip to content

Consume PosixSignal in Microsoft.Extensions.Hosting.Systemd's SystemdLifetime #56058

@eerhardt

Description

@eerhardt

Same change as #55417, but we should make it for Systemd.

// systemd sends SIGTERM to stop the service.
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
return Task.CompletedTask;
}
private void OnApplicationStarted()
{
Logger.LogInformation("Application started. Hosting environment: {EnvironmentName}; Content root path: {ContentRoot}",
Environment.EnvironmentName, Environment.ContentRootPath);
SystemdNotifier.Notify(ServiceState.Ready);
}
private void OnApplicationStopping()
{
Logger.LogInformation("Application is shutting down...");
SystemdNotifier.Notify(ServiceState.Stopping);
}
private void OnProcessExit(object sender, EventArgs e)
{
ApplicationLifetime.StopApplication();
_shutdownBlock.WaitOne();
// On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code.
// Suppress that since we shut down gracefully. https://github.com/dotnet/aspnetcore/issues/6526
System.Environment.ExitCode = 0;
}

We should also figure out how to add real tests, as I don't believe there are any tests for this library, yet. The only test we have:

public void DefaultsToOffOutsideOfService()
{
var host = new HostBuilder()
.UseSystemd()
.Build();
using (host)
{
var lifetime = host.Services.GetRequiredService<IHostLifetime>();
Assert.IsType<ConsoleLifetime>(lifetime);
}
}

tests that Systemd is "off" when not in a systemd service.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions