-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Closed
Copy link
Labels
Milestone
Description
Same change as #55417, but we should make it for Systemd.
runtime/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.cs
Lines 48 to 78 in d38a539
| // 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:
runtime/src/libraries/Microsoft.Extensions.Hosting.Systemd/tests/UseSystemdTests.cs
Lines 13 to 24 in d38a539
| 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.
davidfowldavidfowl