Skip to content

Commit 3acd9c1

Browse files
committed
refactor: extract imagepuller
1 parent ec5813c commit 3acd9c1

File tree

8 files changed

+65
-122
lines changed

8 files changed

+65
-122
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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;
@@ -57,6 +58,10 @@
5758
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');
5859
$networkManager->connectAll($selfContainer);
5960

61+
/* Pull images */
62+
$imagePuller = new ImagePuller($orchestration);
63+
$imagePuller->pull(explode(',', System::getEnv('OPR_EXECUTOR_IMAGES') ?: ''));
64+
6065
/* Start maintenance task */
6166
$maintenance = new Maintenance($orchestration, $runtimes);
6267
$maintenance->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: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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;
@@ -34,52 +33,6 @@ public function __construct(
3433
private readonly Runtimes $runtimes,
3534
private readonly NetworkManager $networkManager
3635
) {
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());
8336
}
8437

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

0 commit comments

Comments
 (0)