feat: add PID file and ports file support
This commit is contained in:
@@ -42,10 +42,7 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
|
||||
|
||||
private int _lameDuck;
|
||||
|
||||
// Used by future ports file implementation
|
||||
#pragma warning disable CS0169 // Field is never used
|
||||
private string? _portsFilePath;
|
||||
#pragma warning restore CS0169
|
||||
|
||||
private static readonly TimeSpan AcceptMinSleep = TimeSpan.FromMilliseconds(10);
|
||||
private static readonly TimeSpan AcceptMaxSleep = TimeSpan.FromSeconds(1);
|
||||
@@ -108,6 +105,9 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
|
||||
if (_monitorServer != null)
|
||||
await _monitorServer.DisposeAsync();
|
||||
|
||||
DeletePidFile();
|
||||
DeletePortsFile();
|
||||
|
||||
_logger.LogInformation("Server Exiting..");
|
||||
_shutdownComplete.TrySetResult();
|
||||
}
|
||||
@@ -263,6 +263,9 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
|
||||
await _monitorServer.StartAsync(linked.Token);
|
||||
}
|
||||
|
||||
WritePidFile();
|
||||
WritePortsFile();
|
||||
|
||||
var tmpDelay = AcceptMinSleep;
|
||||
|
||||
try
|
||||
@@ -492,6 +495,68 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
|
||||
client.Account?.RemoveClient(client.Id);
|
||||
}
|
||||
|
||||
private void WritePidFile()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_options.PidFile)) return;
|
||||
try
|
||||
{
|
||||
File.WriteAllText(_options.PidFile, Environment.ProcessId.ToString());
|
||||
_logger.LogDebug("Wrote PID file {PidFile}", _options.PidFile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error writing PID file {PidFile}", _options.PidFile);
|
||||
}
|
||||
}
|
||||
|
||||
private void DeletePidFile()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_options.PidFile)) return;
|
||||
try
|
||||
{
|
||||
if (File.Exists(_options.PidFile))
|
||||
File.Delete(_options.PidFile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error deleting PID file {PidFile}", _options.PidFile);
|
||||
}
|
||||
}
|
||||
|
||||
private void WritePortsFile()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_options.PortsFileDir)) return;
|
||||
try
|
||||
{
|
||||
var exeName = Path.GetFileNameWithoutExtension(Environment.ProcessPath ?? "nats-server");
|
||||
var fileName = $"{exeName}_{Environment.ProcessId}.ports";
|
||||
_portsFilePath = Path.Combine(_options.PortsFileDir, fileName);
|
||||
|
||||
var ports = new { client = _options.Port, monitor = _options.MonitorPort > 0 ? _options.MonitorPort : (int?)null };
|
||||
var json = System.Text.Json.JsonSerializer.Serialize(ports);
|
||||
File.WriteAllText(_portsFilePath, json);
|
||||
_logger.LogDebug("Wrote ports file {PortsFile}", _portsFilePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error writing ports file to {PortsFileDir}", _options.PortsFileDir);
|
||||
}
|
||||
}
|
||||
|
||||
private void DeletePortsFile()
|
||||
{
|
||||
if (_portsFilePath == null) return;
|
||||
try
|
||||
{
|
||||
if (File.Exists(_portsFilePath))
|
||||
File.Delete(_portsFilePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error deleting ports file {PortsFile}", _portsFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!IsShuttingDown)
|
||||
|
||||
Reference in New Issue
Block a user