Skip to content

Commit 45e4a85

Browse files
authored
Merge pull request #216 from open-runtimes/refactor-network
refactor: network
2 parents 56b9226 + 5fed4b2 commit 45e4a85

File tree

4 files changed

+105
-114
lines changed

4 files changed

+105
-114
lines changed

app/http.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use OpenRuntimes\Executor\Runner\ImagePuller;
1212
use OpenRuntimes\Executor\Runner\Maintenance;
1313
use OpenRuntimes\Executor\Runner\Repository\Runtimes;
14-
use OpenRuntimes\Executor\Runner\NetworkManager;
14+
use OpenRuntimes\Executor\Runner\Network;
1515
use Swoole\Process;
1616
use Swoole\Runtime;
1717
use Utopia\Console;
@@ -50,14 +50,16 @@
5050
));
5151
$runtimes = new Runtimes();
5252

53-
/* Create desired networks if they don't exist */
54-
$networks = explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes');
55-
$networkManager = new NetworkManager($orchestration, $networks);
56-
57-
/* Add the current executor to the networks */
53+
/* Fetch own container information */
5854
$hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname');
5955
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');
60-
$networkManager->connectAll($selfContainer);
56+
57+
/* Create desired networks if they don't exist */
58+
$network = new Network($orchestration);
59+
$network->setup(
60+
explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes'),
61+
$selfContainer->getName()
62+
);
6163

6264
/* Pull images */
6365
$imagePuller = new ImagePuller($orchestration);
@@ -71,16 +73,17 @@
7173
);
7274

7375
/* Runner service, used to manage runtimes */
74-
$runner = new Docker($orchestration, $runtimes, $networkManager);
76+
$runner = new Docker($orchestration, $runtimes, $network->getAvailable());
7577
Http::setResource('runner', fn () => $runner);
7678

7779
$server = new Server('0.0.0.0', '80', $settings);
7880
$http = new Http($server, 'UTC');
7981

80-
Process::signal(SIGTERM, function () use ($maintenance, $runner) {
82+
Process::signal(SIGTERM, function () use ($maintenance, $runner, $network) {
8183
// This doesn't actually work. We need to fix utopia-php/http@0.34.x
8284
$maintenance->stop();
83-
$runner->cleanUp();
85+
$network->cleanup();
86+
$runner->cleanup();
8487
});
8588

8689
Console::success('Executor is ready.');

src/Executor/Runner/Docker.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class Docker extends Adapter
2525
/**
2626
* @param Orchestration $orchestration
2727
* @param Runtimes $runtimes
28-
* @param NetworkManager $networkManager
28+
* @param string[] $networks
2929
*/
3030
public function __construct(
3131
private readonly Orchestration $orchestration,
3232
private readonly Runtimes $runtimes,
33-
private readonly NetworkManager $networkManager
33+
private readonly array $networks
3434
) {
3535
}
3636

@@ -340,7 +340,7 @@ public function createRuntime(
340340
$codeMountPath = $version === 'v2' ? '/usr/code' : '/mnt/code';
341341
$workdir = $version === 'v2' ? '/usr/code' : '';
342342

343-
$network = $this->networkManager->getAvailable()[array_rand($this->networkManager->getAvailable())];
343+
$network = $this->networks[array_rand($this->networks)];
344344

345345
$volumes = [
346346
\dirname($tmpSource) . ':/tmp:rw',
@@ -1053,7 +1053,7 @@ public function createExecution(
10531053
/**
10541054
* @return void
10551055
*/
1056-
public function cleanUp(): void
1056+
public function cleanup(): void
10571057
{
10581058
Console::log('Cleaning up containers and networks...');
10591059

@@ -1084,8 +1084,6 @@ public function cleanUp(): void
10841084
}
10851085
batch($jobsRuntimes);
10861086

1087-
$this->networkManager->removeAll();
1088-
10891087
Console::success('Cleanup finished.');
10901088
}
10911089

src/Executor/Runner/Network.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace OpenRuntimes\Executor\Runner;
4+
5+
use Utopia\Console;
6+
use Utopia\Orchestration\Orchestration;
7+
8+
class Network
9+
{
10+
/** @var string[] Networks that are available */
11+
private array $available = [];
12+
13+
/** @var string|null Name of the container linked to the networks */
14+
private ?string $container = null;
15+
16+
public function __construct(
17+
private readonly Orchestration $orchestration,
18+
) {
19+
}
20+
21+
/**
22+
* @param string[] $networks Networks to ensure exist
23+
* @param string $container Name of the container to link to the networks
24+
*/
25+
public function setup(array $networks, string $container): void
26+
{
27+
foreach ($networks as $network) {
28+
/* Ensure network exists */
29+
if ($this->orchestration->networkExists($network)) {
30+
Console::info("[Network] Network {$network} already exists");
31+
} else {
32+
try {
33+
$this->orchestration->createNetwork($network, false);
34+
Console::success("[Network] Created network: {$network}");
35+
} catch (\Throwable $e) {
36+
Console::error("[Network] Failed to create network {$network}: {$e->getMessage()}");
37+
continue;
38+
}
39+
}
40+
$this->available[] = $network;
41+
42+
/* Add the container */
43+
try {
44+
$this->orchestration->networkConnect($container, $network);
45+
$this->container = $container;
46+
} catch (\Throwable $e) {
47+
Console::error("[Network] Failed to connect container {$container} to network {$network}: {$e->getMessage()}");
48+
}
49+
}
50+
}
51+
52+
/**
53+
* Remove a network and disconnect a container from it.
54+
*/
55+
public function cleanup(): void
56+
{
57+
foreach ($this->available as $network) {
58+
if ($this->container !== null) {
59+
/* Remove the container */
60+
try {
61+
$this->orchestration->networkDisconnect($this->container, $network);
62+
} catch (\Throwable $e) {
63+
Console::error("[Network] Failed to disconnect container {$this->container} from network {$network}: {$e->getMessage()}");
64+
}
65+
}
66+
67+
/* Ensure network exists */
68+
if ($this->orchestration->networkExists($network)) {
69+
Console::info("[Network] Network {$network} already exists");
70+
} else {
71+
try {
72+
$this->orchestration->removeNetwork($network);
73+
Console::success("[Network] Deleted network: {$network}");
74+
} catch (\Throwable $e) {
75+
Console::error("[Network] Failed to delete network {$network}: {$e->getMessage()}");
76+
}
77+
}
78+
}
79+
}
80+
81+
/**
82+
* @return string[] Networks that are available
83+
*/
84+
public function getAvailable(): array
85+
{
86+
return $this->available;
87+
}
88+
}

src/Executor/Runner/NetworkManager.php

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)