You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Command/CloudLifecycle.php

66 lines
2.7 KiB
PHP

<?php
namespace App\Command;
use App\Services\Cloud\CloudAgentReadiness;
use App\Services\Cloud\CloudLifecycleService;
use App\Services\Cloud\CloudRuntimeHeartbeat;
final class CloudLifecycle
{
public static function run(array $argv = [], ?CloudRuntimeHeartbeat $heartbeat = null): int
{
$once = in_array('--once', $argv, true);
$heartbeat ??= CloudRuntimeHeartbeat::start('lifecycle', $once);
$skipEngineObservations = false;
try {
// Online update and boot may execute this PHP process before the
// enrolled Go Agent has completed its mTLS/liveness startup. Skip
// the first host observation during that window.
if (!$once) {
$heartbeat->busy('engine_readiness');
if (!CloudAgentReadiness::wait()) {
$heartbeat->error('engine_readiness_timeout');
fwrite(STDERR, "cloud lifecycle Go Agent readiness timed out\n");
// A prolonged Agent outage must not stop database-only
// billing and expiry work forever. Skip host-backed
// observations in the first tick; the next daemon pass
// resumes normal per-stage isolation.
$skipEngineObservations = true;
}
}
$service = new CloudLifecycleService($heartbeat);
do {
$heartbeat->busy('lifecycle_tick');
try {
// Skip only the first runtime observation after a
// startup-readiness timeout. Database-only billing,
// expiry and reconciliation still run; the next daemon
// pass retries ordinary Agent observations.
$skipThisTick = $skipEngineObservations;
$skipEngineObservations = false;
$service->tick(200, $skipThisTick);
$errors = $service->observationErrors();
if ($errors === []) {
$heartbeat->success();
} else {
$heartbeat->error($errors[0]);
}
} catch (\Throwable $error) {
$heartbeat->error('lifecycle_tick_failed');
fwrite(STDERR, 'cloud lifecycle temporarily unavailable: '
. get_class($error) . PHP_EOL);
if ($once) {
return 1;
}
}
if (!$once) {
usleep(15000000);
}
} while (!$once);
} finally {
$heartbeat->stopped();
}
return 0;
}
}