Skip to content

Commit

Permalink
chore: refactor healthcheck logging
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Dec 17, 2024
1 parent c9407b3 commit f0bf3d9
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@
return $group;
}, ['algorithm', 'request', 'state']);

$healthCheck = function (bool $firstCheck = false) use ($register): void {
$healthCheck = function (bool $verbose = false) use ($register): void {
$logger = $register->get('logger');
$state = $register->get('state');
$verbose = Http::isDevelopment() || $verbose;

$health = new Health();
foreach (\explode(',', (string) Http::getEnv('OPR_PROXY_EXECUTORS', '')) as $hostname) {
Expand All @@ -228,13 +229,12 @@
$executor = $executors[$hostname] ?? [];
$newStatus = $node->isOnline() ? 'online' : 'offline';
$healthy = $healthy && $node->isOnline();
$shouldLog = $firstCheck || Http::isDevelopment() || $executor['status'] !== $newStatus;

if ($node->isOnline() && $shouldLog) {
if ($node->isOnline() && ($verbose || $executor['status'] !== $newStatus)) {
Console::info('Executor "' . $hostname . '" is online');
}

if (!$node->isOnline() && $shouldLog) {
if (!$node->isOnline() && ($verbose || $executor['status'] !== $newStatus)) {
$message = $node->getState()['message'] ?? 'Unexpected error.';
$error = new Exception('Executor "' . $hostname . '" went offline: ' . $message, 500);
logError($error, "healthCheckError", $logger, null);
Expand All @@ -247,7 +247,7 @@
usage: $node->getState()['usage'] ?? 0
);

if (Http::isDevelopment()) {
if ($verbose) {
Console::log('Executor "' . $hostname . '" healthcheck returned ' . \count($node->getState()['runtimes'] ?? []) . ' runtimes');
}

Expand Down Expand Up @@ -625,10 +625,10 @@ function logError(Throwable $error, string $action, ?Logger $logger, Route $rout
// Start HTTP server
$http = new Http(new Server('0.0.0.0', Http::getEnv('PORT', '80'), $settings), 'UTC');

$healthCheck(firstCheck: true);
$healthCheck(verbose: true);

$defaultInterval = '10000'; // 10 seconds
Timer::tick(\intval(Http::getEnv('OPR_PROXY_HEALTHCHECK_INTERVAL', $defaultInterval)), fn () => $healthCheck(firstCheck: false));
Timer::tick(\intval(Http::getEnv('OPR_PROXY_HEALTHCHECK_INTERVAL', $defaultInterval)), fn () => $healthCheck(verbose: false));

Console::success('Functions proxy is ready.');

Expand Down

0 comments on commit f0bf3d9

Please sign in to comment.