Lines 75.47% 40 / 53
Methods 68.18% 15 / 22
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 isLive 100.00% 1 / 1 100.00% 1 / 1 2
 isSpawning 0.00% 0 / 1 0.00% 0 / 1 2
 isStopping 100.00% 1 / 1 100.00% 1 / 1 1
 getOutboxStatus 0.00% 0 / 2 0.00% 0 / 1 6
 beginSpawn 100.00% 3 / 3 100.00% 1 / 1 1
 markSpawnFailed 100.00% 1 / 1 100.00% 1 / 1 1
 recordSpawnedHandle 100.00% 7 / 7 100.00% 1 / 1 2
 markGone 100.00% 2 / 2 100.00% 1 / 1 1
 beginStopping 100.00% 3 / 3 100.00% 1 / 1 1
 beginReplacing 100.00% 3 / 3 100.00% 1 / 1 1
 replaceSlot 0.00% 0 / 4 0.00% 0 / 1 6
 requestReplacement 0.00% 0 / 1 0.00% 0 / 1 2
 requestRetirement 0.00% 0 / 1 0.00% 0 / 1 2
 isRetiring 0.00% 0 / 1 0.00% 0 / 1 2
 takeReplacementRequest 0.00% 0 / 3 0.00% 0 / 1 2
 recordScheduledRestart 100.00% 4 / 4 100.00% 1 / 1 1
 markQuarantined 100.00% 2 / 2 100.00% 1 / 1 1
 stop 100.00% 4 / 4 100.00% 1 / 1 2
 cancelReadyDeadline 100.00% 2 / 2 100.00% 1 / 1 1
 moveTo 100.00% 3 / 3 100.00% 1 / 1 4
 cancelRestart 100.00% 3 / 3 100.00% 1 / 1 1
13final class WorkerSlotState
14{
15    public private(set) WorkerPhase $phase = WorkerPhase::Stopped;
16
17    public private(set) ?WorkerHandle $handle = null;
18
19    private ?TimerHandle $readyDeadline = null;
20
21    private ?TimerHandle $restartTimer = null;
22
23    public private(set) ?Instant $restartDueAt = null;
24
25    public private(set) int $restarts = 0;
26
27    public private(set) int $quarantines = 0;
28
29    private bool $replacementRequested = false;
30
31    private bool $retirementRequested = false;
32
33    private OutboxStatus $retiredOutboxes;
34
35    public function __construct(
36        public private(set) WorkerSlot $slot,
37        public readonly WorkerPoolListener $listener,
38    ) {
39        $this->retiredOutboxes = OutboxStatus::zero();
40    }
41
42    public function isLive(WorkerHandle $handle): bool
43    {
44        return $this->phase === WorkerPhase::Live && $this->handle === $handle;
45    }
46
47    public function isSpawning(): bool
48    {
49        return $this->phase === WorkerPhase::Spawning;
50    }
51
52    public function isStopping(): bool
53    {
54        return $this->phase === WorkerPhase::Stopping;
55    }
56
57    public function getOutboxStatus(): OutboxStatus
58    {
59        $total = $this->retiredOutboxes->plus($this->handle?->getOutboxStatus() ?? OutboxStatus::zero());
60
61        return $this->phase === WorkerPhase::Live ? $total : $total->withoutQueued();
62    }
63
64    public function beginSpawn(): void
65    {
66        $this->moveTo(WorkerPhase::Spawning);
67        $this->cancelRestart();
68        $this->replacementRequested = false;
69    }
70
71    public function markSpawnFailed(): void
72    {
73        $this->moveTo(WorkerPhase::Stopped, from: WorkerPhase::Spawning);
74    }
75
76    public function recordSpawnedHandle(WorkerHandle $handle, ?TimerHandle $readyDeadline): ?WorkerHandle
77    {
78        $this->moveTo(WorkerPhase::Live);
79
80        $previous = $this->handle;
81
82        if ($previous !== null) {
83            $this->retiredOutboxes = $this->retiredOutboxes->plus($previous->getOutboxStatus()->withoutQueued());
84        }
85
86        $this->handle = $handle;
87        $this->readyDeadline = $readyDeadline;
88
89        return $previous;
90    }
91
92    public function markGone(): void
93    {
94        $this->moveTo(WorkerPhase::Stopped, from: WorkerPhase::Live);
95        $this->cancelReadyDeadline();
96    }
97
98    public function beginStopping(): WorkerHandle
99    {
100        $handle = $this->beginReplacing();
101        ++$this->restarts;
102
103        return $handle;
104    }
105
106    public function beginReplacing(): WorkerHandle
107    {
108        $this->moveTo(WorkerPhase::Stopping);
109        $this->cancelReadyDeadline();
110
111        return $this->handle ?? throw new LogicException(\sprintf('Worker %d is stopping without a process.', $this->slot->workerId->value));
112    }
113
114    public function replaceSlot(WorkerSlot $slot): void
115    {
116        if (!$slot->workerId->equals($this->slot->workerId)) {
117            throw new LogicException(\sprintf('Worker %d cannot take the slot of worker %d.', $this->slot->workerId->value, $slot->workerId->value));
118        }
119
120        $this->slot = $slot;
121        $this->retirementRequested = false;
122    }
123
124    public function requestReplacement(): void
125    {
126        $this->replacementRequested = true;
127    }
128
129    public function requestRetirement(): void
130    {
131        $this->retirementRequested = true;
132    }
133
134    public function isRetiring(): bool
135    {
136        return $this->retirementRequested;
137    }
138
139    public function takeReplacementRequest(): bool
140    {
141        $requested = $this->replacementRequested;
142        $this->replacementRequested = false;
143
144        return $requested;
145    }
146
147    public function recordScheduledRestart(TimerHandle $timer, Instant $dueAt): void
148    {
149        $this->moveTo(WorkerPhase::RestartScheduled);
150        $this->restartTimer = $timer;
151        $this->restartDueAt = $dueAt;
152        ++$this->restarts;
153    }
154
155    public function markQuarantined(): void
156    {
157        $this->moveTo(WorkerPhase::Quarantined);
158        ++$this->quarantines;
159    }
160
161    public function stop(): void
162    {
163        if ($this->phase !== WorkerPhase::Stopped) {
164            $this->moveTo(WorkerPhase::Stopped);
165        }
166
167        $this->cancelRestart();
168        $this->cancelReadyDeadline();
169    }
170
171    public function cancelReadyDeadline(): void
172    {
173        $this->readyDeadline?->cancel();
174        $this->readyDeadline = null;
175    }
176
177    private function moveTo(WorkerPhase $next, ?WorkerPhase $from = null): void
178    {
179        if (!$this->phase->canEnter($next) || ($from !== null && $this->phase !== $from)) {
180            throw new LogicException(\sprintf('Worker %d cannot move from %s to %s.', $this->slot->workerId->value, $this->phase->value, $next->value));
181        }
182
183        $this->phase = $next;
184    }
185
186    private function cancelRestart(): void
187    {
188        $this->restartTimer?->cancel();
189        $this->restartTimer = null;
190        $this->restartDueAt = null;
191    }
192}