in rd-net/Lifetimes/Diagnostics/ProcessWatchdog.cs [74:131]
public static void StartWatchdogForPid(Options options)
{
var pid = options.Pid;
var watchThread = new Thread(() =>
{
var lifetime = options.Lifetime;
var beforeProcessKill = options.BeforeProcessKill;
var gracefulShutdownPeriod = options.GracefulShutdownPeriod;
var killCurrentProcess = options.KillCurrentProcess;
ourLogger.Info($"Monitoring parent process PID:{pid}");
var useWinApi = true;
while (true)
{
if (lifetime.IsNotAlive)
{
ourLogger.Info($"Monitoring of process {pid} stopped by lifetime termination");
return;
}
if (!ProcessExists(pid, ref useWinApi))
{
var exitMsg = $"Parent process PID:{pid} has quit, killing ourselves via Process.Kill";
try
{
LogLog.Error(exitMsg);
ourLogger.Error(exitMsg);
beforeProcessKill?.Invoke();
if (gracefulShutdownPeriod.HasValue)
{
var waitingMessage = $"Waiting for grace period: {gracefulShutdownPeriod.Value}";
LogLog.Info(waitingMessage);
ourLogger.Info(waitingMessage);
Thread.Sleep(gracefulShutdownPeriod.Value);
}
}
catch
{
// ignored
}
if (killCurrentProcess != null)
killCurrentProcess();
else
Process.GetCurrentProcess().Kill();
return;
}
Thread.Sleep(DELAY_BEFORE_RETRY);
}
}) {IsBackground = true, Name = "WatchParentPid:" + pid};
watchThread.Start();
}