Lines 94.28% 33 / 35
Methods 85.71% 6 / 7
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 startAccepting 100.00% 1 / 1 100.00% 1 / 1 1
 stopAccepting 100.00% 2 / 2 100.00% 1 / 1 1
 requestReload 100.00% 9 / 9 100.00% 1 / 1 4
 reloadProject 86.66% 13 / 15 0.00% 0 / 1 5.06
 recordApplied 100.00% 5 / 5 100.00% 1 / 1 3
 recordFailure 100.00% 2 / 2 100.00% 1 / 1 1
18final class ReloadCoordinator
19{
20    private ?DeferredCancellation $accepting = null;
21
22    public function __construct(
23        private readonly ReloadCheck $check,
24        private readonly ReloadPlanner $planner,
25        private readonly ReloadApplier $applier,
26        private readonly ReloadState $state,
27        private readonly BrokerRun $run,
28        private readonly Clock $clock,
29        private readonly LoggerInterface $logger,
30    ) {}
31
32    public function startAccepting(): void
33    {
34        $this->accepting ??= new DeferredCancellation();
35    }
36
37    public function stopAccepting(): void
38    {
39        $this->accepting?->cancel();
40        $this->accepting = null;
41    }
42
43    public function requestReload(ReloadTrigger $trigger): ReloadRequestOutcome
44    {
45        if ($this->accepting === null || !$this->run->isRunning()) {
46            return ReloadRequestOutcome::NotAccepting;
47        }
48
49        if ($this->state->isUnderway()) {
50            return ReloadRequestOutcome::AlreadyUnderway;
51        }
52
53        $this->state->beginReload();
54        $this->logger->info('Reloading: checking the project first', ['trigger' => $trigger->value]);
55        $cancellation = $this->accepting->getCancellation();
56        async(fn() => $this->reloadProject($cancellation))->ignore();
57
58        return ReloadRequestOutcome::Started;
59    }
60
61    private function reloadProject(Cancellation $cancellation): void
62    {
63        try {
64            $report = $this->check->runReloadCheck($cancellation);
65            $failure = $report->findFirstError();
66
67            if ($failure !== null) {
68                $this->recordFailure($failure);
69
70                return;
71            }
72
73            if (!$this->run->isRunning()) {
74                $this->state->abandonReload();
75
76                return;
77            }
78
79            $plan = $this->planner->planReload($report);
80            $this->applier->applyReload($plan);
81            $this->recordApplied($plan);
82        } catch (CancelledException) {
83            $this->state->abandonReload();
84        } catch (Throwable $e) {
85            $this->recordFailure($e->getMessage());
86        }
87    }
88
89    private function recordApplied(ReloadPlan $plan): void
90    {
91        $settings = $plan->settingsNeedingRestart;
92
93        if ($settings === []) {
94            $this->logger->info('Reloaded: every worker restarted on the current code and configuration', ['apps' => $plan->catalog->enabled->count()]);
95        } else {
96            $this->logger->warning('Reloaded, except for settings that only a restart applies', ['apps' => $plan->catalog->enabled->count(), 'settings' => $settings]);
97        }
98
99        $this->state->recordResult(new ReloadResult($settings === [] ? ReloadOutcome::Applied : ReloadOutcome::PartlyApplied, $this->clock->getNow(), null, $settings));
100    }
101
102    private function recordFailure(string $reason): void
103    {
104        $this->logger->error('Reload failed; the workers keep running as they were', ['reason' => $reason]);
105        $this->state->recordResult(new ReloadResult(ReloadOutcome::Failed, $this->clock->getNow(), $reason));
106    }
107}