1) { throw new \InvalidArgumentException( 'worker 专用模式不能同时使用。' ); } $heartbeat ??= CloudRuntimeHeartbeat::start( $notificationOnly ? 'notification' : ($recoveryOnly ? 'recovery' : ($policyOnly ? 'policy' : ($vpcBatchOnly ? 'vpc' : 'normal'))), $once ); try { $worker = $notificationOnly ? new CloudNotificationDeliveryService() : ($recoveryOnly ? new CloudAutomaticOperationRecoveryService() : new CloudWorkerService(null, $policyOnly, $vpcBatchOnly)); do { $errors = []; $heartbeat->busy( $notificationOnly ? 'notification' : ($recoveryOnly ? 'recovery_tick' : 'worker_tick') ); try { $worked = $worker->tick(); if (!$recoveryOnly) { $errors = $worker->observationErrors(); } } catch (\Throwable $error) { $code = $notificationOnly ? '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 ? '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']; } } }