Lines
94.44%
17 / 18
Methods
66.66%
2 / 3
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| startWorkers | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 4 | ||
| spawnEveryWorker | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 2.03 | ||
| 16 | final readonly class WorkerStartup | |
| 17 | { | |
| 18 | public function __construct( | |
| 19 | private AppTopology $topology, | |
| 20 | private WorkerPool $pool, | |
| 21 | private WorkerSlotRegistry $slots, | |
| 22 | private WorkerWatchdog $watchdog, | |
| 23 | private BrokerWorkerEvents $workerEvents, | |
| 24 | private ExposureReconciler $reconciler, | |
| 25 | private HaSession $session, | |
| 26 | private BrokerRun $run, | |
| 27 | private LoggerInterface $logger, | |
| 28 | ) {} | |
| 29 | ||
| 30 | /** @throws Throwable */ | |
| 31 | public function startWorkers(): void | |
| 32 | { | |
| 33 | $this->spawnEveryWorker(); | |
| 34 | ||
| 35 | // The run can end while a spawn suspends; run() then reports that reason instead. | |
| 36 | if (!$this->run->isRunning()) { | |
| 37 | return; | |
| 38 | } | |
| 39 | ||
| 40 | $workerSlots = $this->topology->getWorkerSlots(); | |
| 41 | ||
| 42 | if (!$workerSlots->isEmpty() && $this->slots->countLiveWorkers() === 0) { | |
| 43 | throw BrokerException::createForReason(BrokerError::NoWorkersStarted, workers: $workerSlots->count()); | |
| 44 | } | |
| 45 | ||
| 46 | $this->watchdog->startProbing(); | |
| 47 | $this->reconciler->scheduleOnceWorkersSettle(); | |
| 48 | ||
| 49 | $this->logger->info('Stewart is running', [ | |
| 50 | 'workers' => $this->slots->countLiveWorkers(), | |
| 51 | 'entities' => $this->session->countEntities(), | |
| 52 | ]); | |
| 53 | } | |
| 54 | ||
| 55 | /** @throws Throwable */ | |
| 56 | private function spawnEveryWorker(): void | |
| 57 | { | |
| 58 | [$failures] = awaitAll($this->topology->getWorkerSlots()->mapToList( | |
| 59 | fn(WorkerSlot $slot) => async(fn(): bool => $this->pool->startWorker($slot, $this->workerEvents)), | |
| 60 | )); | |
| 61 | ||
| 62 | foreach ($failures as $failure) { | |
| 63 | throw $failure; | |
| 64 | } | |
| 65 | } | |
| 66 | } |