From 09de5ed256dbaba7f0a4652ff759e38f1a5d6ca2 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 28 Jan 2022 05:29:20 +0400 Subject: [PATCH] feat: delete deployment deletion to the worker --- app/controllers/api/functions.php | 53 +++++---------------- app/workers/deletes.php | 76 +++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 41 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 7f666b628..43c925788 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -685,64 +685,30 @@ App::delete('/v1/functions/:functionId/deployments/:deploymentId') ->inject('response') ->inject('dbForProject') ->inject('usage') + ->inject('deletes') ->inject('project') - ->action(function ($functionId, $deploymentId, $response, $dbForProject, $usage, $project) { + ->action(function ($functionId, $deploymentId, $response, $dbForProject, $usage, $deletes, $project) { /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Database $dbForProject */ /** @var Appwrite\Event\Event $usage */ /** @var Utopia\Database\Document $project */ $function = $dbForProject->getDocument('functions', $functionId); - if ($function->isEmpty()) { throw new Exception('Function not found', 404); } $deployment = $dbForProject->getDocument('deployments', $deploymentId); + if ($deployment->isEmpty()) { + throw new Exception('Deployment not found', 404); + } if ($deployment->getAttribute('functionId') !== $function->getId()) { throw new Exception('Deployment not found', 404); } - if ($deployment->isEmpty()) { - throw new Exception('deployment not found', 404); - } - - // Request executor to delete deployment containers - $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); - \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/deployments/$deploymentId"); - \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); - - $error = \curl_error($ch); - - 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); - } - - \curl_close($ch); - - $device = Storage::getDevice('functions'); - - if ($device->delete($deployment->getAttribute('path', ''))) { - if (!$dbForProject->deleteDocument('deployments', $deployment->getId())) { - throw new Exception('Failed to remove deployment from DB', 500); - } + if (!$dbForProject->deleteDocument('deployments', $deployment->getId())) { + throw new Exception('Failed to remove deployment from DB', 500); } if($function->getAttribute('deployment') === $deployment->getId()) { // Reset function deployment @@ -755,6 +721,11 @@ App::delete('/v1/functions/:functionId/deployments/:deploymentId') ->setParam('storage', $deployment->getAttribute('size', 0) * -1) ; + $deletes + ->setParam('type', DELETE_TYPE_DOCUMENT) + ->setParam('document', $deployment) + ; + $response->noContent(); }); diff --git a/app/workers/deletes.php b/app/workers/deletes.php index 057926f41..12999d432 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -379,6 +379,82 @@ class DeletesV1 extends Worker } + /** + * @param Document $document deployment document + * @param string $projectId + */ + protected function deleteDeployment(Document $document, string $projectId): void + { + $dbForProject = $this->getProjectDB($projectId); + + /** + * Request executor to delete the deployment containers + */ + try { + $ch = \curl_init(); + \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); + // TODO: Implement coroutines. + \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/deployments/{$document->getId()}"); + \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + \curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'x-appwrite-project: '. $projectId, + 'x-appwrite-executor-key: '. App::getEnv('_APP_EXECUTOR_SECRET', '') + ]); + + $executorResponse = \curl_exec($ch); + $error = \curl_error($ch); + if (!empty($error)) { + throw new Exception($error, 500); + } + + $statusCode = \curl_getinfo($ch, CURLINFO_HTTP_CODE); + if ($statusCode >= 400) { + throw new Exception('Executor error: ' . $executorResponse, $statusCode); + } + + \curl_close($ch); + } catch (Throwable $th) { + Console::error($th->getMessage()); + } + + /** + * Delete Deployments + */ + $storageFunctions = new Local(APP_STORAGE_FUNCTIONS . '/app-' . $projectId); + $deploymentIds = []; + $this->deleteByGroup('deployments', [ + new Query('functionId', Query::TYPE_EQUAL, [$document->getId()]) + ], $dbForProject, function (Document $document) use ($storageFunctions, &$deploymentIds) { + $deploymentIds[] = $document->getId(); + if ($storageFunctions->delete($document->getAttribute('path', ''), true)) { + Console::success('Delete deployment files: ' . $document->getAttribute('path', '')); + } else { + Console::error('Failed to delete deployment files: ' . $document->getAttribute('path', '')); + } + }); + + /** + * Delete builds + */ + $storageBuilds = new Local(APP_STORAGE_BUILDS . '/app-' . $projectId); + $this->deleteByGroup('builds', [ + new Query('deploymentId', Query::TYPE_EQUAL, $deploymentIds) + ], $dbForProject, function (Document $document) use ($storageBuilds) { + if ($storageBuilds->delete($document->getAttribute('outputPath', ''), true)) { + Console::success('Deleted build files: ' . $document->getAttribute('outputPath', '')); + } else { + Console::error('Failed to delete build files: ' . $document->getAttribute('outputPath', '')); + } + }); + + // Delete Executions + $this->deleteByGroup('executions', [ + new Query('functionId', Query::TYPE_EQUAL, [$document->getId()]) + ], $dbForProject); + + } + /** * @param Document $document to be deleted