Lines
90.00%
45 / 50
Methods
66.66%
4 / 6
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| createBroker | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| buildSyntheticServicesFor | 100.00% | 20 / 20 | 100.00% | 1 / 1 | 1 | ||
| buildNamedArguments | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| warnAboutSlowInitializeCalls | 42.85% | 3 / 7 | 0.00% | 0 / 1 | 4.68 | ||
| warnAboutReadyTimeoutBelowInitialize | 92.85% | 13 / 14 | 0.00% | 0 / 1 | 4.01 | ||
| 29 | final readonly class BrokerKernel | |
| 30 | { | |
| 31 | public function __construct( | |
| 32 | private StewartVersion $stewartVersion, | |
| 33 | private string $userServicesFile, | |
| 34 | private string $stewartConsoleScript, | |
| 35 | private ProjectRoot $projectRoot, | |
| 36 | private Psr4Map $psr4Map, | |
| 37 | private SyntheticServices $overrides = new SyntheticServices(), | |
| 38 | private ProfileCompiler $compiler = new ProfileCompiler(), | |
| 39 | ) {} | |
| 40 | ||
| 41 | /** @throws ConfigurationException */ | |
| 42 | public function createBroker(StewartConfig $config, AppCatalog $apps, LoggerInterface $logger, LogVerbosityPolicy $verbosity, DaemonLaunchOptions $launch = new DaemonLaunchOptions()): BrokerLifecycle | |
| 43 | { | |
| 44 | $this->warnAboutSlowInitializeCalls($config, $logger); | |
| 45 | $this->warnAboutReadyTimeoutBelowInitialize($config, $logger); | |
| 46 | ||
| 47 | return $this->compiler | |
| 48 | ->compileProfileContainer(ContainerProfile::Broker, $this->buildSyntheticServicesFor($config, $apps, $logger, $verbosity, $launch)->withOverrides($this->overrides), $this->buildNamedArguments()) | |
| 49 | ->resolveService(BrokerLifecycle::class); | |
| 50 | } | |
| 51 | ||
| 52 | /** @throws ConfigurationException */ | |
| 53 | private function buildSyntheticServicesFor(StewartConfig $config, AppCatalog $apps, LoggerInterface $logger, LogVerbosityPolicy $verbosity, DaemonLaunchOptions $launch): SyntheticServices | |
| 54 | { | |
| 55 | return new SyntheticServices() | |
| 56 | ->withService(StewartConfig::class, $config) | |
| 57 | ->withService(DaemonLaunchOptions::class, $launch) | |
| 58 | ->withService(StewartVersion::class, $this->stewartVersion) | |
| 59 | ->withService(AppCatalog::class, $apps) | |
| 60 | ->withService(AppDefinitionCollection::class, $apps->enabled) | |
| 61 | ->withService(LoggerInterface::class, $logger) | |
| 62 | ->withService(LogVerbosityPolicy::class, $verbosity) | |
| 63 | ->withService(ConnectionConfig::class, $config->requireHomeAssistant()) | |
| 64 | ->withService(SupervisionConfig::class, $config->supervision) | |
| 65 | ->withService(HaCallPolicy::class, $config->haCalls) | |
| 66 | ->withService(ControlConfig::class, $config->control) | |
| 67 | ->withService(HttpConfig::class, $config->http) | |
| 68 | ->withService(PersistenceConfig::class, $config->persistence) | |
| 69 | ->withService(ProjectRoot::class, $this->projectRoot) | |
| 70 | ->withService(Psr4Map::class, $this->psr4Map) | |
| 71 | ->withService(CodegenConfig::class, $config->codegen) | |
| 72 | ->withService(GitDeployConfig::class, $config->gitDeploy) | |
| 73 | ->withService(ExposeConfig::class, $config->expose) | |
| 74 | ->withService(OutboxLimits::class, new OutboxLimits($config->workerEventBuffer, $config->workerStateBatch)); | |
| 75 | } | |
| 76 | ||
| 77 | private function buildNamedArguments(): NamedArguments | |
| 78 | { | |
| 79 | return new NamedArguments() | |
| 80 | ->withArgument('userServicesFile', $this->userServicesFile) | |
| 81 | ->withArgument('stewartConsoleScript', $this->stewartConsoleScript); | |
| 82 | } | |
| 83 | ||
| 84 | /** @throws ConfigurationException */ | |
| 85 | private function warnAboutSlowInitializeCalls(StewartConfig $config, LoggerInterface $logger): void | |
| 86 | { | |
| 87 | $initializeTimeout = $config->supervision->initializeTimeout->findDuration(); | |
| 88 | $commandTimeout = $config->requireHomeAssistant()->commandTimeout; | |
| 89 | ||
| 90 | if ($initializeTimeout !== null && $commandTimeout->isLongerThan($initializeTimeout)) { | |
| 91 | $logger->warning('An app initializing with a single slow action call will be dropped before that call times out', [ | |
| 92 | 'initialize_timeout' => (string) $initializeTimeout, | |
| 93 | 'command_timeout' => (string) $commandTimeout, | |
| 94 | ]); | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | private function warnAboutReadyTimeoutBelowInitialize(StewartConfig $config, LoggerInterface $logger): void | |
| 99 | { | |
| 100 | $readyTimeout = $config->supervision->readyTimeout->findDuration(); | |
| 101 | $initializeTimeout = $config->supervision->initializeTimeout->findDuration(); | |
| 102 | ||
| 103 | if ($readyTimeout === null) { | |
| 104 | return; | |
| 105 | } | |
| 106 | ||
| 107 | if ($initializeTimeout === null) { | |
| 108 | $logger->warning('A hanging initialize() restarts its whole worker, because initialize_timeout is off while ready_timeout is on', [ | |
| 109 | 'ready_timeout' => (string) $readyTimeout, | |
| 110 | ]); | |
| 111 | ||
| 112 | return; | |
| 113 | } | |
| 114 | ||
| 115 | if (!$readyTimeout->isLongerThan($initializeTimeout)) { | |
| 116 | $logger->warning('A worker is restarted before an app that uses its whole initialize_timeout can report ready', [ | |
| 117 | 'initialize_timeout' => (string) $initializeTimeout, | |
| 118 | 'ready_timeout' => (string) $readyTimeout, | |
| 119 | ]); | |
| 120 | } | |
| 121 | } | |
| 122 | } |