diff --git a/app/http.php b/app/http.php index de7f849..a4d8b9b 100644 --- a/app/http.php +++ b/app/http.php @@ -1,32 +1,22 @@ $payloadSize, @@ -37,6 +27,35 @@ Http::setMode((string)System::getEnv('OPR_EXECUTOR_ENV', Http::MODE_TYPE_PRODUCTION)); +Http::onStart() + ->inject('orchestration') + ->inject('network') + ->inject('imagePuller') + ->inject('maintenance') + ->action(function (Orchestration $orchestration, Network $network, ImagePuller $imagePuller, Maintenance $maintenance) { + /* Fetch own container information */ + $hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname'); + $selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found'); + + /* Create desired networks if they don't exist */ + $network->setup( + explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes'), + $selfContainer->getName() + ); + Http::setResource('networks', fn (): array => $network->getAvailable()); + + /* Pull images */ + $imagePuller->pull(explode(',', System::getEnv('OPR_EXECUTOR_IMAGES') ?: '')); + + /* Start maintenance task */ + $maintenance->start( + (int)System::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'), + (int)System::getEnv('OPR_EXECUTOR_INACTIVE_THRESHOLD', '60') + ); + + Console::success('Executor is ready.'); + }); + Http::onRequest() ->inject('response') ->action(function (Response $response) { @@ -44,49 +63,7 @@ }); run(function () use ($settings) { - $orchestration = new Orchestration(new DockerAPI( - System::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', ''), - System::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '') - )); - $runtimes = new Runtimes(); - - /* Fetch own container information */ - $hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname'); - $selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found'); - - /* Create desired networks if they don't exist */ - $network = new Network($orchestration); - $network->setup( - explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes'), - $selfContainer->getName() - ); - - /* Pull images */ - $imagePuller = new ImagePuller($orchestration); - $imagePuller->pull(explode(',', System::getEnv('OPR_EXECUTOR_IMAGES') ?: '')); - - /* Start maintenance task */ - $maintenance = new Maintenance($orchestration, $runtimes); - $maintenance->start( - (int)System::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'), - (int)System::getEnv('OPR_EXECUTOR_INACTIVE_THRESHOLD', '60') - ); - - /* Runner service, used to manage runtimes */ - $runner = new Docker($orchestration, $runtimes, $network->getAvailable()); - Http::setResource('runner', fn () => $runner); - $server = new Server('0.0.0.0', '80', $settings); $http = new Http($server, 'UTC'); - - Process::signal(SIGTERM, function () use ($maintenance, $runner, $network) { - // This doesn't actually work. We need to fix utopia-php/http@0.34.x - $maintenance->stop(); - $network->cleanup(); - $runner->cleanup(); - }); - - Console::success('Executor is ready.'); - $http->start(); }); diff --git a/app/init.php b/app/init.php index 8082a75..4058458 100644 --- a/app/init.php +++ b/app/init.php @@ -1,8 +1,38 @@ set('runtimes', fn () => new Runtimes()); + +Http::setResource('runtimes', fn () => $registry->get('runtimes')); + +Http::setResource('orchestration', fn (): Orchestration => new Orchestration(new DockerAPI( + System::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', ''), + System::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '') +))); + +Http::setResource('network', fn (Orchestration $orchestration): Network => new Network($orchestration), ['orchestration']); + +Http::setResource('imagePuller', fn (Orchestration $orchestration): ImagePuller => new ImagePuller($orchestration), ['orchestration']); + +Http::setResource('maintenance', fn (Orchestration $orchestration, Runtimes $runtimes): Maintenance => new Maintenance($orchestration, $runtimes), ['orchestration', 'runtimes']); + +Http::setResource('runner', fn (Orchestration $orchestration, Runtimes $runtimes, array $networks): Adapter => new Docker($orchestration, $runtimes, $networks), ['orchestration', 'runtimes', 'networks']);