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/CloudWorker.php

104 lines
4.1 KiB
PHP

<?php
namespace App\Command;
use App\Services\Cloud\CloudInvoiceService;
use App\Services\Cloud\CloudMaintenanceNoticeDeliveryService;
use App\Services\Cloud\CloudAutomaticOperationRecoveryService;
use App\Services\Cloud\CloudRuntimeHeartbeat;
use App\Services\Cloud\CloudWorkerService;
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);
$vpcBatchOnly = in_array('--vpc-batch-only', $argv, true);
$recoveryOnly = in_array('--recovery-only', $argv, true);
$notificationOnly = in_array('--notification-only', $argv, true);
if (($policyOnly ? 1 : 0) + ($vpcBatchOnly ? 1 : 0)
+ ($recoveryOnly ? 1 : 0) + ($notificationOnly ? 1 : 0) > 1) {
throw new \InvalidArgumentException(
'worker 专用模式不能同时使用。'
);
}
$heartbeat ??= CloudRuntimeHeartbeat::start(
$notificationOnly ? 'notification'
: ($recoveryOnly ? 'recovery' : ($policyOnly ? 'policy'
: ($vpcBatchOnly ? 'vpc' : 'normal'))),
$once
);
try {
$worker = $notificationOnly
? new CloudMaintenanceNoticeDeliveryService()
: ($recoveryOnly
? new CloudAutomaticOperationRecoveryService()
: new CloudWorkerService(null, $policyOnly, $vpcBatchOnly));
do {
$errors = [];
$heartbeat->busy(
$notificationOnly ? 'maintenance_notification'
: ($recoveryOnly ? 'recovery_tick' : 'worker_tick')
);
try {
$worked = $worker->tick();
if (!$recoveryOnly && !$notificationOnly) {
$errors = $worker->observationErrors();
}
} catch (\Throwable $error) {
$code = $notificationOnly
? 'maintenance_notification_failed'
: ($recoveryOnly
? 'recovery_tick_failed' : 'worker_tick_failed');
$errors[] = $code;
$heartbeat->error($code);
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 && !$vpcBatchOnly && !$recoveryOnly
&& !$notificationOnly) {
$errors = array_merge($errors, self::deliverNotifications($heartbeat));
}
if ($errors === []) {
$heartbeat->success();
} else {
$heartbeat->error($errors[0]);
}
if (!$once && !$worked) {
usleep(1000000);
}
} while (!$once);
} catch (\Throwable $error) {
$heartbeat->error(
$notificationOnly ? 'maintenance_notification_failed'
: ($recoveryOnly
? 'recovery_tick_failed' : 'worker_tick_failed')
);
throw $error;
} finally {
$heartbeat->stopped();
}
return 0;
}
private static function deliverNotifications(CloudRuntimeHeartbeat $heartbeat): array
{
$heartbeat->busy('notification');
try {
CloudInvoiceService::deliverReminders(1);
return CloudInvoiceService::observationErrors('notifications');
} catch (\Throwable $error) {
fwrite(STDERR, 'cloud notification mail temporarily unavailable: '
. get_class($error) . PHP_EOL);
return ['notification_failed'];
}
}
}