-
Notifications
You must be signed in to change notification settings - Fork 892
Expand file tree
/
Copy pathSystemdLifetime.cs
More file actions
90 lines (72 loc) · 3.43 KB
/
Copy pathSystemdLifetime.cs
File metadata and controls
90 lines (72 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace Microsoft.Extensions.Hosting.Systemd
{
public class SystemdLifetime : IHostLifetime, IDisposable
{
private readonly ManualResetEvent _shutdownBlock = new ManualResetEvent(false);
private CancellationTokenRegistration _applicationStartedRegistration;
private CancellationTokenRegistration _applicationStoppingRegistration;
public SystemdLifetime(IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, ISystemdNotifier systemdNotifier, ILoggerFactory loggerFactory)
{
Environment = environment ?? throw new ArgumentNullException(nameof(environment));
ApplicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime));
SystemdNotifier = systemdNotifier ?? throw new ArgumentNullException(nameof(systemdNotifier));
Logger = loggerFactory.CreateLogger("Microsoft.Hosting.Lifetime");
}
private IHostApplicationLifetime ApplicationLifetime { get; }
private IHostEnvironment Environment { get; }
private ILogger Logger { get; }
private ISystemdNotifier SystemdNotifier { get; }
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task WaitForStartAsync(CancellationToken cancellationToken)
{
_applicationStartedRegistration = ApplicationLifetime.ApplicationStarted.Register(state =>
{
((SystemdLifetime)state).OnApplicationStarted();
},
this);
_applicationStoppingRegistration = ApplicationLifetime.ApplicationStopping.Register(state =>
{
((SystemdLifetime)state).OnApplicationStopping();
},
this);
// 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/aspnet/AspNetCore/issues/6526
System.Environment.ExitCode = 0;
}
public void Dispose()
{
_shutdownBlock.Set();
AppDomain.CurrentDomain.ProcessExit -= OnProcessExit;
_applicationStartedRegistration.Dispose();
_applicationStoppingRegistration.Dispose();
}
}
}