Lines
87.50%
14 / 16
Functions and Methods
87.50%
7 / 8
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| WorkerSlotRegistry | 87.50% | 14 / 16 | 87.50% | 7 / 8 | 12.28 | 0.00% | 0 / 1 | |||
| findOrCreateSlotState | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| findSlotState | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| findHandleForWorker | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| listSlotStates | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| listLiveHandles | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 4 | |||||
| broadcast | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 6 | |||||
| countSpawnedSlots | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| countLiveWorkers | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Broker; | |
| 6 | ||
| 7 | use Stewart\Runtime\Ipc\Message\BrokerMessage; | |
| 8 | use Stewart\Runtime\Model\WorkerId; | |
| 9 | ||
| 10 | final class WorkerSlotRegistry | |
| 11 | { | |
| 12 | /** @var array<int, WorkerSlotState> */ | |
| 13 | private array $states = []; | |
| 14 | ||
| 15 | public function findOrCreateSlotState(WorkerSlot $slot, WorkerPoolListener $listener): WorkerSlotState | |
| 16 | { | |
| 17 | return $this->states[$slot->workerId->value] ??= new WorkerSlotState($slot, $listener); | |
| 18 | } | |
| 19 | ||
| 20 | public function findSlotState(WorkerId $workerId): ?WorkerSlotState | |
| 21 | { | |
| 22 | return $this->states[$workerId->value] ?? null; | |
| 23 | } | |
| 24 | ||
| 25 | public function findHandleForWorker(WorkerId $workerId): ?WorkerHandle | |
| 26 | { | |
| 27 | return $this->findSlotState($workerId)?->handle; | |
| 28 | } | |
| 29 | ||
| 30 | /** @return list<WorkerSlotState> */ | |
| 31 | public function listSlotStates(): array | |
| 32 | { | |
| 33 | $states = $this->states; | |
| 34 | ksort($states); | |
| 35 | ||
| 36 | return array_values($states); | |
| 37 | } | |
| 38 | ||
| 39 | /** @return list<WorkerHandle> */ | |
| 40 | public function listLiveHandles(): array | |
| 41 | { | |
| 42 | $live = []; | |
| 43 | ||
| 44 | foreach ($this->states as $state) { | |
| 45 | $handle = $state->handle; | |
| 46 | ||
| 47 | if ($handle !== null && !$handle->isTerminated()) { | |
| 48 | $live[] = $handle; | |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | return $live; | |
| 53 | } | |
| 54 | ||
| 55 | public function broadcast(BrokerMessage $message): void | |
| 56 | { | |
| 57 | foreach ($this->listLiveHandles() as $handle) { | |
| 58 | $handle->send($message); | |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | public function countSpawnedSlots(): int | |
| 63 | { | |
| 64 | return \count(array_filter($this->states, static fn(WorkerSlotState $state): bool => $state->handle !== null)); | |
| 65 | } | |
| 66 | ||
| 67 | public function countLiveWorkers(): int | |
| 68 | { | |
| 69 | return \count($this->listLiveHandles()); | |
| 70 | } | |
| 71 | } |