From faef5af96cf8dc3cc1869252e119378fc870028e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 19 Jul 2022 11:00:43 +0000 Subject: [PATCH] Fix executor execution request --- app/executor.php | 33 +++++++++++++++++++++--- app/functionsProxy.php | 4 +++ src/Executor/Executor.php | 54 ++++++++++++--------------------------- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/app/executor.php b/app/executor.php index 6e521bf1ae..6b5b41903b 100644 --- a/app/executor.php +++ b/app/executor.php @@ -3,6 +3,7 @@ require_once __DIR__ . '/../vendor/autoload.php'; use Appwrite\Runtimes\Runtimes; +use Executor\Executor; use Swoole\ConnectionPool; use Swoole\Http\Request as SwooleRequest; use Swoole\Http\Response as SwooleResponse; @@ -451,18 +452,42 @@ App::delete('/v1/runtimes/:runtimeId') App::post('/v1/execution') ->desc('Create an execution') + // execution-related ->param('runtimeId', '', new Text(64), 'The runtimeID to execute.') - ->param('vars', [], new Assoc(), 'Environment variables required for the build.') + ->param('vars', [], new Assoc(), 'Environment variables required for the build.') // TODO: Possibly introduce build-vars and execution-vars in future ->param('data', '', new Text(8192), 'Data to be forwarded to the function, this is user specified.', true) ->param('timeout', 15, new Range(1, (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)), 'Function maximum execution time in seconds.') + // runtime-related + ->param('projectId', '', new Text(64), 'The project ID of function.') + ->param('deploymentId', '', new Text(64), 'The deployment ID of function.') + ->param('source', '', new Text(0), 'Path to source files.') + ->param('destination', '', new Text(0), 'Destination folder to store build files into.', true) + ->param('commands', [], new ArrayList(new Text(1024), 100), 'Commands required to build the container. Maximum of 100 commands are allowed, each 1024 characters long.') + ->param('runtime', '', new Text(128), 'Runtime for the cloud function.') + ->param('baseImage', '', new Text(128), 'Base image name of the runtime.') + ->param('entrypoint', '', new Text(256), 'Entrypoint of the code file.', true) ->inject('activeRuntimes') ->inject('response') ->action( - function (string $runtimeId, array $vars, string $data, $timeout, $activeRuntimes, Response $response) { + function (string $runtimeId, array $vars, string $data, $timeout, string $projectId, string $deploymentId, string $source, string $destination, array $commands, string $runtime, string $baseImage, string $entrypoint, $activeRuntimes, Response $response) { + // Prepare runtime if (!$activeRuntimes->exists($runtimeId)) { - throw new Exception('Runtime not found. Please create the runtime.', 404); + // TODO: Try multiple times? + $executor = new Executor('http://localhost:3000/v1'); + $response = $executor->createRuntime( + deploymentId: $deploymentId, + projectId: $projectId, + source: $source, + runtime: $runtime, + baseImage: $baseImage, + vars: $vars, + entrypoint: $entrypoint, + commands: [] + ); + \var_dump($response); } + // Ensure runtime started for ($i = 0; $i < 5; $i++) { if ($activeRuntimes->get($runtimeId)['status'] === 'pending') { Console::info('Waiting for runtime to be ready...'); @@ -524,6 +549,8 @@ App::post('/v1/execution') \curl_close($ch); + // TODO: Re-run in case of 406 + switch (true) { /** No Error. */ case $errNo === 0: diff --git a/app/functionsProxy.php b/app/functionsProxy.php index 541d5d47b0..c3ee8ce96a 100644 --- a/app/functionsProxy.php +++ b/app/functionsProxy.php @@ -95,6 +95,10 @@ function fetchExecutorsState(RedisPool $redisPool, bool $forceShowError = false) \curl_close($ch); + \var_dump($executorResponse); + \var_dump($statusCode); + \var_dump($error); + if ($statusCode === 200) { markOnline($cache, $id, $forceShowError); } else { diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 5fee583589..4b3a9e4fb4 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -156,10 +156,21 @@ class Executor 'x-appwrite-executor-key' => App::getEnv('_APP_FUNCTIONS_PROXY_SECRET', '') ]; $params = [ + // execution-related 'runtimeId' => "$projectId-$deploymentId", 'vars' => $vars, 'data' => $data, 'timeout' => $timeout, + + // runtime-related + 'deploymentId' => $deploymentId, + 'projectId' => $projectId, + 'source' => $path, + 'runtime' => $runtime, + 'baseImage' => $baseImage, + 'vars' => $vars, + 'entrypoint' => $entrypoint, + 'commands' => [] ]; /* Add 2 seconds as a buffer to the actual timeout value since there can be a slight variance*/ @@ -168,44 +179,11 @@ class Executor $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout); $status = $response['headers']['status-code']; - for ($attempts = 0; $attempts < 10; $attempts++) { - try { - switch (true) { - case $status < 400: - return $response['body']; - case $status === 404: - $response = $this->createRuntime( - deploymentId: $deploymentId, - projectId: $projectId, - source: $path, - runtime: $runtime, - baseImage: $baseImage, - vars: $vars, - entrypoint: $entrypoint, - commands: [] - ); - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout); - $status = $response['headers']['status-code']; - - if ($status < 400) { - return $response['body']; - } - break; - case $status === 406: - $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout); - $status = $response['headers']['status-code']; - - if ($status < 400) { - return $response['body']; - } - break; - default: - throw new \Exception($response['body']['message'], $status); - } - } catch (\Exception $e) { - throw new \Exception($e->getMessage(), $e->getCode()); - } - sleep(2); + switch (true) { + case $status < 400: + return $response['body']; + default: + throw new \Exception($response['body']['message'], $status); } throw new Exception($response['body']['message'], 503);