From 06448bbdc50b217250a729264c0888b8b724ee4a Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 24 Feb 2022 12:58:10 +0000 Subject: [PATCH 1/4] Remove Synchronous execution model --- app/controllers/api/functions.php | 13 ++++- src/Appwrite/Utopia/Response.php | 1 - .../Utopia/Response/Model/SyncExecution.php | 53 ------------------- 3 files changed, 11 insertions(+), 56 deletions(-) delete mode 100644 src/Appwrite/Utopia/Response/Model/SyncExecution.php diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 075dfc6499..91c8aeda24 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -967,11 +967,20 @@ App::post('/v1/functions/:functionId/executions') } Authorization::skip(fn() => $dbForProject->updateDocument('executions', $executionId, $execution)); - $executionResponse['response'] = ($executionResponse['status'] !== 'completed') ? $executionResponse['stderr'] : $executionResponse['stdout']; + + $executionResponse['$id'] = $executionId; + $executionResponse['$read'] = $execution->getAttribute('$read', []); + $executionResponse['$write'] = $execution->getAttribute('$write', []); + $executionResponse['functionId'] = $function->getId(); + $executionResponse['dateCreated'] = $execution->getAttribute('dateCreated'); + $executionResponse['trigger'] = $execution->getAttribute('trigger'); + $executionResponse['status'] = $execution->getAttribute('status'); + $executionResponse['statusCode'] = $execution->getAttribute('statusCode'); + $executionResponse['time'] = $execution->getAttribute('time'); $response ->setStatusCode(Response::STATUS_CODE_CREATED) - ->dynamic(new Document($executionResponse), Response::MODEL_SYNC_EXECUTION); + ->dynamic(new Document($executionResponse), Response::MODEL_EXECUTION); }); App::get('/v1/functions/:functionId/executions') diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 32375af358..2cfe7f7ef6 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -156,7 +156,6 @@ class Response extends SwooleResponse const MODEL_DEPLOYMENT = 'deployment'; const MODEL_DEPLOYMENT_LIST = 'deploymentList'; const MODEL_EXECUTION = 'execution'; - const MODEL_SYNC_EXECUTION = 'syncExecution'; const MODEL_EXECUTION_LIST = 'executionList'; const MODEL_BUILD = 'build'; const MODEL_BUILD_LIST = 'buildList'; diff --git a/src/Appwrite/Utopia/Response/Model/SyncExecution.php b/src/Appwrite/Utopia/Response/Model/SyncExecution.php deleted file mode 100644 index 05220f78fd..0000000000 --- a/src/Appwrite/Utopia/Response/Model/SyncExecution.php +++ /dev/null @@ -1,53 +0,0 @@ -addRule('status', [ - 'type' => self::TYPE_STRING, - 'description' => 'Execution Status.', - 'default' => '', - 'example' => 'completed', - ]) - ->addRule('response', [ - 'type' => self::TYPE_STRING, - 'description' => 'Execution Response.', - 'default' => '', - 'example' => 'Hello World!', - ]) - ->addRule('time', [ - 'type' => self::TYPE_INTEGER, - 'description' => 'Execution Time.', - 'default' => 0, - 'example' => 100, - ]) - ; - } - - /** - * Get Name - * - * @return string - */ - public function getName():string - { - return 'Syncronous Execution'; - } - - /** - * Get Collection - * - * @return string - */ - public function getType():string - { - return Response::MODEL_SYNC_EXECUTION; - } -} \ No newline at end of file From b2217f695001f3cce2ab1ca6ca54c6590512d9b7 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 24 Feb 2022 13:22:20 +0000 Subject: [PATCH 2/4] Use execution directly --- app/controllers/api/functions.php | 12 +----------- src/Appwrite/Utopia/Response.php | 2 -- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 91c8aeda24..268f2b06d5 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -968,19 +968,9 @@ App::post('/v1/functions/:functionId/executions') Authorization::skip(fn() => $dbForProject->updateDocument('executions', $executionId, $execution)); - $executionResponse['$id'] = $executionId; - $executionResponse['$read'] = $execution->getAttribute('$read', []); - $executionResponse['$write'] = $execution->getAttribute('$write', []); - $executionResponse['functionId'] = $function->getId(); - $executionResponse['dateCreated'] = $execution->getAttribute('dateCreated'); - $executionResponse['trigger'] = $execution->getAttribute('trigger'); - $executionResponse['status'] = $execution->getAttribute('status'); - $executionResponse['statusCode'] = $execution->getAttribute('statusCode'); - $executionResponse['time'] = $execution->getAttribute('time'); - $response ->setStatusCode(Response::STATUS_CODE_CREATED) - ->dynamic(new Document($executionResponse), Response::MODEL_EXECUTION); + ->dynamic($execution, Response::MODEL_EXECUTION); }); App::get('/v1/functions/:functionId/executions') diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 2cfe7f7ef6..4d67302f38 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -31,7 +31,6 @@ use Appwrite\Utopia\Response\Model\Domain; use Appwrite\Utopia\Response\Model\Error; use Appwrite\Utopia\Response\Model\ErrorDev; use Appwrite\Utopia\Response\Model\Execution; -use Appwrite\Utopia\Response\Model\SyncExecution; use Appwrite\Utopia\Response\Model\Build; use Appwrite\Utopia\Response\Model\File; use Appwrite\Utopia\Response\Model\Bucket; @@ -267,7 +266,6 @@ class Response extends SwooleResponse ->setModel(new Runtime()) ->setModel(new Deployment()) ->setModel(new Execution()) - ->setModel(new SyncExecution()) ->setModel(new Build()) ->setModel(new Project()) ->setModel(new Webhook()) From d38a6907d2a6767810dd1fcba09ed57576db3a14 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 24 Feb 2022 18:31:30 +0400 Subject: [PATCH 3/4] feat: review comments --- app/executor.php | 6 +----- app/workers/builds.php | 1 - app/workers/deletes.php | 4 ++-- app/workers/functions.php | 1 - src/Executor/Executor.php | 9 +-------- 5 files changed, 4 insertions(+), 17 deletions(-) diff --git a/app/executor.php b/app/executor.php index fe93f37876..d9be75dca7 100644 --- a/app/executor.php +++ b/app/executor.php @@ -395,17 +395,13 @@ App::delete('/v1/runtimes/:runtimeId') App::post('/v1/execution') ->desc('Create an execution') ->param('runtimeId', '', new Text(64), 'The runtimeID to execute') - ->param('path', '', new Text(0), 'Path containing the built files.', false) ->param('vars', [], new Assoc(), 'Environment variables required for the build', false) ->param('data', '', new Text(8192), 'Data to be forwarded to the function, this is user specified.', true) - ->param('runtime', '', new Text(128), 'Runtime for the cloud function', false) - ->param('entrypoint', '', new Text(256), 'Entrypoint of the code file') ->param('timeout', 15, new ValidatorRange(1, 900), 'Function maximum execution time in seconds.', true) - ->param('baseImage', '', new Text(128), 'Base image name of the runtime', false) ->inject('activeRuntimes') ->inject('response') ->action( - function (string $runtimeId, string $path, array $vars, string $data, string $runtime, string $entrypoint, $timeout, string $baseImage, $activeRuntimes, Response $response) { + function (string $runtimeId, array $vars, string $data, $timeout, $activeRuntimes, Response $response) { if (!$activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime not found. Please create the runtime.', 404); diff --git a/app/workers/builds.php b/app/workers/builds.php index a213cfdec4..6e3b56880e 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -114,7 +114,6 @@ class BuildsV1 extends Worker try { $response = $this->executor->createRuntime( projectId: $projectId, - functionId: $functionId, deploymentId: $deploymentId, entrypoint: $deployment->getAttribute('entrypoint'), source: $source, diff --git a/app/workers/deletes.php b/app/workers/deletes.php index d403c1093a..de5ce5aebd 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -371,7 +371,7 @@ class DeletesV1 extends Worker $executor = new Executor(); foreach ($deploymentIds as $deploymentId) { try { - $executor->deleteRuntime($projectId, $functionId, $deploymentId); + $executor->deleteRuntime($projectId, $deploymentId); } catch (Throwable $th) { Console::error($th->getMessage()); } @@ -421,7 +421,7 @@ class DeletesV1 extends Worker Console::info("Requesting executor to delete deployment container for deployment " . $deploymentId); try { $executor = new Executor(); - $executor->deleteRuntime($projectId, $functionId, $deploymentId); + $executor->deleteRuntime($projectId, $deploymentId); } catch (Throwable $th) { Console::error($th->getMessage()); } diff --git a/app/workers/functions.php b/app/workers/functions.php index c98dae63f0..d98160f151 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -290,7 +290,6 @@ class FunctionsV1 extends Worker try { $executionResponse = $this->executor->createExecution( projectId: $projectId, - functionId: $functionId, deploymentId: $deploymentId, path: $build->getAttribute('outputPath', ''), vars: $vars, diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 4b3953a357..841e1ab64a 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -32,7 +32,6 @@ class Executor } public function createRuntime( - string $functionId, string $deploymentId, string $projectId, string $source, @@ -75,7 +74,7 @@ class Executor return $response['body']; } - public function deleteRuntime(string $projectId, string $functionId, string $deploymentId) + public function deleteRuntime(string $projectId, string $deploymentId) { $runtimeId = "$projectId-$deploymentId"; $route = "/runtimes/$runtimeId"; @@ -98,7 +97,6 @@ class Executor public function createExecution( string $projectId, - string $functionId, string $deploymentId, string $path, array $vars, @@ -116,13 +114,9 @@ class Executor ]; $params = [ 'runtimeId' => "$projectId-$deploymentId", - 'path' => $path, 'vars' => $vars, 'data' => $data, - 'runtime' => $runtime, - 'entrypoint' => $entrypoint, 'timeout' => $timeout, - 'baseImage' => $baseImage, ]; $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); @@ -133,7 +127,6 @@ class Executor switch ($status) { case 404: $response = $this->createRuntime( - functionId: $functionId, deploymentId: $deploymentId, projectId: $projectId, source: $path, From 8d0ff927a73ebea8dc021b2690aab4587118d2b4 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 24 Feb 2022 21:00:05 +0400 Subject: [PATCH 4/4] feat: fix tests --- app/controllers/api/functions.php | 1 - src/Executor/Executor.php | 2 -- .../Services/Functions/FunctionsCustomClientTest.php | 2 +- .../Services/Functions/FunctionsCustomServerTest.php | 12 ++++++------ 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 268f2b06d5..7a49c70fcc 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -942,7 +942,6 @@ App::post('/v1/functions/:functionId/executions') try { $executionResponse = $executor->createExecution( projectId: $project->getId(), - functionId: $function->getId(), deploymentId: $deployment->getId(), path: $build->getAttribute('outputPath', ''), vars: $vars, diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 659ad16170..2bdad22e8f 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -36,7 +36,6 @@ class Executor * * Launches a runtime container for a deployment ready for execution * - * @param string $functionId * @param string $deploymentId * @param string $projectId * @param string $source @@ -126,7 +125,6 @@ class Executor * Create an execution * * @param string $projectId - * @param string $functionId * @param string $deploymentId * @param string $path * @param array $vars diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index bedeee6ed6..d2443783c0 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -383,7 +383,7 @@ class FunctionsCustomClientTest extends Scope 'async' => false ]); - $output = json_decode($execution['body']['response'], true); + $output = json_decode($execution['body']['stdout'], true); $this->assertEquals(201, $execution['headers']['status-code']); $this->assertEquals('completed', $execution['body']['status']); $this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']); diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 9a2afde739..1654fd4af4 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -594,12 +594,12 @@ class FunctionsCustomServerTest extends Scope ]); $this->assertEquals('completed', $execution['body']['status']); - $this->assertStringContainsString($data['deploymentId'], $execution['body']['response']); - $this->assertStringContainsString('Test1', $execution['body']['response']); - $this->assertStringContainsString('http', $execution['body']['response']); - $this->assertStringContainsString('PHP', $execution['body']['response']); - $this->assertStringContainsString('8.0', $execution['body']['response']); - // $this->assertStringContainsString('êä', $execution['body']['response']); // tests unknown utf-8 chars + $this->assertStringContainsString($data['deploymentId'], $execution['body']['stdout']); + $this->assertStringContainsString('Test1', $execution['body']['stdout']); + $this->assertStringContainsString('http', $execution['body']['stdout']); + $this->assertStringContainsString('PHP', $execution['body']['stdout']); + $this->assertStringContainsString('8.0', $execution['body']['stdout']); + // $this->assertStringContainsString('êä', $execution['body']['sdtout']); // tests unknown utf-8 chars $this->assertLessThan(0.500, $execution['body']['time']); return $data;