Lines
100.00%
18 / 18
Methods
100.00%
2 / 2
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| retryUntilConnected | 100.00% | 17 / 17 | 100.00% | 1 / 1 | 7 | ||
| 17 | final readonly class Reconnector | |
| 18 | { | |
| 19 | public function __construct( | |
| 20 | private StewartConfig $config, | |
| 21 | private LoggerInterface $logger, | |
| 22 | private Deadlines $deadlines, | |
| 23 | ) {} | |
| 24 | ||
| 25 | /** @param Closure(): void $attempt */ | |
| 26 | public function retryUntilConnected(Closure $attempt, Cancellation $stop): void | |
| 27 | { | |
| 28 | for ($failures = 0; !$stop->isRequested(); ++$failures) { | |
| 29 | try { | |
| 30 | $attempt(); | |
| 31 | ||
| 32 | return; | |
| 33 | } catch (Throwable $e) { | |
| 34 | if ($e instanceof HaClientException && $e->reason->isFatal()) { | |
| 35 | throw $e; | |
| 36 | } | |
| 37 | ||
| 38 | $this->logger->log( | |
| 39 | $failures === 0 ? LogLevel::ERROR : LogLevel::WARNING, | |
| 40 | 'Could not reach Home Assistant', | |
| 41 | ['exception' => $e, 'attempt' => $failures + 1], | |
| 42 | ); | |
| 43 | } | |
| 44 | ||
| 45 | try { | |
| 46 | $stop->throwIfRequested(); | |
| 47 | ||
| 48 | $delay = $this->config->reconnectBackoff->delayFor($failures + 1); | |
| 49 | ||
| 50 | $this->logger->info('Retrying Home Assistant', ['in' => (string) $delay, 'attempt' => $failures + 2]); | |
| 51 | $this->deadlines->delay($delay, $stop); | |
| 52 | } catch (CancelledException) { | |
| 53 | return; | |
| 54 | } | |
| 55 | } | |
| 56 | } | |
| 57 | } |