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.
69 lines
2.9 KiB
PHP
69 lines
2.9 KiB
PHP
<?php
|
|
namespace App\Command;
|
|
|
|
use App\Services\Cloud\CloudEngineReadiness;
|
|
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
|
|
// Type=exec engine has completed its ASGI startup. Keep the first
|
|
// automatic inventory tick from turning that startup window into
|
|
// a false host-unavailable result. One-shot diagnostics retain
|
|
// their existing immediate behavior.
|
|
if (!$once) {
|
|
$heartbeat->busy('engine_readiness');
|
|
if (!CloudEngineReadiness::wait()) {
|
|
$heartbeat->error('engine_readiness_timeout');
|
|
fwrite(STDERR, "cloud lifecycle engine readiness timed out\n");
|
|
// A prolonged engine outage must not stop database-only
|
|
// billing and expiry work forever. Skip engine-backed
|
|
// observations in the first tick so the startup race
|
|
// cannot overwrite the last successful inventory; 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 capacity/runtime observation after
|
|
// a startup-readiness timeout. Database-only billing,
|
|
// expiry and reconciliation still run; the next daemon
|
|
// pass retries ordinary engine 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;
|
|
}
|
|
}
|