Skip to content

Commit be8e6b7

Browse files
authored
Merge pull request #213 from open-runtimes/refactor-extract-imagepuller
refactor: extract imagepuller
2 parents ec5813c + af7365e commit be8e6b7

File tree

8 files changed

+75
-124
lines changed

8 files changed

+75
-124
lines changed

.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
OPR_EXECUTOR_ENV=development
2-
OPR_EXECUTOR_IMAGE_PULL=enabled
3-
OPR_EXECUTOR_RUNTIMES=php-8.1,dart-2.18,deno-1.24,node-18.0,python-3.10,ruby-3.1,cpp-17,dotnet-6.0
2+
OPR_EXECUTOR_IMAGES=openruntimes/php:v5-8.1,openruntimes/php:v2-8.1,openruntimes/dart:v5-2.18,openruntimes/deno:v5-1.24,openruntimes/deno:v2-1.24,openruntimes/node:v5-18.0,openruntimes/node:v2-18.0,openruntimes/python:v5-3.10,openruntimes/python:v2-3.10,openruntimes/ruby:v5-3.1,openruntimes/ruby:v2-3.1,openruntimes/cpp:v5-17,openruntimes/cpp:v2-17,openruntimes/dotnet:v5-6.0,openruntimes/dotnet:v2-6.0
43
OPR_EXECUTOR_CONNECTION_STORAGE=local://localhost
54
OPR_EXECUTOR_INACTIVE_THRESHOLD=60
65
OPR_EXECUTOR_MAINTENANCE_INTERVAL=60

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ services:
4949
- ./functions:/storage/functions:rw
5050
environment:
5151
- OPR_EXECUTOR_ENV
52-
- OPR_EXECUTOR_RUNTIMES
52+
- OPR_EXECUTOR_IMAGES
5353
- OPR_EXECUTOR_CONNECTION_STORAGE
5454
- OPR_EXECUTOR_INACTIVE_THRESHOLD
5555
- OPR_EXECUTOR_MAINTENANCE_INTERVAL
@@ -79,9 +79,8 @@ volumes:
7979

8080
```
8181
OPR_EXECUTOR_ENV=development
82-
OPR_EXECUTOR_RUNTIMES=php-8.0
82+
OPR_EXECUTOR_IMAGES=openruntimes/php:v5-8.3
8383
OPR_EXECUTOR_CONNECTION_STORAGE=file://localhost
84-
OPR_EXECUTOR_IMAGE_PULL=enabled
8584
OPR_EXECUTOR_INACTIVE_THRESHOLD=60
8685
OPR_EXECUTOR_MAINTENANCE_INTERVAL=60
8786
OPR_EXECUTOR_NETWORK=openruntimes-runtimes
@@ -192,7 +191,7 @@ docker compose down
192191
| Variable name | Description |
193192
|------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
194193
| OPR_EXECUTOR_ENV | Environment mode of the executor, ex. `development` |
195-
| OPR_EXECUTOR_RUNTIMES | Comma-separated list of supported runtimes `(ex: php-8.1,dart-2.18,deno-1.24,..)`. These runtimes should be available as container images. |
194+
| OPR_EXECUTOR_IMAGES | Comma-separated list of supported images `(ex: openruntimes/php:v5-8.1,openruntimes/php:v2-8.1,..)`. |
196195
| OPR_EXECUTOR_CONNECTION_STORAGE | DSN string that represents a connection to your storage device, ex: `file://localhost` for local storage |
197196
| OPR_EXECUTOR_INACTIVE_THRESHOLD | Threshold time (in seconds) for detecting inactive runtimes, ex: `60` |
198197
| OPR_EXECUTOR_MAINTENANCE_INTERVAL| Interval (in seconds) at which the Executor performs maintenance tasks, ex: `60` |
@@ -205,7 +204,6 @@ docker compose down
205204
| OPR_EXECUTOR_RUNTIME_VERSIONS | Version tag for runtime environments, ex: `v5` |
206205
| OPR_EXECUTOR_RETRY_ATTEMPTS | Number of retry attempts for failed executions, ex: `5` |
207206
| OPR_EXECUTOR_RETRY_DELAY_MS | Delay (in milliseconds) between retry attempts, ex: `500` |
208-
| OPR_EXECUTOR_IMAGE_PULL | Pull open runtimes images before executor starts. Takes `disabled` and `enabled` |
209207

210208
## Contributing
211209

app/http.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
require_once __DIR__ . '/controllers.php';
99

1010
use OpenRuntimes\Executor\Runner\Docker;
11+
use OpenRuntimes\Executor\Runner\ImagePuller;
1112
use OpenRuntimes\Executor\Runner\Maintenance;
1213
use OpenRuntimes\Executor\Runner\Repository\Runtimes;
1314
use OpenRuntimes\Executor\Runner\NetworkManager;
15+
use Swoole\Process;
1416
use Swoole\Runtime;
1517
use Utopia\Console;
1618
use Utopia\Http\Http;
@@ -57,6 +59,10 @@
5759
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');
5860
$networkManager->connectAll($selfContainer);
5961

62+
/* Pull images */
63+
$imagePuller = new ImagePuller($orchestration);
64+
$imagePuller->pull(explode(',', System::getEnv('OPR_EXECUTOR_IMAGES') ?: ''));
65+
6066
/* Start maintenance task */
6167
$maintenance = new Maintenance($orchestration, $runtimes);
6268
$maintenance->start(
@@ -71,6 +77,12 @@
7177
$server = new Server('0.0.0.0', '80', $settings);
7278
$http = new Http($server, 'UTC');
7379

80+
Process::signal(SIGTERM, function () use ($maintenance, $runner) {
81+
// This doesn't actually work. We need to fix utopia-php/http@0.34.x
82+
$maintenance->stop();
83+
$runner->cleanUp();
84+
});
85+
7486
Console::success('Executor is ready.');
7587

7688
$http->start();

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"ext-curl": "*",
2121
"ext-json": "*",
2222
"ext-swoole": "*",
23-
"appwrite/php-runtimes": "0.19.*",
2423
"utopia-php/config": "^0.2.2",
2524
"utopia-php/console": "0.0.*",
2625
"utopia-php/dsn": "0.2.*",

composer.lock

Lines changed: 8 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
x-logging: &x-logging
77
logging:
8-
driver: 'json-file'
8+
driver: "json-file"
99
options:
10-
max-file: '5'
11-
max-size: '10m'
10+
max-file: "5"
11+
max-size: "10m"
1212

1313
services:
1414
openruntimes-executor:
@@ -40,7 +40,7 @@ services:
4040
- ./tests/resources/sites:/storage/sites:rw
4141
environment:
4242
- OPR_EXECUTOR_ENV
43-
- OPR_EXECUTOR_RUNTIMES
43+
- OPR_EXECUTOR_IMAGES
4444
- OPR_EXECUTOR_CONNECTION_STORAGE
4545
- OPR_EXECUTOR_INACTIVE_THRESHOLD
4646
- OPR_EXECUTOR_MAINTENANCE_INTERVAL

src/Executor/Runner/Docker.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
namespace OpenRuntimes\Executor\Runner;
44

55
use OpenRuntimes\Executor\Logs;
6-
use Appwrite\Runtimes\Runtimes as AppwriteRuntimes;
76
use OpenRuntimes\Executor\Exception;
87
use OpenRuntimes\Executor\Runner\Repository\Runtimes;
98
use OpenRuntimes\Executor\StorageFactory;
109
use OpenRuntimes\Executor\Validator\TCP;
11-
use Swoole\Process;
1210
use Swoole\Timer;
1311
use Throwable;
1412
use Utopia\Console;
@@ -34,52 +32,6 @@ public function __construct(
3432
private readonly Runtimes $runtimes,
3533
private readonly NetworkManager $networkManager
3634
) {
37-
$this->init();
38-
}
39-
40-
/**
41-
* @return void
42-
* @throws \Utopia\Http\Exception
43-
*/
44-
private function init(): void
45-
{
46-
/**
47-
* Warmup: make sure images are ready to run fast 🚀
48-
*/
49-
$allowList = empty(System::getEnv('OPR_EXECUTOR_RUNTIMES')) ? [] : \explode(',', System::getEnv('OPR_EXECUTOR_RUNTIMES'));
50-
51-
if (System::getEnv('OPR_EXECUTOR_IMAGE_PULL', 'enabled') === 'disabled') {
52-
// Useful to prevent auto-pulling from remote when testing local images
53-
Console::info("Skipping image pulling");
54-
} else {
55-
$runtimeVersions = \explode(',', System::getEnv('OPR_EXECUTOR_RUNTIME_VERSIONS', 'v5') ?? 'v5');
56-
foreach ($runtimeVersions as $runtimeVersion) {
57-
Console::success("Pulling $runtimeVersion images...");
58-
$images = new AppwriteRuntimes($runtimeVersion); // TODO: @Meldiron Make part of open runtimes
59-
$images = $images->getAll(true, $allowList);
60-
$callables = [];
61-
foreach ($images as $image) {
62-
$callables[] = function () use ($image) {
63-
Console::log('Warming up ' . $image['name'] . ' ' . $image['version'] . ' environment...');
64-
$response = $this->orchestration->pull($image['image']);
65-
if ($response) {
66-
Console::info("Successfully Warmed up {$image['name']} {$image['version']}!");
67-
} else {
68-
Console::warning("Failed to Warmup {$image['name']} {$image['version']}!");
69-
}
70-
};
71-
}
72-
73-
batch($callables);
74-
}
75-
}
76-
77-
Console::success("Image pulling finished.");
78-
79-
Process::signal(SIGINT, fn () => $this->cleanUp());
80-
Process::signal(SIGQUIT, fn () => $this->cleanUp());
81-
Process::signal(SIGKILL, fn () => $this->cleanUp());
82-
Process::signal(SIGTERM, fn () => $this->cleanUp());
8335
}
8436

8537
/**
@@ -1107,7 +1059,7 @@ public function createExecution(
11071059
/**
11081060
* @return void
11091061
*/
1110-
private function cleanUp(): void
1062+
public function cleanUp(): void
11111063
{
11121064
Console::log('Cleaning up containers and networks...');
11131065

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace OpenRuntimes\Executor\Runner;
4+
5+
use Utopia\Console;
6+
use Utopia\Orchestration\Orchestration;
7+
8+
use function Swoole\Coroutine\batch;
9+
10+
readonly class ImagePuller
11+
{
12+
public function __construct(private Orchestration $orchestration)
13+
{
14+
}
15+
16+
/**
17+
* Pulls images from the registry.
18+
*
19+
* @param list<string> $images
20+
*/
21+
public function pull(array $images): void
22+
{
23+
if (empty($images)) {
24+
Console::log('[ImagePuller] No images to pull.');
25+
return;
26+
}
27+
28+
$jobs = array_map(fn ($image) => function () use ($image) {
29+
if (!$this->orchestration->pull($image)) {
30+
Console::error("[ImagePuller] Failed to pull image $image");
31+
return;
32+
}
33+
34+
return true;
35+
}, $images);
36+
37+
go(function () use ($jobs) {
38+
$results = batch($jobs);
39+
$success = \count(array_filter($results));
40+
41+
Console::info("[ImagePuller] Pulled $success/". \count($jobs) . " images.");
42+
});
43+
44+
Console::info('[ImagePuller] Started pulling images.');
45+
}
46+
}

0 commit comments

Comments
 (0)