Lines 100.00% 22 / 22
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerStatusBuilder 100.00% 22 / 22 100.00% 3 / 3 6 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 buildWorkerStatuses 100.00% 1 / 1 100.00% 1 / 1 1
 buildWorkerStatus 100.00% 20 / 20 100.00% 1 / 1 4
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Control\Assembler;
6
7use Stewart\Runtime\Broker\HaCallSlots;
8use Stewart\Runtime\Broker\WorkerRestartPolicy;
9use Stewart\Runtime\Broker\WorkerSlotRegistry;
10use Stewart\Runtime\Broker\WorkerSlotState;
11use Stewart\Runtime\Broker\WorkerWatchdog;
12use Stewart\Runtime\Control\Protocol\Status\WorkerStatus;
13use Stewart\Runtime\Lifecycle\WorkerPhase;
14
15final readonly class WorkerStatusBuilder
16{
17    public function __construct(
18        private WorkerSlotRegistry $slots,
19        private WorkerWatchdog $watchdog,
20        private WorkerRestartPolicy $restartPolicy,
21        private HaCallSlots $callSlots,
22    ) {}
23
24    /** @return list<WorkerStatus> */
25    public function buildWorkerStatuses(): array
26    {
27        return array_map($this->buildWorkerStatus(...), $this->slots->listSlotStates());
28    }
29
30    private function buildWorkerStatus(WorkerSlotState $state): WorkerStatus
31    {
32        $workerId = $state->slot->workerId;
33        $live = $state->phase === WorkerPhase::Live ? $state->handle : null;
34        $pong = $live?->lastPong;
35
36        return new WorkerStatus(
37            workerId: $workerId->value,
38            phase: $state->phase,
39            appIds: $state->slot->listAppIds()->toStrings(),
40            pid: $live?->getPid(),
41            ready: $live?->isReady() ?? false,
42            missedProbes: $live === null ? 0 : $this->watchdog->countMissedProbes($live),
43            loopLag: $pong?->loopLag,
44            memoryBytes: $pong?->memoryBytes,
45            lastPongAt: $live?->lastPongAt,
46            restartsInWindow: $this->restartPolicy->countRestartsInWindow($workerId),
47            restartDueAt: $state->restartDueAt,
48            outbox: $state->getOutboxStatus(),
49            inFlightServiceCalls: $live === null ? 0 : $this->callSlots->countInFlightCallsFor($live),
50            restarts: $state->restarts,
51            quarantines: $state->quarantines,
52        );
53    }
54}