|
1 | 1 | <?php |
2 | 2 |
|
3 | | -if (file_exists(__DIR__ . '/../vendor/autoload.php')) { |
4 | | - require_once __DIR__ . '/../vendor/autoload.php'; |
5 | | -} |
6 | | - |
| 3 | +require_once __DIR__ . '/../vendor/autoload.php'; |
7 | 4 | require_once __DIR__ . '/error.php'; |
8 | 5 | require_once __DIR__ . '/controllers.php'; |
9 | 6 |
|
10 | | -use OpenRuntimes\Executor\Runner\Docker; |
11 | 7 | use OpenRuntimes\Executor\Runner\ImagePuller; |
12 | 8 | use OpenRuntimes\Executor\Runner\Maintenance; |
13 | | -use OpenRuntimes\Executor\Runner\Repository\Runtimes; |
14 | 9 | use OpenRuntimes\Executor\Runner\Network; |
15 | | -use Swoole\Process; |
16 | 10 | use Swoole\Runtime; |
17 | 11 | use Utopia\Console; |
18 | 12 | use Utopia\Http\Http; |
19 | 13 | use Utopia\Http\Response; |
20 | 14 | use Utopia\Http\Adapter\Swoole\Server; |
21 | 15 | use Utopia\System\System; |
22 | | -use Utopia\Orchestration\Adapter\DockerAPI; |
23 | 16 | use Utopia\Orchestration\Orchestration; |
24 | 17 |
|
25 | 18 | use function Swoole\Coroutine\run; |
26 | 19 |
|
27 | | -// Unlimited memory limit to handle as many coroutines/requests as possible |
28 | | -ini_set('memory_limit', '-1'); |
29 | | - |
30 | 20 | $payloadSize = 22 * (1024 * 1024); |
31 | 21 | $settings = [ |
32 | 22 | 'package_max_length' => $payloadSize, |
|
37 | 27 |
|
38 | 28 | Http::setMode((string)System::getEnv('OPR_EXECUTOR_ENV', Http::MODE_TYPE_PRODUCTION)); |
39 | 29 |
|
| 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 | + |
40 | 59 | Http::onRequest() |
41 | 60 | ->inject('response') |
42 | 61 | ->action(function (Response $response) { |
43 | 62 | $response->addHeader('Server', 'Executor'); |
44 | 63 | }); |
45 | 64 |
|
46 | 65 | 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 | | - |
79 | 66 | $server = new Server('0.0.0.0', '80', $settings); |
80 | 67 | $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 | | - |
91 | 68 | $http->start(); |
92 | 69 | }); |
0 commit comments