3using System.Threading.Tasks;
5using Microsoft.Extensions.Hosting;
6using Microsoft.Extensions.Logging;
38 readonly ILogger<PosixSignalHandler>
logger;
60 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
69 public Task
StartAsync(CancellationToken cancellationToken)
72 throw new InvalidOperationException(
"Attempted to start PosixSignalHandler twice!");
78 return Task.CompletedTask;
82 public async Task
StopAsync(CancellationToken cancellationToken)
87 logger.LogDebug(
"Stopping SignalCheckerThread...");
90 logger.LogTrace(
"Joining SignalCheckerThread...");
104 logger.LogTrace(
"Started SignalChecker");
106 using var unixSignal =
new UnixSignal(signum);
107 if (!unixSignal.IsSet)
109 logger.LogTrace(
"Waiting for {signum}...", signum);
111 while (!unixSignal.IsSet)
114 logger.LogTrace(
"{signum} received!", signum);
117 logger.LogDebug(
"{signum} has already been sent", signum);
119 logger.LogTrace(
"Triggering graceful shutdown...");
122 catch (OperationCanceledException ex)
124 logger.LogDebug(ex,
"SignalChecker cancelled!");
128 logger.LogError(ex,
"SignalChecker crashed!");
132 logger.LogTrace(
"Exiting SignalChecker...");
Task StartAsync(CancellationToken cancellationToken)
Task? signalCheckerTask
The thread used to check the signal. See http://docs.go-mono.com/?link=T%3aMono.Unix....
readonly CancellationTokenSource cancellationTokenSource
The CancellationTokenSource used to stop the signalCheckerTask.
const int CheckDelayMs
Check for signals each time this amount of milliseconds pass.
PosixSignalHandler(IServerControl serverControl, IAsyncDelayer asyncDelayer, ILogger< PosixSignalHandler > logger)
Initializes a new instance of the PosixSignalHandler class.
async Task SignalChecker(Signum signum, bool detach)
Thread for listening to signal.
readonly ILogger< PosixSignalHandler > logger
The ILogger for the PosixSignalHandler.
async Task StopAsync(CancellationToken cancellationToken)
readonly IServerControl serverControl
The IServerControl for the PosixSignalHandler.
readonly IAsyncDelayer asyncDelayer
The IAsyncDelayer for the PosixSignalHandler.
Represents a service that may take an updated Host assembly and run it, stopping the current assembly...
ValueTask GracefulShutdown(bool detach)
Gracefully shutsdown the Host.
For waiting asynchronously.
ValueTask Delay(TimeSpan timeSpan, CancellationToken cancellationToken)
Create a Task that completes after a given timeSpan .