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

0.1.x to main #28

Merged
merged 20 commits into from
Aug 8, 2023
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
70 changes: 49 additions & 21 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
$register = new Registry();

/**
* Create logger for cloud logging
*/
* Create logger for cloud logging
*/
$register->set('logger', function () {
$providerName = App::getEnv('OPR_EXECUTOR_LOGGING_PROVIDER', '');
$providerConfig = App::getEnv('OPR_EXECUTOR_LOGGING_CONFIG', '');
Expand Down Expand Up @@ -93,8 +93,8 @@
});

/**
* Create a Swoole table to store runtime information
*/
* Create a Swoole table to store runtime information
*/
$register->set('activeRuntimes', function () {
$table = new Table(1024);

Expand All @@ -111,8 +111,8 @@
});

/**
* Create a Swoole table of usage stats (separate for host and containers)
*/
* Create a Swoole table of usage stats (separate for host and containers)
*/
$register->set('statsContainers', function () {
$table = new Table(1024);

Expand Down Expand Up @@ -141,17 +141,21 @@
App::setResource('orchestrationConnection', fn (Pool $orchestrationPool) => $orchestrationPool->pop(), ['orchestrationPool']);
App::setResource('orchestration', fn (Connection $orchestrationConnection) => $orchestrationConnection->getResource(), ['orchestrationConnection']);

function logError(Throwable $error, string $action, Logger $logger = null, Utopia\Route $route = null): void
App::setResource('log', fn () => new Log());

function logError(Log $log, Throwable $error, string $action, Logger $logger = null, Utopia\Route $route = null): void
{
Console::error('[Error] Type: ' . get_class($error));
Console::error('[Error] Message: ' . $error->getMessage());
Console::error('[Error] File: ' . $error->getFile());
Console::error('[Error] Line: ' . $error->getLine());

if ($logger) {
$version = (string) App::getEnv('OPR_EXECUTOR_VERSION', 'UNKNOWN');
if ($logger && ($error->getCode() === 500 || $error->getCode() === 0)) {
$version = (string) App::getEnv('OPR_EXECUTOR_VERSION', '');
if (empty($version)) {
$version = 'UNKNOWN';
}

$log = new Log();
$log->setNamespace("executor");
$log->setServer(\gethostname() !== false ? \gethostname() : null);
$log->setVersion($version);
Expand All @@ -163,7 +167,7 @@ function logError(Throwable $error, string $action, Logger $logger = null, Utopi
$log->addTag('url', $route->getPath());
}

$log->addTag('code', $error->getCode());
$log->addTag('code', \strval($error->getCode()));
$log->addTag('verboseType', get_class($error));

$log->addExtra('file', $error->getFile());
Expand Down Expand Up @@ -281,12 +285,15 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
->inject('orchestration')
->inject('activeRuntimes')
->inject('response')
->action(function (string $runtimeId, string $image, string $entrypoint, string $source, string $destination, array $variables, array $commands, int $timeout, string $workdir, bool $remove, int $cpus, int $memory, Orchestration $orchestration, Table $activeRuntimes, Response $response) {
->inject('log')
->action(function (string $runtimeId, string $image, string $entrypoint, string $source, string $destination, array $variables, array $commands, int $timeout, string $workdir, bool $remove, int $cpus, int $memory, Orchestration $orchestration, Table $activeRuntimes, Response $response, Log $log) {
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)

$runtimeHostname = \uniqid();

$log->addTag('runtimeId', $activeRuntimeId);

if ($activeRuntimes->exists($activeRuntimeId)) {
if ($activeRuntimes->get($activeRuntimeId)['status'] == 'pending') {
throw new \Exception('A runtime with the same ID is already being created. Attempt a execution soon.', 500);
Expand Down Expand Up @@ -452,7 +459,7 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
// Silently try to kill container
try {
$orchestration->remove($containerId, true);
} catch(Throwable $th) {
} catch (Throwable $th) {
}

$activeRuntimes->del($activeRuntimeId);
Expand Down Expand Up @@ -499,9 +506,12 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
->param('runtimeId', '', new Text(64), 'Runtime unique ID.')
->inject('activeRuntimes')
->inject('response')
->action(function (string $runtimeId, Table $activeRuntimes, Response $response) {
->inject('log')
->action(function (string $runtimeId, Table $activeRuntimes, Response $response, Log $log) {
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)

$log->addTag('runtimeId', $activeRuntimeId);

if (!$activeRuntimes->exists($activeRuntimeId)) {
throw new Exception('Runtime not found', 404);
}
Expand All @@ -519,10 +529,13 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
->inject('orchestration')
->inject('activeRuntimes')
->inject('response')
->action(function (string $runtimeId, Orchestration $orchestration, Table $activeRuntimes, Response $response) {
->inject('log')
->action(function (string $runtimeId, Orchestration $orchestration, Table $activeRuntimes, Response $response, Log $log) {
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)

$log->addTag('runtimeId', $activeRuntimeId);

if (!$activeRuntimes->exists($activeRuntimeId)) {
throw new Exception('Runtime not found', 404);
}
Expand Down Expand Up @@ -550,11 +563,14 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
->param('memory', 512, new Integer(), 'Comtainer RAM memory.', true)
->inject('activeRuntimes')
->inject('response')
->inject('log')
->action(
function (string $runtimeId, string $payload, array $variables, int $timeout, string $image, string $source, string $entrypoint, int $cpus, int $memory, Table $activeRuntimes, Response $response) {
function (string $runtimeId, string $payload, array $variables, int $timeout, string $image, string $source, string $entrypoint, int $cpus, int $memory, Table $activeRuntimes, Response $response, Log $log) {
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)

$log->addTag('runtimeId', $activeRuntimeId);

$variables = \array_merge($variables, [
'INERNAL_EXECUTOR_HOSTNAME' => System::getHostname()
]);
Expand Down Expand Up @@ -617,7 +633,7 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st

// Prepare runtime
for ($i = 0; $i < 5; $i++) {
[ 'errNo' => $errNo, 'error' => $error, 'statusCode' => $statusCode, 'executorResponse' => $executorResponse ] = \call_user_func($sendCreateRuntimeRequest);
['errNo' => $errNo, 'error' => $error, 'statusCode' => $statusCode, 'executorResponse' => $executorResponse] = \call_user_func($sendCreateRuntimeRequest);

if ($errNo === 0 && $statusCode < 500) {
$body = \json_decode($executorResponse, true);
Expand All @@ -635,6 +651,11 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
}
}

// Update swoole table
$runtime = $activeRuntimes->get($activeRuntimeId) ?? [];
$runtime['updated'] = \time();
$activeRuntimes->set($activeRuntimeId, $runtime);

// Ensure runtime started
for ($i = 0; $i < 5; $i++) {
if ($activeRuntimes->get($activeRuntimeId)['status'] !== 'pending') {
Expand Down Expand Up @@ -709,7 +730,7 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st

// Execute function
for ($i = 0; $i < 5; $i++) {
[ 'errNo' => $errNo, 'error' => $error, 'statusCode' => $statusCode, 'executorResponse' => $executorResponse ] = \call_user_func($sendExecuteRequest);
['errNo' => $errNo, 'error' => $error, 'statusCode' => $statusCode, 'executorResponse' => $executorResponse] = \call_user_func($sendExecuteRequest);

// No error
if ($errNo === 0) {
Expand Down Expand Up @@ -804,9 +825,10 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
->inject('logger')
->inject('request')
->inject('response')
->action(function (App $utopia, Throwable $error, ?Logger $logger, Request $request, Response $response) {
->inject('log')
->action(function (App $utopia, Throwable $error, ?Logger $logger, Request $request, Response $response, Log $log) {
$route = $utopia->match($request);
logError($error, "httpError", $logger, $route);
logError($log, $error, "httpError", $logger, $route);

switch ($error->getCode()) {
case 400: // Error allowed publicly
Expand Down Expand Up @@ -1011,11 +1033,17 @@ function getStats(Table $statsHost, Table $statsContainers, Orchestration $orche
} catch (\Throwable $th) {
$code = 500;


/**
* @var Logger $logger
*/
$logger = $app->getResource('logger');
logError($th, "serverError", $logger);

/**
* @var Log $log
*/
$log = $app->getResource('log');
logError($log, $th, "serverError", $logger);
$swooleResponse->setStatusCode($code);
$output = [
'message' => 'Error: ' . $th->getMessage(),
Expand Down
Loading