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.
73 lines
2.5 KiB
PHTML
73 lines
2.5 KiB
PHTML
|
2 weeks ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Command;
|
||
|
|
|
||
|
|
use App\Services\Cloud\CloudRuntimeHeartbeat;
|
||
|
|
use App\Services\Cloud\CloudStorageReplicaWorkerService;
|
||
|
|
|
||
|
|
/** Independent worker so large object copies cannot block VM lifecycle jobs. */
|
||
|
|
final class CloudStorageReplicaWorker
|
||
|
|
{
|
||
|
|
public static function run(
|
||
|
|
array $argv = [],
|
||
|
|
?CloudRuntimeHeartbeat $heartbeat = null
|
||
|
|
): int
|
||
|
|
{
|
||
|
|
$once = in_array('--once', $argv, true);
|
||
|
|
$heartbeat ??= CloudRuntimeHeartbeat::start(
|
||
|
|
'storage_replica',
|
||
|
|
$once
|
||
|
|
);
|
||
|
|
$unresolved = false;
|
||
|
|
try {
|
||
|
|
$worker = new CloudStorageReplicaWorkerService();
|
||
|
|
do {
|
||
|
|
$errors = [];
|
||
|
|
$heartbeat->busy('storage_replica_tick');
|
||
|
|
try {
|
||
|
|
$worked = $worker->tick();
|
||
|
|
$errors = $worker->observationErrors();
|
||
|
|
} catch (\Throwable $error) {
|
||
|
|
$errors[] = 'storage_replica_failed';
|
||
|
|
$heartbeat->error('storage_replica_failed');
|
||
|
|
fwrite(
|
||
|
|
STDERR,
|
||
|
|
'storage replica worker temporarily unavailable: '
|
||
|
|
. get_class($error) . PHP_EOL
|
||
|
|
);
|
||
|
|
if ($once) {
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
$worked = false;
|
||
|
|
}
|
||
|
|
if ($errors === []) {
|
||
|
|
// An idle poll proves only that the process is alive. It
|
||
|
|
// must not erase a prior copy/reconciliation warning; a
|
||
|
|
// later item has to complete cleanly before the worker is
|
||
|
|
// projected healthy again.
|
||
|
|
if ($worked) {
|
||
|
|
$unresolved = false;
|
||
|
|
$heartbeat->success();
|
||
|
|
} elseif (!$unresolved) {
|
||
|
|
$heartbeat->success();
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// Error details remain in durable operation records. The
|
||
|
|
// public heartbeat exposes only this bounded category.
|
||
|
|
$unresolved = true;
|
||
|
|
$heartbeat->error('storage_replica_failed');
|
||
|
|
}
|
||
|
|
if (!$once && !$worked) {
|
||
|
|
usleep(1000000);
|
||
|
|
}
|
||
|
|
} while (!$once);
|
||
|
|
} catch (\Throwable $error) {
|
||
|
|
$heartbeat->error('storage_replica_failed');
|
||
|
|
throw $error;
|
||
|
|
} finally {
|
||
|
|
$heartbeat->stopped();
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|