Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: getting executor name #100

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ OPR_EXECUTOR_CONNECTION_STORAGE=file://localhost
OPR_EXECUTOR_INACTIVE_TRESHOLD=60
OPR_EXECUTOR_MAINTENANCE_INTERVAL=60
OPR_EXECUTOR_NETWORK=runtimes-1,runtimes-2
OPR_EXECUTOR_NAME=executor
OPR_EXECUTOR_IMAGE=ldoes-not-exist
OPR_EXECUTOR_SECRET=executor-secret-key
OPR_EXECUTOR_LOGGING_PROVIDER=
OPR_EXECUTOR_LOGGING_CONFIG=
OPR_EXECUTOR_DOCKER_HUB_USERNAME=
OPR_EXECUTOR_DOCKER_HUB_PASSWORD=
OPR_EXECUTOR_RUNTIME_VERSIONS=v3
OPR_EXECUTOR_RUNTIME_VERSIONS=v3
52 changes: 33 additions & 19 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL);

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

// Setup Registry
$register = new Registry();
Expand Down Expand Up @@ -81,8 +81,8 @@
* Create orchestration
*/
$register->set('orchestration', function () {
$dockerUser = (string) Http::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', '');
$dockerPass = (string) Http::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '');
$dockerUser = (string)Http::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', '');
$dockerPass = (string)Http::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '');
$orchestration = new Orchestration(new DockerCLI($dockerUser, $dockerPass));

return $orchestration;
Expand Down Expand Up @@ -145,7 +145,7 @@ function logError(Log $log, Throwable $error, string $action, Logger $logger = n
Console::error('[Error] Line: ' . $error->getLine());

if ($logger && ($error->getCode() === 500 || $error->getCode() === 0)) {
$version = (string) Http::getEnv('OPR_EXECUTOR_VERSION', '');
$version = (string)Http::getEnv('OPR_EXECUTOR_VERSION', '');
if (empty($version)) {
$version = 'UNKNOWN';
}
Expand Down Expand Up @@ -261,14 +261,25 @@ function getStorageDevice(string $root): Device
}
}


/**
* @param array<string> $networks
*
* @return array<string>
*/
function createNetworks(Orchestration $orchestration, array $networks): array
{
$containerName = Http::getEnv('OPR_EXECUTOR_NAME') ?? 'executor';
$imageName = Http::getEnv('OPR_EXECUTOR_IMAGE') ;
if (empty($imageName)) {
throw new \Exception('Executor image name is not set');
}
$containers = $orchestration->list(['ancestor' => $imageName, 'status' => 'running']);

if (count($containers) < 1) {
throw new \Exception('No running executor found');
}

$containerName = $containers[0]->getName();
$createdNetworks = [];
$jobs = [];

Expand Down Expand Up @@ -502,18 +513,21 @@ function cleanUp(Orchestration $orchestration, Table $activeRuntimes, array $net
/**
* Create container
*/
$variables = \array_merge($variables, match ($version) {
'v2' => [
'INTERNAL_RUNTIME_KEY' => $secret,
'INTERNAL_RUNTIME_ENTRYPOINT' => $entrypoint,
'INERNAL_EXECUTOR_HOSTNAME' => System::getHostname()
],
'v3' => [
'OPEN_RUNTIMES_SECRET' => $secret,
'OPEN_RUNTIMES_ENTRYPOINT' => $entrypoint,
'OPEN_RUNTIMES_HOSTNAME' => System::getHostname()
]
});
$variables = \array_merge(
$variables,
match ($version) {
'v2' => [
'INTERNAL_RUNTIME_KEY' => $secret,
'INTERNAL_RUNTIME_ENTRYPOINT' => $entrypoint,
'INERNAL_EXECUTOR_HOSTNAME' => System::getHostname()
],
'v3' => [
'OPEN_RUNTIMES_SECRET' => $secret,
'OPEN_RUNTIMES_ENTRYPOINT' => $entrypoint,
'OPEN_RUNTIMES_HOSTNAME' => System::getHostname()
]
}
);

$variables = array_map(fn ($v) => strval($v), $variables);
$orchestration
Expand Down Expand Up @@ -1155,7 +1169,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
$response
->setStatusCode(Response::STATUS_CODE_OK)
->setContentType(Response::CONTENT_TYPE_JSON, Response::CHARSET_UTF8)
->send((string) $executionString);
->send((string)$executionString);
}
);

Expand Down Expand Up @@ -1296,7 +1310,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
* Run a maintenance worker every X seconds to remove inactive runtimes
*/
Console::info('Starting maintenance interval...');
$interval = (int) Http::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'); // In seconds
$interval = (int)Http::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'); // In seconds
Timer::tick($interval * 1000, function () use ($orchestration, $activeRuntimes) {
Console::info("Running maintenance task ...");
// Stop idling runtimes
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ version: '3'

services:
openruntimes-executor:
container_name: $OPR_EXECUTOR_NAME
hostname: $OPR_EXECUTOR_NAME
hostname: executor
image: $OPR_EXECUTOR_IMAGE
<<: *x-logging
stop_signal: SIGINT
build:
Expand All @@ -42,7 +42,7 @@ services:
- OPR_EXECUTOR_MAINTENANCE_INTERVAL
- OPR_EXECUTOR_NETWORK
- OPR_EXECUTOR_SECRET
- OPR_EXECUTOR_NAME
- OPR_EXECUTOR_IMAGE
- OPR_EXECUTOR_LOGGING_PROVIDER
- OPR_EXECUTOR_LOGGING_CONFIG
- OPR_EXECUTOR_DOCKER_HUB_USERNAME
Expand All @@ -55,4 +55,4 @@ services:

volumes:
openruntimes-builds:
openruntimes-functions:
openruntimes-functions:
Loading