Skip to content

Commit

Permalink
Fix zombie healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed May 23, 2023
1 parent 9cfd578 commit f50c801
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/Server/HealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use UnexpectedValueException;
use function Amp\async;
use function Amp\Future\awaitAll;
use function Amp\trapSignal;

class HealthCheck
{
Expand All @@ -39,9 +40,9 @@ public static function start(int $parentPid): void
static::$checkInterval = (int)Config::getInstance()->get('health_check.interval');
static::$requestTimeout = (int)Config::getInstance()->get('health_check.timeout');

try {
EventLoop::repeat(static::$checkInterval, static function () use ($parentPid) {
Logger::getInstance()->info('Start health check');
EventLoop::repeat(static::$checkInterval, static function () use ($parentPid, &$token) {
try {
Logger::getInstance()->warning('Start health check');
if (!self::isProcessAlive($parentPid)) {
throw new RuntimeException('Parent process died');
}
Expand All @@ -54,21 +55,22 @@ public static function start(int $parentPid): void
awaitAll($futures);

Logger::getInstance()->info('Health check ok. Sessions checked: ' . count($sessionsForCheck));
});
EventLoop::run();
} catch (Throwable $e) {
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");
} catch (Throwable $e) {
Logger::getInstance()->error($e->getMessage());
Logger::getInstance()->critical('Health check failed');
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');

}
Expand Down

0 comments on commit f50c801

Please sign in to comment.