feat: implement log reopening on SIGUSR1 signal
This commit is contained in:
@@ -139,6 +139,14 @@ using var server = new NatsServer(options, loggerFactory);
|
|||||||
// Register signal handlers
|
// Register signal handlers
|
||||||
server.HandleSignals();
|
server.HandleSignals();
|
||||||
|
|
||||||
|
server.ReOpenLogFile = () =>
|
||||||
|
{
|
||||||
|
Log.Information("Reopening log file");
|
||||||
|
Log.CloseAndFlush();
|
||||||
|
Log.Logger = logConfig.CreateLogger();
|
||||||
|
Log.Information("File log re-opened");
|
||||||
|
};
|
||||||
|
|
||||||
// Ctrl+C triggers graceful shutdown
|
// Ctrl+C triggers graceful shutdown
|
||||||
Console.CancelKeyPress += (_, e) =>
|
Console.CancelKeyPress += (_, e) =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
|
|||||||
public string ServerNKey { get; }
|
public string ServerNKey { get; }
|
||||||
public bool IsShuttingDown => Volatile.Read(ref _shutdown) != 0;
|
public bool IsShuttingDown => Volatile.Read(ref _shutdown) != 0;
|
||||||
public bool IsLameDuckMode => Volatile.Read(ref _lameDuck) != 0;
|
public bool IsLameDuckMode => Volatile.Read(ref _lameDuck) != 0;
|
||||||
|
public Action? ReOpenLogFile { get; set; }
|
||||||
public IEnumerable<NatsClient> GetClients() => _clients.Values;
|
public IEnumerable<NatsClient> GetClients() => _clients.Values;
|
||||||
|
|
||||||
public Task WaitForReadyAsync() => _listeningStarted.Task;
|
public Task WaitForReadyAsync() => _listeningStarted.Task;
|
||||||
@@ -192,7 +193,7 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers Unix signal handlers.
|
/// Registers Unix signal handlers.
|
||||||
/// SIGTERM → shutdown, SIGUSR2 → lame duck, SIGUSR1 → log reopen (stub), SIGHUP → reload (stub).
|
/// SIGTERM → shutdown, SIGUSR2 → lame duck, SIGUSR1 → log reopen, SIGHUP → reload (stub).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void HandleSignals()
|
public void HandleSignals()
|
||||||
{
|
{
|
||||||
@@ -222,7 +223,8 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
|
|||||||
_signalRegistrations.Add(PosixSignalRegistration.Create((PosixSignal)10, ctx =>
|
_signalRegistrations.Add(PosixSignalRegistration.Create((PosixSignal)10, ctx =>
|
||||||
{
|
{
|
||||||
ctx.Cancel = true;
|
ctx.Cancel = true;
|
||||||
_logger.LogWarning("Trapped SIGUSR1 signal — log reopen not yet supported");
|
_logger.LogInformation("Trapped SIGUSR1 signal — reopening log file");
|
||||||
|
ReOpenLogFile?.Invoke();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
_signalRegistrations.Add(PosixSignalRegistration.Create((PosixSignal)12, ctx =>
|
_signalRegistrations.Add(PosixSignalRegistration.Create((PosixSignal)12, ctx =>
|
||||||
|
|||||||
Reference in New Issue
Block a user