Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 0.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Aug 8, 2023
2 parents c203f6f + b5a65a8 commit 76c2724
Show file tree
Hide file tree
Showing 27 changed files with 545 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
OPR_EXECUTOR_ENV=development
OPR_EXECUTOR_RUNTIMES=php-8.0
OPR_EXECUTOR_RUNTIMES=php-8.1,dart-2.17,deno-1.24,node-18.0,python-3.9,ruby-3.1,cpp-17
OPR_EXECUTOR_CONNECTION_STORAGE=file://localhost
OPR_EXECUTOR_INACTIVE_TRESHOLD=60
OPR_EXECUTOR_MAINTENANCE_INTERVAL=60
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ jobs:
docker pull composer:2.0
docker compose build
docker compose up -d
sleep 15
sleep 60
- name: Doctor
run: |
docker compose logs
docker ps
docker network ls
Expand Down
90 changes: 41 additions & 49 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@
$table = new Table(1024);

$table->column('id', Table::TYPE_STRING, 256);
$table->column('created', Table::TYPE_INT, 8);
$table->column('updated', Table::TYPE_INT, 8);
$table->column('created', Table::TYPE_FLOAT);
$table->column('updated', Table::TYPE_FLOAT);
$table->column('name', Table::TYPE_STRING, 256);
$table->column('hostname', Table::TYPE_STRING, 256);
$table->column('status', Table::TYPE_STRING, 128);
Expand Down Expand Up @@ -306,17 +306,16 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
$containerId = '';
$stdout = '';
$stderr = '';
$startTimeUnix = \microtime(true);
$endTimeUnix = 0;
$startTime = \microtime(true);

$secret = \bin2hex(\random_bytes(16));

$activeRuntimes->set($activeRuntimeId, [
'id' => $containerId,
'name' => $activeRuntimeId,
'hostname' => $runtimeHostname,
'created' => (int) $startTimeUnix,
'updated' => \time(),
'created' => $startTime,
'updated' => $startTime,
'status' => 'pending',
'key' => $secret,
]);
Expand Down Expand Up @@ -416,39 +415,41 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
throw new Exception('Something went wrong when starting runtime.', 500);
}

$size = $localDevice->getFileSize($tmpBuild);
$container['size'] = $size;

$destinationDevice = getStorageDevice($destination);
$outputPath = $destinationDevice->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION));
$path = $destinationDevice->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION));

$buffer = $localDevice->read($tmpBuild);
if (!$destinationDevice->write($outputPath, $buffer, $localDevice->getFileMimeType($tmpBuild))) {
if (!$destinationDevice->write($path, $buffer, $localDevice->getFileMimeType($tmpBuild))) {
throw new Exception('Failed to move built code to storage', 500);
};

$container['outputPath'] = $outputPath;
$container['path'] = $path;
}

if ($stdout === '') {
$stdout = 'Runtime created successfully!';
}

$endTimeUnix = \microtime(true);
$duration = $endTimeUnix - $startTimeUnix;
$endTime = \microtime(true);
$duration = $endTime - $startTime;

$container = array_merge($container, [
'status' => 'ready',
'stdout' => \mb_strcut($stdout, 0, 1000000), // Limit to 1MB
'stderr' => \mb_strcut($stderr, 0, 1000000), // Limit to 1MB
'startTimeUnix' => (int) $startTimeUnix,
'endTimeUnix' => (int) $endTimeUnix,
'startTime' => $startTime,
'duration' => $duration,
]);

$activeRuntimes->set($activeRuntimeId, [
'id' => $containerId,
'name' => $activeRuntimeId,
'hostname' => $runtimeHostname,
'created' => (int) $startTimeUnix,
'updated' => \time(),
'created' => $startTime,
'updated' => \microtime(true),
'status' => 'Up ' . \round($duration, 2) . 's',
'key' => $secret,
]);
Expand Down Expand Up @@ -574,7 +575,7 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
'INERNAL_EXECUTOR_HOSTNAME' => System::getHostname()
]);

$coldStartTime = 0;
$coldStartDuration = 0;

// Prepare runtime
if (!$activeRuntimes->exists($activeRuntimeId)) {
Expand Down Expand Up @@ -636,7 +637,7 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st

if ($errNo === 0 && $statusCode < 500) {
$body = \json_decode($executorResponse, true);
$coldStartTime = \floatval($body['duration']);
$coldStartDuration = \floatval($body['duration']);
break;
} elseif ($errNo !== 111) { // Connection Refused - see https://openswoole.com/docs/swoole-error-code
throw new Exception('An internal curl error has occurred while starting runtime! Error Msg: ' . $error, 500);
Expand Down Expand Up @@ -676,12 +677,12 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
throw new Exception('Runtime secret not found. Please re-create the runtime.', 500);
}

$executionStart = \microtime(true);
$startTime = \microtime(true);

// Prepare request to runtime
$sendExecuteRequest = function () use ($variables, $payload, $secret, $hostname, &$executionStart, $timeout) {
$sendExecuteRequest = function () use ($variables, $payload, $secret, $hostname, &$startTime, $timeout) {
// Restart execution timer to not could failed attempts
$executionStart = \microtime(true);
$startTime = \microtime(true);

$statusCode = 0;
$errNo = -1;
Expand All @@ -699,7 +700,7 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
\curl_setopt($ch, CURLOPT_POST, true);
\curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_TIMEOUT, $timeout + 2); // max 2 seconds expected network latency
\curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
\curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

\curl_setopt($ch, CURLOPT_HTTPHEADER, [
Expand Down Expand Up @@ -748,33 +749,25 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
}

// Extract response
$executorResponse = json_decode($executorResponse ?? '{}', false);

$execution = [];
$stdout = '';
$stderr = '';
$res = '';

$executorResponse = json_decode($executorResponse ?? '{}', true);

switch (true) {
case $statusCode >= 500:
$stderr = $executorResponse['stderr'] ?? '';
$stdout = $executorResponse['stdout'] ?? '';
break;
case $statusCode >= 100:
$stdout = $executorResponse['stdout'] ?? '';
$res = $executorResponse['response'] ?? '';
if (is_array($res)) {
$res = json_encode($res, JSON_UNESCAPED_UNICODE);
}
break;
default:
$stderr = $executorResponse['stderr'] ?? '';
$stdout = $executorResponse['stdout'] ?? '';
break;
$stderr = $executorResponse->stderr ?? '';
$stdout = $executorResponse->stdout ?? '';

if ($statusCode >= 200 && $statusCode < 500) {
$res = $executorResponse->response ?? '';
if (is_array($res)) {
$res = json_encode($res, JSON_UNESCAPED_UNICODE);
} elseif (is_object($res)) {
$res = json_encode($res, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
}
}

$executionEnd = \microtime(true);
$executionTime = ($executionEnd - $executionStart);

$endTime = \microtime(true);
$duration = $endTime - $startTime;
$functionStatus = ($statusCode >= 500) ? 'failed' : 'completed';

$execution = [
Expand All @@ -783,12 +776,13 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
'response' => \mb_strcut($res, 0, 1000000), // Limit to 1MB
'stdout' => \mb_strcut($stdout, 0, 1000000), // Limit to 1MB
'stderr' => \mb_strcut($stderr, 0, 1000000), // Limit to 1MB
'duration' => $executionTime + $coldStartTime,
'duration' => $duration + $coldStartDuration,
'startTime' => $startTime,
];

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

// Finish request
Expand Down Expand Up @@ -1007,10 +1001,8 @@ function getStats(Table $statsHost, Table $statsContainers, Orchestration $orche
}
}

// TODO: @Meldiron Might cause stack overflow due to infinite recursion
if ($recursive) {
\sleep(1);
getStats($statsHost, $statsContainers, $orchestration, $recursive);
Timer::after(1000, fn () => getStats($statsHost, $statsContainers, $orchestration, $recursive));
}
}

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
}
},
"scripts": {
"lint": "./vendor/bin/pint --preset psr12 --test",
"format": "./vendor/bin/pint --preset psr12",
"lint": "./vendor/bin/pint --test --config pint.json",
"format": "./vendor/bin/pint --config pint.json",
"check": "./vendor/bin/phpstan analyse --level 8 -c phpstan.neon app src tests",
"test": "./vendor/bin/phpunit --configuration phpunit.xml --debug"
},
Expand Down
6 changes: 4 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
parameters:
scanDirectories:
- vendor/swoole/ide-helper
scanDirectories:
- vendor/swoole/ide-helper
excludePaths:
- tests/resources
6 changes: 6 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"preset": "psr12",
"exclude": [
"tests/resources"
]
}
2 changes: 1 addition & 1 deletion tests/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function call(string $method, string $path = '', array $headers = [], arr
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
$len = strlen($header);
$header = explode(':', $header, 2);
Expand Down
Loading

0 comments on commit 76c2724

Please sign in to comment.