Lines
100.00%
31 / 31
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| createBootstrapMessage | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 1 | ||
| buildWorkerSettings | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 2 | ||
| buildStoreSettings | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| 18 | final readonly class BootstrapMessageFactory | |
| 19 | { | |
| 20 | // Headroom so a worker times out a call only after Home Assistant has. | |
| 21 | private const int CALL_TIMEOUT_GRACE_SECONDS = 5; | |
| 22 | ||
| 23 | private Duration $callTimeout; | |
| 24 | ||
| 25 | private ?StoreSettings $storeSettings; | |
| 26 | ||
| 27 | /** @throws ConfigurationException */ | |
| 28 | public function __construct( | |
| 29 | private StewartConfig $config, | |
| 30 | private AppTopology $topology, | |
| 31 | private AppPauseRegistry $pausedApps, | |
| 32 | private string $userServicesFile, | |
| 33 | ) { | |
| 34 | $this->callTimeout = $config->requireHomeAssistant()->commandTimeout->plus(Duration::seconds(self::CALL_TIMEOUT_GRACE_SECONDS)); | |
| 35 | $this->storeSettings = $this->buildStoreSettings($config->persistence); | |
| 36 | } | |
| 37 | ||
| 38 | public function createBootstrapMessage(WorkerHandle $handle, HaSession $session): Bootstrap | |
| 39 | { | |
| 40 | return new Bootstrap( | |
| 41 | protocol: IpcCodec::PROTOCOL_VERSION, | |
| 42 | workerId: $handle->id, | |
| 43 | timeZone: $session->getTimeZone()->getName(), | |
| 44 | location: $session->getLocation(), | |
| 45 | apps: WorkerAppsFragment::fromCollection($handle->slot->listWorkerApps()), | |
| 46 | settings: $this->buildWorkerSettings(), | |
| 47 | store: $this->storeSettings, | |
| 48 | knownAppIds: AppIdsFragment::fromCollection($this->topology->getCatalog()->knownIds), | |
| 49 | pausedAppIds: AppIdsFragment::fromCollection($this->pausedApps->listPausedAppIds()), | |
| 50 | haUserId: $session->getHaUserId(), | |
| 51 | ); | |
| 52 | } | |
| 53 | ||
| 54 | // services.php is looked for at every spawn, so one created after the start is used once the workers reload. | |
| 55 | private function buildWorkerSettings(): WorkerSettings | |
| 56 | { | |
| 57 | return new WorkerSettings( | |
| 58 | logLevel: $this->config->logLevel, | |
| 59 | callTimeout: $this->callTimeout, | |
| 60 | shutdownGrace: $this->config->shutdownGrace, | |
| 61 | subscriptionBuffer: $this->config->subscriptionBuffer, | |
| 62 | initializeTimeout: $this->config->supervision->initializeTimeout->findDuration(), | |
| 63 | servicesFile: is_file($this->userServicesFile) ? $this->userServicesFile : null, | |
| 64 | generatedNamespace: $this->config->codegen->namespace->value, | |
| 65 | mqttEnabled: $this->config->mqtt !== null, | |
| 66 | ); | |
| 67 | } | |
| 68 | ||
| 69 | private function buildStoreSettings(PersistenceConfig $persistence): ?StoreSettings | |
| 70 | { | |
| 71 | $url = $persistence->url; | |
| 72 | ||
| 73 | return $url === null ? null : new StoreSettings( | |
| 74 | dsn: $url->reveal(), | |
| 75 | prefix: $persistence->prefix->value, | |
| 76 | timeout: $persistence->timeout, | |
| 77 | recoveryInterval: $persistence->recoveryInterval, | |
| 78 | ); | |
| 79 | } | |
| 80 | } |