From f5e1ce01b7d786328c0d0559900bfc64f1aa670f Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 27 Aug 2021 11:55:22 +0100 Subject: [PATCH] More Changes --- app/controllers/api/functions.php | 4 +- app/executor.php | 88 +++++++++++++++++++------------ app/workers/functions.php | 2 +- docker-compose.yml | 3 -- 4 files changed, 57 insertions(+), 40 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index f4c47f028..6f35d9016 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -355,7 +355,7 @@ App::patch('/v1/functions/:functionId/tag') /** @var Appwrite\Database\Document $project */ $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, "http://executor:8080/v1/tag"); + \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor:8080/v1/tag"); \curl_setopt($ch, CURLOPT_POST, true); \curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ 'functionId' => $functionId, @@ -775,7 +775,7 @@ App::post('/v1/functions/:functionId/executions') } // Directly execute function. $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, "http://executor:8080/v1/execute"); + \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor:8080/v1/execute"); \curl_setopt($ch, CURLOPT_POST, true); \curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ 'trigger' => 'http', diff --git a/app/executor.php b/app/executor.php index dcf19b0a9..98ed8460d 100644 --- a/app/executor.php +++ b/app/executor.php @@ -39,21 +39,21 @@ $runtimes = Config::getParam('runtimes'); Swoole\Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_CURL); // Warmup: make sure images are ready to run fast 🚀 -Co\run(function() use ($runtimes, $orchestration) { - foreach($runtimes as $runtime) { - go(function() use ($runtime, $orchestration) { - Console::info('Warming up '.$runtime['name'].' '.$runtime['version'].' environment...'); +// Co\run(function() use ($runtimes, $orchestration) { +// foreach($runtimes as $runtime) { +// go(function() use ($runtime, $orchestration) { +// Console::info('Warming up '.$runtime['name'].' '.$runtime['version'].' environment...'); - $response = $orchestration->pull($runtime['image']); +// $response = $orchestration->pull($runtime['image']); - if ($response) { - Console::success("Successfully Warmed up {$runtime['name']} {$runtime['version']}!"); - } else { - Console::error("Failed to Warmup {$runtime['name']} {$runtime['version']}!"); - } - }); - } -}); +// if ($response) { +// Console::success("Successfully Warmed up {$runtime['name']} {$runtime['version']}!"); +// } else { +// Console::error("Failed to Warmup {$runtime['name']} {$runtime['version']}!"); +// } +// }); +// } +// }); /** * List function servers @@ -409,40 +409,60 @@ function execute(string $trigger, string $projectId, string $executionId, string $exitCode = 0; + $errNo = -1; + $attempts = 0; + $max = 5; + + $executorResponse = ''; + // cURL request to runtime - $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, "http://".$container.":3000/"); - \curl_setopt($ch, CURLOPT_POST, true); - \curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ - 'path' => '/usr/code', - 'file' => 'index.js', - 'env' => $vars, - 'payload' => $data - ])); - \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - \curl_setopt($ch, CURLOPT_TIMEOUT, $function->getAttribute('timeout', (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900))); - \curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + do { + $attempts++; + $ch = \curl_init(); + \curl_setopt($ch, CURLOPT_URL, "http://".$container.":3000/"); + \curl_setopt($ch, CURLOPT_POST, true); + \curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ + 'path' => '/usr/code', + 'file' => 'index.js', + 'env' => $vars, + 'payload' => $data + ])); + \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + \curl_setopt($ch, CURLOPT_TIMEOUT, $function->getAttribute('timeout', (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900))); + \curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + + $executorResponse = \curl_exec($ch); - $executorResponse = \curl_exec($ch); + $error = \curl_error($ch); + + $errNo = \curl_errno($ch); - $error = \curl_error($ch); - if (!empty($error)) { + \curl_close($ch); + if ($errNo != CURLE_COULDNT_CONNECT) { + break; + } + + sleep(1); + } while ($attempts < $max); + + if ($attempts >= 5) { + $stderr = 'Failed to connect to executor runtime after 5 attempts.'; + $exitCode = 124; + } + + if ($errNo !== 0 && $errNo != CURLE_COULDNT_CONNECT) { throw new Exception('Curl error: ' . $error, 500); } - \curl_close($ch); - $executionData = json_decode($executorResponse, true); - if (\is_null($executionData)) { - throw new Exception('Failed to decode JSON response', 500); - } + if (isset($executionData['code'])) { $exitCode = $executionData['code']; } if ($exitCode === 500) { $stderr = $executionData['message']; - } else { + } else if ($exitCode === 0) { $stdout = $executorResponse; } diff --git a/app/workers/functions.php b/app/workers/functions.php index d7de086fd..b419265e3 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -297,7 +297,7 @@ class FunctionsV1 extends Worker public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function, string $event = '', string $eventData = '', string $data = '', array $webhooks = [], string $userId = '', string $jwt = ''): void { $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_URL, "http://executor:8080/v1/execute"); + \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor:8080/v1/execute"); \curl_setopt($ch, CURLOPT_POST, true); \curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ 'trigger' => $trigger, diff --git a/docker-compose.yml b/docker-compose.yml index ef450a1e0..31d21df1b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -319,7 +319,6 @@ services: - -e - app/executor.php - -dopcache.preload=opcache.preload=/usr/src/code/app/preload.php - container_name: appwrite-executor ports: - "8080:8080" build: @@ -330,8 +329,6 @@ services: - VERSION=dev networks: appwrite: - aliases: - - executor runtimes: volumes: - /var/run/docker.sock:/var/run/docker.sock