Lines 90.00% 36 / 40
Methods 60.00% 3 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 run 100.00% 30 / 30 100.00% 1 / 1 5
 startHttpListeners 100.00% 2 / 2 100.00% 1 / 1 2
 stopHttpListeners 50.00% 2 / 4 0.00% 0 / 1 4.12
 stopControlPlane 33.33% 1 / 3 0.00% 0 / 1 3.19
19final readonly class BrokerLifecycle
20{
21    public function __construct(
22        private HaSession $session,
23        private BrokerHaEvents $haEvents,
24        private BrokerRun $run,
25        private SignalHandlers $signals,
26        private LoopErrorLogger $loopErrors,
27        private WorkerStartup $workers,
28        private GeneratedCodeDrift $drift,
29        private ConnectionTracker $connection,
30        private ProcessTimeZone $processTimeZone,
31        private ControlPlane $control,
32        private HttpListenerCollection $httpListeners,
33        private MqttLink $mqtt,
34        private MqttMessageRouter $mqttRouter,
35        private AppPauseService $pauses,
36        private ReleasePoller $releases,
37        private ReloadCoordinator $reloads,
38        private SourceWatcher $sourceWatcher,
39        private LoggerInterface $logger,
40        private ?StoreBackend $store = null,
41    ) {}
42
43    /** @throws Throwable */
44    public function run(): BrokerStopOutcome
45    {
46        $this->loopErrors->install();
47        $this->run->start();
48        $this->signals->install();
49
50        try {
51            // Before Home Assistant: an unreachable store is a config error, not an outage.
52            $this->store?->probe();
53            $this->pauses->restoreStoredOverrides();
54
55            // A stop can land while the previous step suspends; it has already closed what would start here.
56            if ($this->run->isRunning()) {
57                $this->control->start();
58                $this->startHttpListeners();
59                $this->mqtt->startInBackground($this->mqttRouter);
60            }
61
62            if ($this->run->isRunning()) {
63                $this->session->open($this->haEvents);
64            }
65
66            if ($this->run->isRunning()) {
67                $this->processTimeZone->useAsProcessDefault($this->session->getTimeZone());
68                $this->connection->markConnected();
69                $this->drift->recheckGeneratedCode();
70                $this->workers->startWorkers();
71                $this->releases->startPolling();
72                $this->reloads->startAccepting();
73                $this->sourceWatcher->startWatching();
74            }
75
76            return $this->run->awaitStopped();
77        } catch (Throwable $e) {
78            $this->run->stop('fatal error');
79
80            throw $e;
81        } finally {
82            $this->sourceWatcher->stopWatching();
83            $this->reloads->stopAccepting();
84            $this->releases->stopPolling();
85            $this->stopHttpListeners();
86            $this->stopControlPlane();
87            $this->signals->removeAll();
88            $this->loopErrors->restorePrevious();
89        }
90    }
91
92    /** @throws Throwable */
93    private function startHttpListeners(): void
94    {
95        foreach ($this->httpListeners as $listener) {
96            $listener->start();
97        }
98    }
99
100    private function stopHttpListeners(): void
101    {
102        foreach ($this->httpListeners as $listener) {
103            try {
104                $listener->stop();
105            } catch (Throwable $e) {
106                $this->logger->error('Could not stop an HTTP listener while shutting down', ['exception' => $e]);
107            }
108        }
109    }
110
111    private function stopControlPlane(): void
112    {
113        try {
114            $this->control->stop();
115        } catch (Throwable $e) {
116            $this->logger->error('Could not stop the control plane while shutting down', ['exception' => $e]);
117        }
118    }
119}