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.4 KiB
PHP
69 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Services\Cloud\CloudRuntimeHeartbeat;
|
|
use App\Services\Cloud\CloudStoragePublicationOutboxService;
|
|
|
|
/** Finalizes protected template and rescue publications outside copy workers. */
|
|
final class CloudStoragePublicationWorker
|
|
{
|
|
public static function run(
|
|
array $argv = [],
|
|
?CloudRuntimeHeartbeat $heartbeat = null
|
|
): int {
|
|
$once = in_array('--once', $argv, true);
|
|
$heartbeat ??= CloudRuntimeHeartbeat::start(
|
|
'storage_publication',
|
|
$once
|
|
);
|
|
$unresolved = false;
|
|
try {
|
|
$worker = new CloudStoragePublicationOutboxService();
|
|
do {
|
|
$errors = [];
|
|
$heartbeat->busy('storage_publication_tick');
|
|
try {
|
|
$worked = $worker->tick();
|
|
$errors = $worker->observationErrors();
|
|
} catch (\Throwable $error) {
|
|
$errors[] = 'storage_publication_failed';
|
|
$heartbeat->error('storage_publication_failed');
|
|
fwrite(
|
|
STDERR,
|
|
'storage publication 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. A
|
|
// real successful finalization is required to clear an
|
|
// earlier warning from this worker instance.
|
|
if ($worked) {
|
|
$unresolved = false;
|
|
$heartbeat->success();
|
|
} elseif (!$unresolved) {
|
|
$heartbeat->success();
|
|
}
|
|
} else {
|
|
$unresolved = true;
|
|
$heartbeat->error('storage_publication_failed');
|
|
}
|
|
if (!$once && !$worked) {
|
|
usleep(1000000);
|
|
}
|
|
} while (!$once);
|
|
} catch (\Throwable $error) {
|
|
$heartbeat->error('storage_publication_failed');
|
|
throw $error;
|
|
} finally {
|
|
$heartbeat->stopped();
|
|
}
|
|
return 0;
|
|
}
|
|
}
|