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.
66 lines
2.5 KiB
PHP
66 lines
2.5 KiB
PHP
<?php
|
|
namespace App\Command;
|
|
|
|
use App\Services\Cloud\CloudWorkerService;
|
|
use App\Services\Cloud\CloudInvoiceService;
|
|
use App\Services\Cloud\CloudRuntimeHeartbeat;
|
|
|
|
final class CloudWorker
|
|
{
|
|
public static function run(array $argv = [], ?CloudRuntimeHeartbeat $heartbeat = null): int
|
|
{
|
|
$once = in_array('--once', $argv, true);
|
|
$policyOnly = in_array('--policy-only', $argv, true);
|
|
$heartbeat ??= CloudRuntimeHeartbeat::start($policyOnly ? 'policy' : 'normal', $once);
|
|
try {
|
|
$worker = new CloudWorkerService(null, $policyOnly);
|
|
do {
|
|
$errors = [];
|
|
$heartbeat->busy('worker_tick');
|
|
try {
|
|
$worked = $worker->tick();
|
|
$errors = $worker->observationErrors();
|
|
} catch (\Throwable $error) {
|
|
$errors[] = 'worker_tick_failed';
|
|
$heartbeat->error('worker_tick_failed');
|
|
fwrite(STDERR, 'cloud worker temporarily unavailable: ' . get_class($error) . PHP_EOL);
|
|
if ($once) {
|
|
return 1;
|
|
}
|
|
$worked = false;
|
|
}
|
|
if ($errors !== []) {
|
|
$heartbeat->error($errors[0]);
|
|
}
|
|
// SMTP remains isolated from lifecycle and policy workers.
|
|
if (!$policyOnly) {
|
|
$heartbeat->busy('notification');
|
|
try {
|
|
CloudInvoiceService::deliverReminders(1);
|
|
$errors = array_merge($errors,
|
|
CloudInvoiceService::observationErrors('notifications'));
|
|
} catch (\Throwable $error) {
|
|
$errors[] = 'notification_failed';
|
|
fwrite(STDERR, 'cloud notification mail temporarily unavailable: '
|
|
. get_class($error) . PHP_EOL);
|
|
}
|
|
}
|
|
if ($errors === []) {
|
|
$heartbeat->success();
|
|
} else {
|
|
$heartbeat->error($errors[0]);
|
|
}
|
|
if (!$once && !$worked) {
|
|
usleep(1000000);
|
|
}
|
|
} while (!$once);
|
|
} catch (\Throwable $error) {
|
|
$heartbeat->error('worker_tick_failed');
|
|
throw $error;
|
|
} finally {
|
|
$heartbeat->stopped();
|
|
}
|
|
return 0;
|
|
}
|
|
}
|