From a82562c16e6fcca5ee5bfdaf2d374a32ddc79947 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 28 Jan 2022 03:36:30 +0400 Subject: [PATCH] feat: add deploymentId property to builds collection --- app/config/collections.php | 15 ++++++++-- app/controllers/api/functions.php | 46 +++++++++++++++---------------- app/executor.php | 1 + app/init.php | 1 + app/workers/builds.php | 1 + app/workers/deletes.php | 23 ++++++++++++++-- 6 files changed, 60 insertions(+), 27 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 7678b9c697..418fdfa3c9 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -2049,6 +2049,17 @@ $collections = [ '$id' => 'builds', 'name' => 'Builds', 'attributes' => [ + [ + '$id' => 'deploymentId', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => 'dateCreated', 'type' => Database::VAR_INTEGER, @@ -2173,9 +2184,9 @@ $collections = [ ], 'indexes' => [ [ - '$id' => '_key_status', + '$id' => '_key_deployment', 'type' => Database::INDEX_KEY, - 'attributes' => ['status'], + 'attributes' => ['deploymentId'], 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 47cae1cec3..aa341d62bf 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -431,34 +431,34 @@ App::delete('/v1/functions/:functionId') $function = $dbForProject->getDocument('functions', $functionId); - // Request executor to delete deployment containers - $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); - \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/functions/$functionId"); - \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - \curl_setopt($ch, CURLOPT_TIMEOUT, 900); - \curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); - \curl_setopt($ch, CURLOPT_HTTPHEADER, [ - 'Content-Type: application/json', - 'x-appwrite-project: '.$project->getId(), - 'x-appwrite-executor-key: '. App::getEnv('_APP_EXECUTOR_SECRET', '') - ]); + // // Request executor to delete deployment containers + // $ch = \curl_init(); + // \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); + // \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/functions/$functionId"); + // \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + // \curl_setopt($ch, CURLOPT_TIMEOUT, 900); + // \curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + // \curl_setopt($ch, CURLOPT_HTTPHEADER, [ + // 'Content-Type: application/json', + // 'x-appwrite-project: '.$project->getId(), + // 'x-appwrite-executor-key: '. App::getEnv('_APP_EXECUTOR_SECRET', '') + // ]); - $executorResponse = \curl_exec($ch); + // $executorResponse = \curl_exec($ch); - $error = \curl_error($ch); + // $error = \curl_error($ch); - if (!empty($error)) { - throw new Exception('Executor Cleanup Error: ' . $error, 500); - } + // if (!empty($error)) { + // throw new Exception('Executor Cleanup Error: ' . $error, 500); + // } - // Check status code - $statusCode = \curl_getinfo($ch, CURLINFO_HTTP_CODE); - if (200 !== $statusCode) { - throw new Exception('Executor error: ' . $executorResponse, $statusCode); - } + // // Check status code + // $statusCode = \curl_getinfo($ch, CURLINFO_HTTP_CODE); + // if (200 !== $statusCode) { + // throw new Exception('Executor error: ' . $executorResponse, $statusCode); + // } - \curl_close($ch); + // \curl_close($ch); if ($function->isEmpty()) { throw new Exception('Function not found', 404); diff --git a/app/executor.php b/app/executor.php index 3bd6eb5f4b..2d3cbd7ab3 100644 --- a/app/executor.php +++ b/app/executor.php @@ -413,6 +413,7 @@ function execute(string $trigger, string $projectId, string $executionId, string '$id' => $buildId, '$read' => ($userId !== '') ? ['user:' . $userId] : [], '$write' => [], + 'deploymentId' => $deployment->getId(), 'dateCreated' => time(), 'status' => 'processing', 'outputPath' => '', diff --git a/app/init.php b/app/init.php index fe330815e8..3c22aa3aef 100644 --- a/app/init.php +++ b/app/init.php @@ -100,6 +100,7 @@ const DELETE_TYPE_DOCUMENT = 'document'; const DELETE_TYPE_COLLECTIONS = 'collections'; const DELETE_TYPE_PROJECTS = 'projects'; const DELETE_TYPE_FUNCTIONS = 'functions'; +const DELETE_TYPE_DEPLOYMENTS = 'deployments'; const DELETE_TYPE_USERS = 'users'; const DELETE_TYPE_TEAMS= 'teams'; const DELETE_TYPE_EXECUTIONS = 'executions'; diff --git a/app/workers/builds.php b/app/workers/builds.php index ac20e0031a..0e529249d9 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -118,6 +118,7 @@ class BuildsV1 extends Worker '$id' => $buildId, '$read' => [], '$write' => [], + 'deploymentId' => $deploymentId, 'dateCreated' => time(), 'status' => 'processing', 'runtime' => $function->getAttribute('runtime'), diff --git a/app/workers/deletes.php b/app/workers/deletes.php index bc7721d550..4795b94412 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -50,6 +50,9 @@ class DeletesV1 extends Worker case DELETE_TYPE_FUNCTIONS: $this->deleteFunction($document, $projectId); break; + case DELETE_TYPE_DEPLOYMENTS: + $this->deleteDeployment($document, $projectId); + break; case DELETE_TYPE_USERS: $this->deleteUser($document, $projectId); break; @@ -313,18 +316,34 @@ class DeletesV1 extends Worker $this->deleteByGroup('deployments', [ new Query('functionId', Query::TYPE_EQUAL, [$document->getId()]) ], $dbForProject, function (Document $document) use ($device) { - - if ($device->delete($document->getAttribute('path', ''))) { + if ($device->delete($document->getAttribute('path', ''), true)) { Console::success('Delete code deployment: ' . $document->getAttribute('path', '')); } else { Console::error('Failed to delete code deployment: ' . $document->getAttribute('path', '')); } }); + // Delete builds + $this->deleteByGroup('builds', [ + new Query('functionId', Query::TYPE_EQUAL, [$document->getId()]) + ], $dbForProject, function (Document $document) use ($device) { + if ($device->delete($document->getAttribute('path', ''), true)) { + Console::success('Delete code deployment: ' . $document->getAttribute('path', '')); + } else { + Console::error('Failed to delete code deployment: ' . $document->getAttribute('path', '')); + } + }); + + // Delete build files + // Delete Executions $this->deleteByGroup('executions', [ new Query('functionId', Query::TYPE_EQUAL, [$document->getId()]) ], $dbForProject); + + + // Delete deployment files + }