Fix zombie healthcheck

This commit is contained in:
Alexander Pankratov 2023-05-23 23:54:57 +02:00
parent 9cfd578695
commit e07c7e4e43

View File

@ -13,6 +13,7 @@ use Throwable;
use UnexpectedValueException; use UnexpectedValueException;
use function Amp\async; use function Amp\async;
use function Amp\Future\awaitAll; use function Amp\Future\awaitAll;
use function Amp\trapSignal;
class HealthCheck class HealthCheck
{ {
@ -39,8 +40,8 @@ class HealthCheck
static::$checkInterval = (int)Config::getInstance()->get('health_check.interval'); static::$checkInterval = (int)Config::getInstance()->get('health_check.interval');
static::$requestTimeout = (int)Config::getInstance()->get('health_check.timeout'); static::$requestTimeout = (int)Config::getInstance()->get('health_check.timeout');
try { EventLoop::repeat(static::$checkInterval, static function () use ($parentPid) {
EventLoop::repeat(static::$checkInterval, static function () use ($parentPid) { try {
Logger::getInstance()->info('Start health check'); Logger::getInstance()->info('Start health check');
if (!self::isProcessAlive($parentPid)) { if (!self::isProcessAlive($parentPid)) {
throw new RuntimeException('Parent process died'); throw new RuntimeException('Parent process died');
@ -54,21 +55,22 @@ class HealthCheck
awaitAll($futures); awaitAll($futures);
Logger::getInstance()->info('Health check ok. Sessions checked: ' . count($sessionsForCheck)); Logger::getInstance()->info('Health check ok. Sessions checked: ' . count($sessionsForCheck));
}); } catch (Throwable $e) {
EventLoop::run(); Logger::getInstance()->error($e->getMessage());
} catch (Throwable $e) { Logger::getInstance()->critical('Health check failed');
Logger::getInstance()->error($e->getMessage());
Logger::getInstance()->critical('Health check failed');
if (self::isProcessAlive($parentPid)) {
Logger::getInstance()->critical('Killing parent process');
exec("kill -2 $parentPid");
if (self::isProcessAlive($parentPid)) { if (self::isProcessAlive($parentPid)) {
exec("kill -9 $parentPid"); Logger::getInstance()->critical('Killing parent process');
}
}
}
exec("kill -2 $parentPid");
if (self::isProcessAlive($parentPid)) {
exec("kill -9 $parentPid");
}
}
exit(1);
}
});
trapSignal([SIGINT, SIGTERM]);
Logger::getInstance()->critical('Health check process exit'); Logger::getInstance()->critical('Health check process exit');
} }