Skip to content

Commit bfdd8e0

Browse files
authored
Merge pull request #194 from open-runtimes/chore-replace-http-getenv
chore: replace http::getenv
2 parents 2a864a0 + 3a08c36 commit bfdd8e0

File tree

6 files changed

+47
-46
lines changed

6 files changed

+47
-46
lines changed

app/controllers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
->param('remove', false, new Boolean(), 'Remove a runtime after execution.', true)
6767
->param('cpus', 1, new FloatValidator(true), 'Container CPU.', true)
6868
->param('memory', 512, new Integer(), 'Container RAM memory.', true)
69-
->param('version', 'v5', new WhiteList(\explode(',', Http::getEnv('OPR_EXECUTOR_RUNTIME_VERSIONS', 'v5') ?? 'v5')), 'Runtime Open Runtime version.', true)
69+
->param('version', 'v5', new WhiteList(\explode(',', System::getEnv('OPR_EXECUTOR_RUNTIME_VERSIONS', 'v5') ?? 'v5')), 'Runtime Open Runtime version.', true)
7070
->param('restartPolicy', DockerAPI::RESTART_NO, new WhiteList([DockerAPI::RESTART_NO, DockerAPI::RESTART_ALWAYS, DockerAPI::RESTART_ON_FAILURE, DockerAPI::RESTART_UNLESS_STOPPED], true), 'Define restart policy for the runtime once an exit code is returned. Default value is "no". Possible values are "no", "always", "on-failure", "unless-stopped".', true)
7171
->inject('response')
7272
->inject('log')
@@ -159,7 +159,7 @@
159159
->param('variables', [], new AnyOf([new Text(65535), new Assoc()], AnyOf::TYPE_MIXED), 'Environment variables passed into runtime.', true)
160160
->param('cpus', 1, new FloatValidator(true), 'Container CPU.', true)
161161
->param('memory', 512, new Integer(true), 'Container RAM memory.', true)
162-
->param('version', 'v5', new WhiteList(\explode(',', Http::getEnv('OPR_EXECUTOR_RUNTIME_VERSIONS', 'v5') ?? 'v5')), 'Runtime Open Runtime version.', true)
162+
->param('version', 'v5', new WhiteList(\explode(',', System::getEnv('OPR_EXECUTOR_RUNTIME_VERSIONS', 'v5') ?? 'v5')), 'Runtime Open Runtime version.', true)
163163
->param('runtimeEntrypoint', '', new Text(1024, 0), 'Commands to run when creating a container. Maximum of 100 commands are allowed, each 1024 characters long.', true)
164164
->param('logging', true, new Boolean(true), 'Whether executions will be logged.', true)
165165
->param('restartPolicy', DockerAPI::RESTART_NO, new WhiteList([DockerAPI::RESTART_NO, DockerAPI::RESTART_ALWAYS, DockerAPI::RESTART_ON_FAILURE, DockerAPI::RESTART_UNLESS_STOPPED], true), 'Define restart policy once exit code is returned by command. Default value is "no". Possible values are "no", "always", "on-failure", "unless-stopped".', true)
@@ -326,7 +326,7 @@ function (
326326
->inject('request')
327327
->action(function (Request $request) {
328328
$secretKey = \explode(' ', $request->getHeader('authorization', ''))[1] ?? '';
329-
if (empty($secretKey) || $secretKey !== Http::getEnv('OPR_EXECUTOR_SECRET', '')) {
329+
if (empty($secretKey) || $secretKey !== System::getEnv('OPR_EXECUTOR_SECRET', '')) {
330330
throw new Exception(Exception::GENERAL_UNAUTHORIZED, 'Missing executor key');
331331
}
332332
});

app/error.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Utopia\Logger\Log;
77
use Utopia\Logger\Logger;
88
use Utopia\Http\Response;
9+
use Utopia\System\System;
910

1011
function logError(Log $log, Throwable $error, ?Logger $logger = null): void
1112
{
@@ -58,7 +59,7 @@ function logError(Log $log, Throwable $error, ?Logger $logger = null): void
5859
'type' => $exception instanceof Exception ? $exception->getType() : Exception::GENERAL_UNKNOWN,
5960
'message' => $exception->getMessage(),
6061
'code' => $code,
61-
'version' => Http::getEnv('OPR_EXECUTOR_VERSION', 'unknown')
62+
'version' => System::getEnv('OPR_EXECUTOR_VERSION', 'unknown')
6263
];
6364

6465
// If in development, include some additional details.

app/http.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Utopia\Http\Http;
1414
use Utopia\Http\Response;
1515
use Utopia\Http\Adapter\Swoole\Server;
16+
use Utopia\System\System;
1617
use Utopia\Orchestration\Adapter\DockerAPI;
1718
use Utopia\Orchestration\Orchestration;
1819

@@ -23,7 +24,7 @@
2324

2425
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL);
2526

26-
Http::setMode((string)Http::getEnv('OPR_EXECUTOR_ENV', Http::MODE_TYPE_PRODUCTION));
27+
Http::setMode((string)System::getEnv('OPR_EXECUTOR_ENV', Http::MODE_TYPE_PRODUCTION));
2728
Http::onRequest()
2829
->inject('response')
2930
->action(function (Response $response) {
@@ -32,10 +33,10 @@
3233

3334
run(function () {
3435
$orchestration = new Orchestration(new DockerAPI(
35-
Http::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', ''),
36-
Http::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '')
36+
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', ''),
37+
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '')
3738
));
38-
$networks = explode(',', Http::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes');
39+
$networks = explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes');
3940
$runner = new Docker($orchestration, $networks);
4041

4142
Http::setResource('runner', fn () => $runner);

app/init.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
$register = new Registry();
2323

2424
$register->set('logger', function () {
25-
$providerName = Http::getEnv('OPR_EXECUTOR_LOGGING_PROVIDER', '');
26-
$providerConfig = Http::getEnv('OPR_EXECUTOR_LOGGING_CONFIG', '');
25+
$providerName = System::getEnv('OPR_EXECUTOR_LOGGING_PROVIDER', '');
26+
$providerConfig = System::getEnv('OPR_EXECUTOR_LOGGING_CONFIG', '');
2727

2828
try {
2929
$loggingProvider = new DSN($providerConfig ?? '');

src/Executor/Runner/Adapter.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use OpenRuntimes\Executor\Stats;
66
use Utopia\CLI\Console;
77
use Utopia\DSN\DSN;
8-
use Utopia\Http\Http;
98
use Utopia\Http\Response;
109
use Utopia\Logger\Log;
1110
use Utopia\Storage\Device;
@@ -17,6 +16,7 @@
1716
use Utopia\Storage\Device\S3;
1817
use Utopia\Storage\Device\Wasabi;
1918
use Utopia\Storage\Storage;
19+
use Utopia\System\System;
2020

2121
abstract class Adapter
2222
{
@@ -134,7 +134,7 @@ protected function getStorageDevice(
134134
string $root,
135135
string $region = ''
136136
): Device {
137-
$connections = Http::getEnv('OPR_EXECUTOR_CONNECTION_STORAGE', '') ?? '';
137+
$connections = System::getEnv('OPR_EXECUTOR_CONNECTION_STORAGE', '') ?? '';
138138

139139
if (\preg_match('/^\w+=/', $connections)) {
140140
// Multi region
@@ -169,7 +169,7 @@ protected function getStorageDevice(
169169
$bucket = $dsn->getPath() ?? '';
170170
$region = $dsn->getParam('region');
171171
$insecure = $dsn->getParam('insecure', 'false') === 'true';
172-
$url = Http::getEnv('OPR_EXECUTOR_STORAGE_S3_ENDPOINT', '');
172+
$url = System::getEnv('OPR_EXECUTOR_STORAGE_S3_ENDPOINT', '');
173173
} catch (\Exception $e) {
174174
Console::warning($e->getMessage() . 'Invalid DSN. Defaulting to Local device.');
175175
}
@@ -198,18 +198,18 @@ protected function getStorageDevice(
198198
return new Local($root);
199199
}
200200
} else {
201-
switch (strtolower(Http::getEnv('OPR_EXECUTOR_STORAGE_DEVICE', Storage::DEVICE_LOCAL) ?? '')) {
201+
switch (strtolower(System::getEnv('OPR_EXECUTOR_STORAGE_DEVICE', Storage::DEVICE_LOCAL) ?? '')) {
202202
case Storage::DEVICE_LOCAL:
203203
default:
204204
return new Local($root);
205205
case Storage::DEVICE_S3:
206-
$s3AccessKey = Http::getEnv('OPR_EXECUTOR_STORAGE_S3_ACCESS_KEY', '') ?? '';
207-
$s3SecretKey = Http::getEnv('OPR_EXECUTOR_STORAGE_S3_SECRET', '') ?? '';
208-
$s3Host = Http::getEnv('OPR_EXECUTOR_STORAGE_S3_HOST', '') ?? '';
209-
$s3Region = Http::getEnv('OPR_EXECUTOR_STORAGE_S3_REGION', '') ?? '';
210-
$s3Bucket = Http::getEnv('OPR_EXECUTOR_STORAGE_S3_BUCKET', '') ?? '';
206+
$s3AccessKey = System::getEnv('OPR_EXECUTOR_STORAGE_S3_ACCESS_KEY', '') ?? '';
207+
$s3SecretKey = System::getEnv('OPR_EXECUTOR_STORAGE_S3_SECRET', '') ?? '';
208+
$s3Host = System::getEnv('OPR_EXECUTOR_STORAGE_S3_HOST', '') ?? '';
209+
$s3Region = System::getEnv('OPR_EXECUTOR_STORAGE_S3_REGION', '') ?? '';
210+
$s3Bucket = System::getEnv('OPR_EXECUTOR_STORAGE_S3_BUCKET', '') ?? '';
211211
$s3Acl = 'private';
212-
$s3EndpointUrl = Http::getEnv('OPR_EXECUTOR_STORAGE_S3_ENDPOINT', '');
212+
$s3EndpointUrl = System::getEnv('OPR_EXECUTOR_STORAGE_S3_ENDPOINT', '');
213213
if (!empty($s3EndpointUrl)) {
214214
$bucketRoot = (!empty($s3Bucket) ? $s3Bucket . '/' : '') . \ltrim($root, '/');
215215
return new S3($bucketRoot, $s3AccessKey, $s3SecretKey, $s3EndpointUrl, $s3Region, $s3Acl);
@@ -220,31 +220,31 @@ protected function getStorageDevice(
220220
}
221221
// no break
222222
case Storage::DEVICE_DO_SPACES:
223-
$doSpacesAccessKey = Http::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_ACCESS_KEY', '') ?? '';
224-
$doSpacesSecretKey = Http::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_SECRET', '') ?? '';
225-
$doSpacesRegion = Http::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_REGION', '') ?? '';
226-
$doSpacesBucket = Http::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_BUCKET', '') ?? '';
223+
$doSpacesAccessKey = System::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_ACCESS_KEY', '') ?? '';
224+
$doSpacesSecretKey = System::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_SECRET', '') ?? '';
225+
$doSpacesRegion = System::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_REGION', '') ?? '';
226+
$doSpacesBucket = System::getEnv('OPR_EXECUTOR_STORAGE_DO_SPACES_BUCKET', '') ?? '';
227227
$doSpacesAcl = 'private';
228228
return new DOSpaces($root, $doSpacesAccessKey, $doSpacesSecretKey, $doSpacesBucket, $doSpacesRegion, $doSpacesAcl);
229229
case Storage::DEVICE_BACKBLAZE:
230-
$backblazeAccessKey = Http::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_ACCESS_KEY', '') ?? '';
231-
$backblazeSecretKey = Http::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_SECRET', '') ?? '';
232-
$backblazeRegion = Http::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_REGION', '') ?? '';
233-
$backblazeBucket = Http::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_BUCKET', '') ?? '';
230+
$backblazeAccessKey = System::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_ACCESS_KEY', '') ?? '';
231+
$backblazeSecretKey = System::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_SECRET', '') ?? '';
232+
$backblazeRegion = System::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_REGION', '') ?? '';
233+
$backblazeBucket = System::getEnv('OPR_EXECUTOR_STORAGE_BACKBLAZE_BUCKET', '') ?? '';
234234
$backblazeAcl = 'private';
235235
return new Backblaze($root, $backblazeAccessKey, $backblazeSecretKey, $backblazeBucket, $backblazeRegion, $backblazeAcl);
236236
case Storage::DEVICE_LINODE:
237-
$linodeAccessKey = Http::getEnv('OPR_EXECUTOR_STORAGE_LINODE_ACCESS_KEY', '') ?? '';
238-
$linodeSecretKey = Http::getEnv('OPR_EXECUTOR_STORAGE_LINODE_SECRET', '') ?? '';
239-
$linodeRegion = Http::getEnv('OPR_EXECUTOR_STORAGE_LINODE_REGION', '') ?? '';
240-
$linodeBucket = Http::getEnv('OPR_EXECUTOR_STORAGE_LINODE_BUCKET', '') ?? '';
237+
$linodeAccessKey = System::getEnv('OPR_EXECUTOR_STORAGE_LINODE_ACCESS_KEY', '') ?? '';
238+
$linodeSecretKey = System::getEnv('OPR_EXECUTOR_STORAGE_LINODE_SECRET', '') ?? '';
239+
$linodeRegion = System::getEnv('OPR_EXECUTOR_STORAGE_LINODE_REGION', '') ?? '';
240+
$linodeBucket = System::getEnv('OPR_EXECUTOR_STORAGE_LINODE_BUCKET', '') ?? '';
241241
$linodeAcl = 'private';
242242
return new Linode($root, $linodeAccessKey, $linodeSecretKey, $linodeBucket, $linodeRegion, $linodeAcl);
243243
case Storage::DEVICE_WASABI:
244-
$wasabiAccessKey = Http::getEnv('OPR_EXECUTOR_STORAGE_WASABI_ACCESS_KEY', '') ?? '';
245-
$wasabiSecretKey = Http::getEnv('OPR_EXECUTOR_STORAGE_WASABI_SECRET', '') ?? '';
246-
$wasabiRegion = Http::getEnv('OPR_EXECUTOR_STORAGE_WASABI_REGION', '') ?? '';
247-
$wasabiBucket = Http::getEnv('OPR_EXECUTOR_STORAGE_WASABI_BUCKET', '') ?? '';
244+
$wasabiAccessKey = System::getEnv('OPR_EXECUTOR_STORAGE_WASABI_ACCESS_KEY', '') ?? '';
245+
$wasabiSecretKey = System::getEnv('OPR_EXECUTOR_STORAGE_WASABI_SECRET', '') ?? '';
246+
$wasabiRegion = System::getEnv('OPR_EXECUTOR_STORAGE_WASABI_REGION', '') ?? '';
247+
$wasabiBucket = System::getEnv('OPR_EXECUTOR_STORAGE_WASABI_BUCKET', '') ?? '';
248248
$wasabiAcl = 'private';
249249
return new Wasabi($root, $wasabiAccessKey, $wasabiSecretKey, $wasabiBucket, $wasabiRegion, $wasabiAcl);
250250
}

src/Executor/Runner/Docker.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Swoole\Timer;
1414
use Throwable;
1515
use Utopia\CLI\Console;
16-
use Utopia\Http\Http;
1716
use Utopia\Http\Response;
1817
use Utopia\Logger\Log;
1918
use Utopia\Orchestration\Orchestration;
@@ -82,13 +81,13 @@ private function init(array $networks): void
8281
/**
8382
* Warmup: make sure images are ready to run fast 🚀
8483
*/
85-
$allowList = empty(Http::getEnv('OPR_EXECUTOR_RUNTIMES')) ? [] : \explode(',', Http::getEnv('OPR_EXECUTOR_RUNTIMES'));
84+
$allowList = empty(System::getEnv('OPR_EXECUTOR_RUNTIMES')) ? [] : \explode(',', System::getEnv('OPR_EXECUTOR_RUNTIMES'));
8685

87-
if (Http::getEnv('OPR_EXECUTOR_IMAGE_PULL', 'enabled') === 'disabled') {
86+
if (System::getEnv('OPR_EXECUTOR_IMAGE_PULL', 'enabled') === 'disabled') {
8887
// Useful to prevent auto-pulling from remote when testing local images
8988
Console::info("Skipping image pulling");
9089
} else {
91-
$runtimeVersions = \explode(',', Http::getEnv('OPR_EXECUTOR_RUNTIME_VERSIONS', 'v5') ?? 'v5');
90+
$runtimeVersions = \explode(',', System::getEnv('OPR_EXECUTOR_RUNTIME_VERSIONS', 'v5') ?? 'v5');
9291
foreach ($runtimeVersions as $runtimeVersion) {
9392
Console::success("Pulling $runtimeVersion images...");
9493
$runtimes = new Runtimes($runtimeVersion); // TODO: @Meldiron Make part of open runtimes
@@ -116,12 +115,12 @@ private function init(array $networks): void
116115
* Run a maintenance worker every X seconds to remove inactive runtimes
117116
*/
118117
Console::info('Starting maintenance interval...');
119-
$interval = (int)Http::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'); // In seconds
118+
$interval = (int)System::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'); // In seconds
120119
Timer::tick($interval * 1000, function () {
121120
Console::info("Running maintenance task ...");
122121
// Stop idling runtimes
123122
foreach ($this->activeRuntimes as $runtimeName => $runtime) {
124-
$inactiveThreshold = \time() - \intval(Http::getEnv('OPR_EXECUTOR_INACTIVE_THRESHOLD', '60'));
123+
$inactiveThreshold = \time() - \intval(System::getEnv('OPR_EXECUTOR_INACTIVE_THRESHOLD', '60'));
125124
if ($runtime['updated'] < $inactiveThreshold) {
126125
go(function () use ($runtimeName, $runtime) {
127126
try {
@@ -780,7 +779,7 @@ public function createExecution(
780779
\curl_setopt($ch, CURLOPT_HTTPHEADER, [
781780
'Content-Type: application/json',
782781
'Content-Length: ' . \strlen($body ?: ''),
783-
'authorization: Bearer ' . Http::getEnv('OPR_EXECUTOR_SECRET', '')
782+
'authorization: Bearer ' . System::getEnv('OPR_EXECUTOR_SECRET', '')
784783
]);
785784

786785
$executorResponse = \curl_exec($ch);
@@ -1125,8 +1124,8 @@ public function createExecution(
11251124
// Execute function
11261125
$executionRequest = $version === 'v2' ? $executeV2 : $executeV5;
11271126

1128-
$retryDelayMs = \intval(Http::getEnv('OPR_EXECUTOR_RETRY_DELAY_MS', '500'));
1129-
$retryAttempts = \intval(Http::getEnv('OPR_EXECUTOR_RETRY_ATTEMPTS', '5'));
1127+
$retryDelayMs = \intval(System::getEnv('OPR_EXECUTOR_RETRY_DELAY_MS', '500'));
1128+
$retryAttempts = \intval(System::getEnv('OPR_EXECUTOR_RETRY_ATTEMPTS', '5'));
11301129

11311130
$attempts = 0;
11321131
do {
@@ -1262,7 +1261,7 @@ private function createNetworks(array $networks): array
12621261
}
12631262
batch($jobs);
12641263

1265-
$image = Http::getEnv('OPR_EXECUTOR_IMAGE', '');
1264+
$image = System::getEnv('OPR_EXECUTOR_IMAGE', '');
12661265
$containers = $this->orchestration->list(['label' => "com.openruntimes.executor.image=$image"]);
12671266

12681267
if (count($containers) < 1) {

0 commit comments

Comments
 (0)