From 7fce57f35d50163db76a176368e7ea2385c57eeb Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 8 Jun 2022 16:15:17 +0100 Subject: [PATCH] Clean up unused code and get tests working --- app/executor.php | 36 +++++++++++++++++++++++++----------- src/Executor/Executor.php | 10 ++++------ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/executor.php b/app/executor.php index 1eb73df85c..8df9f7090f 100644 --- a/app/executor.php +++ b/app/executor.php @@ -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)) { diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 4ca1704705..ed00cd0b08 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -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);