Lines 85.71% 6 / 7
Functions and Methods 75.00% 3 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerRestartPolicy 85.71% 6 / 7 75.00% 3 / 4 5.07 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 decideRestart 100.00% 4 / 4 100.00% 1 / 1 2
 forgetRestarts 100.00% 1 / 1 100.00% 1 / 1 1
 countRestartsInWindow 0.00% 0 / 1 0.00% 0 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker;
6
7use Stewart\Contracts\Time\Clock;
8use Stewart\Runtime\Config\SupervisionConfig;
9use Stewart\Runtime\Model\WorkerId;
10
11final readonly class WorkerRestartPolicy
12{
13    public function __construct(
14        private RestartBudget $budget,
15        private SupervisionConfig $supervision,
16        private Clock $clock,
17    ) {}
18
19    public function decideRestart(WorkerId $workerId): RestartDecision
20    {
21        $attempt = $this->budget->claimAttempt($workerId, $this->clock->getMonotonicTime());
22
23        return $attempt === null
24            ? RestartDecision::quarantine()
25            : RestartDecision::restartAfter($this->supervision->restartBackoff->delayFor($attempt));
26    }
27
28    public function forgetRestarts(WorkerId $workerId): void
29    {
30        $this->budget->forgetAttempts($workerId);
31    }
32
33    public function countRestartsInWindow(WorkerId $workerId): int
34    {
35        return $this->budget->countUsedAttempts($workerId, $this->clock->getMonotonicTime());
36    }
37}