From 5fed4b2688669736d0983c1d2b43fa63aeb21f7c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 16 Jan 2026 11:40:43 +0000 Subject: [PATCH] refactor: network --- app/http.php | 23 +++--- src/Executor/Runner/Docker.php | 10 ++- src/Executor/Runner/Network.php | 88 +++++++++++++++++++++++ src/Executor/Runner/NetworkManager.php | 98 -------------------------- 4 files changed, 105 insertions(+), 114 deletions(-) create mode 100644 src/Executor/Runner/Network.php delete mode 100644 src/Executor/Runner/NetworkManager.php diff --git a/app/http.php b/app/http.php index 225ae5d..de7f849 100644 --- a/app/http.php +++ b/app/http.php @@ -11,7 +11,7 @@ use OpenRuntimes\Executor\Runner\ImagePuller; use OpenRuntimes\Executor\Runner\Maintenance; use OpenRuntimes\Executor\Runner\Repository\Runtimes; -use OpenRuntimes\Executor\Runner\NetworkManager; +use OpenRuntimes\Executor\Runner\Network; use Swoole\Process; use Swoole\Runtime; use Utopia\Console; @@ -50,14 +50,16 @@ )); $runtimes = new Runtimes(); - /* Create desired networks if they don't exist */ - $networks = explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes'); - $networkManager = new NetworkManager($orchestration, $networks); - - /* Add the current executor to the networks */ + /* 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'); - $networkManager->connectAll($selfContainer); + + /* 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); @@ -71,16 +73,17 @@ ); /* Runner service, used to manage runtimes */ - $runner = new Docker($orchestration, $runtimes, $networkManager); + $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) { + 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(); - $runner->cleanUp(); + $network->cleanup(); + $runner->cleanup(); }); Console::success('Executor is ready.'); diff --git a/src/Executor/Runner/Docker.php b/src/Executor/Runner/Docker.php index a684e72..340d9e1 100644 --- a/src/Executor/Runner/Docker.php +++ b/src/Executor/Runner/Docker.php @@ -25,12 +25,12 @@ class Docker extends Adapter /** * @param Orchestration $orchestration * @param Runtimes $runtimes - * @param NetworkManager $networkManager + * @param string[] $networks */ public function __construct( private readonly Orchestration $orchestration, private readonly Runtimes $runtimes, - private readonly NetworkManager $networkManager + private readonly array $networks ) { } @@ -340,7 +340,7 @@ public function createRuntime( $codeMountPath = $version === 'v2' ? '/usr/code' : '/mnt/code'; $workdir = $version === 'v2' ? '/usr/code' : ''; - $network = $this->networkManager->getAvailable()[array_rand($this->networkManager->getAvailable())]; + $network = $this->networks[array_rand($this->networks)]; $volumes = [ \dirname($tmpSource) . ':/tmp:rw', @@ -1053,7 +1053,7 @@ public function createExecution( /** * @return void */ - public function cleanUp(): void + public function cleanup(): void { Console::log('Cleaning up containers and networks...'); @@ -1084,8 +1084,6 @@ public function cleanUp(): void } batch($jobsRuntimes); - $this->networkManager->removeAll(); - Console::success('Cleanup finished.'); } diff --git a/src/Executor/Runner/Network.php b/src/Executor/Runner/Network.php new file mode 100644 index 0000000..17ced52 --- /dev/null +++ b/src/Executor/Runner/Network.php @@ -0,0 +1,88 @@ +orchestration->networkExists($network)) { + Console::info("[Network] Network {$network} already exists"); + } else { + try { + $this->orchestration->createNetwork($network, false); + Console::success("[Network] Created network: {$network}"); + } catch (\Throwable $e) { + Console::error("[Network] Failed to create network {$network}: {$e->getMessage()}"); + continue; + } + } + $this->available[] = $network; + + /* Add the container */ + try { + $this->orchestration->networkConnect($container, $network); + $this->container = $container; + } catch (\Throwable $e) { + Console::error("[Network] Failed to connect container {$container} to network {$network}: {$e->getMessage()}"); + } + } + } + + /** + * Remove a network and disconnect a container from it. + */ + public function cleanup(): void + { + foreach ($this->available as $network) { + if ($this->container !== null) { + /* Remove the container */ + try { + $this->orchestration->networkDisconnect($this->container, $network); + } catch (\Throwable $e) { + Console::error("[Network] Failed to disconnect container {$this->container} from network {$network}: {$e->getMessage()}"); + } + } + + /* Ensure network exists */ + if ($this->orchestration->networkExists($network)) { + Console::info("[Network] Network {$network} already exists"); + } else { + try { + $this->orchestration->removeNetwork($network); + Console::success("[Network] Deleted network: {$network}"); + } catch (\Throwable $e) { + Console::error("[Network] Failed to delete network {$network}: {$e->getMessage()}"); + } + } + } + } + + /** + * @return string[] Networks that are available + */ + public function getAvailable(): array + { + return $this->available; + } +} diff --git a/src/Executor/Runner/NetworkManager.php b/src/Executor/Runner/NetworkManager.php deleted file mode 100644 index 35b7120..0000000 --- a/src/Executor/Runner/NetworkManager.php +++ /dev/null @@ -1,98 +0,0 @@ - fn (): ?string => $this->ensure($network), - $networks - ); - - $this->available = array_values(array_filter( - batch($jobs), - fn ($v) => \is_string($v) && $v !== '' - )); - } - - /** @return string[] */ - public function getAvailable(): array - { - return $this->available; - } - - public function connectAll(Container $container): void - { - foreach ($this->available as $network) { - try { - $this->orchestration->networkConnect($container->getName(), $network); - } catch (\Throwable) { - // TODO: Orchestration library should throw a distinct exception for "already connected" - } - } - } - - public function removeAll(): void - { - if (empty($this->available)) { - return; - } - - batch(array_map( - fn ($network) => fn () => $this->remove($network), - $this->available - )); - } - - private function remove(string $network): void - { - if (!$this->orchestration->networkExists($network)) { - Console::error("[NetworkManager] Network {$network} does not exist"); - return; - } - - try { - $this->orchestration->removeNetwork($network); - Console::success("[NetworkManager] Removed network: {$network}"); - } catch (\Throwable $e) { - Console::error("[NetworkManager] Failed to remove network {$network}: {$e->getMessage()}"); - } - } - - private function ensure(string $network): ?string - { - if ($this->orchestration->networkExists($network)) { - Console::info("[NetworkManager] Network {$network} already exists"); - return $network; - } - - try { - $this->orchestration->createNetwork($network, false); - Console::success("[NetworkManager] Created network: {$network}"); - return $network; - } catch (\Throwable $e) { - Console::error("[NetworkManager] Failed to create network {$network}: {$e->getMessage()}"); - return null; - } - } -}