Clean up unused code and get tests working

This commit is contained in:
Bradley Schofield
2022-06-08 16:15:17 +01:00
parent d2c4bb9872
commit 7fce57f35d
2 changed files with 29 additions and 17 deletions
+25 -11
View File
@@ -178,9 +178,8 @@ App::post('/v1/runtimes')
->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $baseImage, string $entrypoint, bool $remove, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) {
if ($activeRuntimes->exists($runtimeId)) {
if ($activeRuntimes->get($runtimeId)['key'] == 'Pending') {
sleep(1);
throw new \Exception('A runtime with the same ID is already being created.', 500);
if ($activeRuntimes->get($runtimeId)['key'] == 'pending') {
throw new \Exception('A runtime with the same ID is already being created. Attempt a execution soon.', 500);
}
throw new Exception('Runtime already exists.', 409);
@@ -196,14 +195,16 @@ App::post('/v1/runtimes')
$secret = \bin2hex(\random_bytes(16));
$activeRuntimes->set($runtimeId, [
'id' => $containerId,
'name' => $runtimeId,
'created' => $startTime,
'updated' => $endTime,
'status' => 'Pending',
'key' => $secret,
]);
if (!$remove) {
$activeRuntimes->set($runtimeId, [
'id' => $containerId,
'name' => $runtimeId,
'created' => $startTime,
'updated' => $endTime,
'status' => 'pending',
'key' => $secret,
]);
}
try {
Console::info('Building container : ' . $runtimeId);
@@ -460,6 +461,19 @@ App::post('/v1/execution')
throw new Exception('Runtime not found. Please create the runtime.', 404);
}
for ($i = 0; $i < 5; $i++) {
if ($activeRuntimes->get($runtimeId)['status'] === 'pending') {
Console::info('Waiting for runtime to be ready...');
sleep(1);
} else {
break;
}
if ($i === 4) {
throw new Exception('Runtime failed to launch in allocated time.', 500);
}
}
$runtime = $activeRuntimes->get($runtimeId);
$secret = $runtime['key'];
if (empty($secret)) {
+4 -6
View File
@@ -161,11 +161,13 @@ class Executor
'timeout' => $timeout,
];
/* Add 2 seconds as a buffer to the actual timeout value since there can be a slight variance*/
$requestTimeout = $timeout + 2;
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout);
for ($attempts = 0; $attempts < 10; $attempts++) {
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout);
$status = $response['headers']['status-code'];
try {
@@ -183,14 +185,10 @@ class Executor
entrypoint: $entrypoint,
commands: []
);
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout);
$status = $response['headers']['status-code'];
// 500 usually means that the runtime is being created but is not ready, retry.
if ($status == 500) {
continue 2;
}
break;
case $status === 406:
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout);