Skip to content

Commit ac39d1f

Browse files
authored
Merge pull request #217 from open-runtimes/refactor-dependencies
refactor: dependencies
2 parents 45e4a85 + c206e60 commit ac39d1f

File tree

2 files changed

+60
-53
lines changed

2 files changed

+60
-53
lines changed

app/http.php

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
<?php
22

3-
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
4-
require_once __DIR__ . '/../vendor/autoload.php';
5-
}
6-
3+
require_once __DIR__ . '/../vendor/autoload.php';
74
require_once __DIR__ . '/error.php';
85
require_once __DIR__ . '/controllers.php';
96

10-
use OpenRuntimes\Executor\Runner\Docker;
117
use OpenRuntimes\Executor\Runner\ImagePuller;
128
use OpenRuntimes\Executor\Runner\Maintenance;
13-
use OpenRuntimes\Executor\Runner\Repository\Runtimes;
149
use OpenRuntimes\Executor\Runner\Network;
15-
use Swoole\Process;
1610
use Swoole\Runtime;
1711
use Utopia\Console;
1812
use Utopia\Http\Http;
1913
use Utopia\Http\Response;
2014
use Utopia\Http\Adapter\Swoole\Server;
2115
use Utopia\System\System;
22-
use Utopia\Orchestration\Adapter\DockerAPI;
2316
use Utopia\Orchestration\Orchestration;
2417

2518
use function Swoole\Coroutine\run;
2619

27-
// Unlimited memory limit to handle as many coroutines/requests as possible
28-
ini_set('memory_limit', '-1');
29-
3020
$payloadSize = 22 * (1024 * 1024);
3121
$settings = [
3222
'package_max_length' => $payloadSize,
@@ -37,56 +27,43 @@
3727

3828
Http::setMode((string)System::getEnv('OPR_EXECUTOR_ENV', Http::MODE_TYPE_PRODUCTION));
3929

30+
Http::onStart()
31+
->inject('orchestration')
32+
->inject('network')
33+
->inject('imagePuller')
34+
->inject('maintenance')
35+
->action(function (Orchestration $orchestration, Network $network, ImagePuller $imagePuller, Maintenance $maintenance) {
36+
/* Fetch own container information */
37+
$hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname');
38+
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');
39+
40+
/* Create desired networks if they don't exist */
41+
$network->setup(
42+
explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes'),
43+
$selfContainer->getName()
44+
);
45+
Http::setResource('networks', fn (): array => $network->getAvailable());
46+
47+
/* Pull images */
48+
$imagePuller->pull(explode(',', System::getEnv('OPR_EXECUTOR_IMAGES') ?: ''));
49+
50+
/* Start maintenance task */
51+
$maintenance->start(
52+
(int)System::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'),
53+
(int)System::getEnv('OPR_EXECUTOR_INACTIVE_THRESHOLD', '60')
54+
);
55+
56+
Console::success('Executor is ready.');
57+
});
58+
4059
Http::onRequest()
4160
->inject('response')
4261
->action(function (Response $response) {
4362
$response->addHeader('Server', 'Executor');
4463
});
4564

4665
run(function () use ($settings) {
47-
$orchestration = new Orchestration(new DockerAPI(
48-
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', ''),
49-
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '')
50-
));
51-
$runtimes = new Runtimes();
52-
53-
/* Fetch own container information */
54-
$hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname');
55-
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');
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-
);
63-
64-
/* Pull images */
65-
$imagePuller = new ImagePuller($orchestration);
66-
$imagePuller->pull(explode(',', System::getEnv('OPR_EXECUTOR_IMAGES') ?: ''));
67-
68-
/* Start maintenance task */
69-
$maintenance = new Maintenance($orchestration, $runtimes);
70-
$maintenance->start(
71-
(int)System::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'),
72-
(int)System::getEnv('OPR_EXECUTOR_INACTIVE_THRESHOLD', '60')
73-
);
74-
75-
/* Runner service, used to manage runtimes */
76-
$runner = new Docker($orchestration, $runtimes, $network->getAvailable());
77-
Http::setResource('runner', fn () => $runner);
78-
7966
$server = new Server('0.0.0.0', '80', $settings);
8067
$http = new Http($server, 'UTC');
81-
82-
Process::signal(SIGTERM, function () use ($maintenance, $runner, $network) {
83-
// This doesn't actually work. We need to fix utopia-php/http@0.34.x
84-
$maintenance->stop();
85-
$network->cleanup();
86-
$runner->cleanup();
87-
});
88-
89-
Console::success('Executor is ready.');
90-
9168
$http->start();
9269
});

app/init.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
<?php
22

3+
use OpenRuntimes\Executor\Runner\Docker;
4+
use OpenRuntimes\Executor\Runner\ImagePuller;
5+
use OpenRuntimes\Executor\Runner\Maintenance;
6+
use OpenRuntimes\Executor\Runner\Network;
7+
use OpenRuntimes\Executor\Runner\Repository\Runtimes;
8+
use OpenRuntimes\Executor\Runner\Adapter;
9+
use Utopia\Http\Http;
10+
use Utopia\Orchestration\Adapter\DockerAPI;
11+
use Utopia\Orchestration\Orchestration;
12+
use Utopia\System\System;
13+
use Utopia\Registry\Registry;
314
use Utopia\Config\Config;
415

516
const MAX_LOG_SIZE = 5 * 1024 * 1024;
617
const MAX_BUILD_LOG_SIZE = 1 * 1000 * 1000;
718

819
Config::load('errors', __DIR__ . '/config/errors.php');
20+
21+
$registry = new Registry();
22+
23+
$registry->set('runtimes', fn () => new Runtimes());
24+
25+
Http::setResource('runtimes', fn () => $registry->get('runtimes'));
26+
27+
Http::setResource('orchestration', fn (): Orchestration => new Orchestration(new DockerAPI(
28+
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', ''),
29+
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '')
30+
)));
31+
32+
Http::setResource('network', fn (Orchestration $orchestration): Network => new Network($orchestration), ['orchestration']);
33+
34+
Http::setResource('imagePuller', fn (Orchestration $orchestration): ImagePuller => new ImagePuller($orchestration), ['orchestration']);
35+
36+
Http::setResource('maintenance', fn (Orchestration $orchestration, Runtimes $runtimes): Maintenance => new Maintenance($orchestration, $runtimes), ['orchestration', 'runtimes']);
37+
38+
Http::setResource('runner', fn (Orchestration $orchestration, Runtimes $runtimes, array $networks): Adapter => new Docker($orchestration, $runtimes, $networks), ['orchestration', 'runtimes', 'networks']);

0 commit comments

Comments
 (0)