Skip to content

Commit

Permalink
Fix critical bug in healthcheck. Use repeat instead of delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Nov 28, 2020
1 parent 769d39a commit 0323493
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Server/HealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public static function start(int $parentPid): void
static::$requestTimeout = (int) Config::getInstance()->get('health_check.timeout');

try {
Loop::delay(static::$checkInterval*1000, function() {
Loop::repeat(static::$checkInterval*1000, function() use($parentPid){
Logger::getInstance()->info('Start health check');

if (!self::isProcessAlive($parentPid)) {
throw new RuntimeException('Parent process died');
}
$sessions = yield from static::getSessionList();
$sessionsForCheck = static::getLoggedInSessions($sessions);
$promises = [];
Expand All @@ -54,10 +56,15 @@ public static function start(int $parentPid): void
Loop::run();
} catch (\Throwable $e) {
Logger::getInstance()->error($e->getMessage());
Logger::getInstance()->critical('Health check failed. Killing parent process');
exec("kill -9 $parentPid");
Logger::getInstance()->critical('Health check failed');
if (self::isProcessAlive($parentPid)) {
Logger::getInstance()->critical('Killing parent process');
exec("kill -9 $parentPid");
}
}

Logger::getInstance()->critical('Health check process exit');

}

private static function getSessionList()
Expand Down Expand Up @@ -112,4 +119,10 @@ private static function sendRequest(string $url): Promise
return yield $response->getBody()->buffer();
});
}

private static function isProcessAlive(int $pid): bool
{
$result = exec("ps -p $pid | grep $pid");
return !empty($result);
}
}

0 comments on commit 0323493

Please sign in to comment.