diff --git a/app/executor.php b/app/executor.php index 0c4b5c8f04..9c237d6a07 100644 --- a/app/executor.php +++ b/app/executor.php @@ -830,68 +830,6 @@ App::post('/v1/functions/:functionId/executions') } ); -App::delete('/v1/functions/:functionId') - ->desc('Delete a function') - ->param('functionId', '', new UID()) - ->inject('projectId') - ->inject('response') - ->inject('dbForProject') - ->action( - function (string $functionId, string $projectId, Response $response, Database $dbForProject) use ($orchestrationPool) { - - $results = $dbForProject->find('deployments', [new Query('resourceId', Query::TYPE_EQUAL, [$functionId])], 999); - - // If amount is 0 then we simply return true - if (count($results) === 0) { - $response - ->setStatusCode(Response::STATUS_CODE_OK) - ->send(); - } - - Console::info('Deleting function: ' . $functionId); - // Delete the containers of all deployments - global $register; - foreach ($results as $deployment) { - go(function () use ($orchestrationPool, $deployment, $register, $projectId) { - $db = $register->get('dbPool')->get(); - $redis = $register->get('redisPool')->get(); - $cache = new Cache(new RedisCache($redis)); - $dbForProject = new Database(new MariaDB($db), $cache); - $dbForProject->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); - $dbForProject->setNamespace('_project_' . $projectId); - - try { - $orchestration = $orchestrationPool->get(); - // Remove the container of the deployment - $orchestration->remove('appwrite-function-' . $deployment['$id'], true); - Console::success('Removed container for deployment: ' . $deployment['$id']); - - $builds = $dbForProject->find('builds', [ - new Query('deploymentId', Query::TYPE_EQUAL, [$deployment['$id']]), - new Query('status', Query::TYPE_EQUAL, ['building']) - ], 999); - - // Remove all the build containers - foreach ($builds as $build) { - $orchestration->remove('build-stage-' . $build['$id'], true); - Console::success("Removed build contanier: $build for deployment: " . $deployment['$id']); - } - } catch (\Throwable $th) { - Console::error($th->getMessage()); - } finally { - $orchestrationPool->put($orchestration); - $register->get('dbPool')->put($db); - $register->get('redisPool')->put($redis); - } - }); - } - - $response - ->setStatusCode(Response::STATUS_CODE_OK) - ->send(); - } - ); - App::post('/v1/functions/:functionId/deployments/:deploymentId/runtime') ->desc('Create a new runtime server for a deployment') ->param('functionId', '', new UID(), 'Function unique ID.') diff --git a/app/workers/deletes.php b/app/workers/deletes.php index af244906e9..0341eae2cd 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -312,36 +312,6 @@ class DeletesV1 extends Worker { $dbForProject = $this->getProjectDB($projectId); - /** - * Request executor to delete all deployment containers - */ - try { - $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); - \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor/v1/functions/{$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 */ @@ -361,11 +331,13 @@ class DeletesV1 extends Worker /** * Delete builds */ - if (!empty($deploymentIds)) { - $storageBuilds = new Local(APP_STORAGE_BUILDS . '/app-' . $projectId); + $storageBuilds = new Local(APP_STORAGE_BUILDS . '/app-' . $projectId); + $buildIds = []; + foreach ($deploymentIds as $deploymentId) { $this->deleteByGroup('builds', [ - new Query('deploymentId', Query::TYPE_EQUAL, $deploymentIds) - ], $dbForProject, function (Document $document) use ($storageBuilds) { + new Query('deploymentId', Query::TYPE_EQUAL, [$deploymentId]) + ], $dbForProject, function (Document $document) use ($storageBuilds, $deploymentId, &$buildIds) { + $buildIds[$deploymentId][] = $document->getId(); if ($storageBuilds->delete($document->getAttribute('outputPath', ''), true)) { Console::success('Deleted build files: ' . $document->getAttribute('outputPath', '')); } else { @@ -379,6 +351,30 @@ class DeletesV1 extends Worker new Query('functionId', Query::TYPE_EQUAL, [$document->getId()]) ], $dbForProject); + /** + * Request executor to delete all deployment containers + */ + foreach ($deploymentIds as $deploymentId) { + try { + $route = "/deployments/$deploymentId"; + $headers = [ + 'content-Type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-executor-key' => App::getEnv('_APP_EXECUTOR_SECRET', '') + ]; + $params = [ + 'buildIds' => $buildIds[$deploymentId] ?? [], + ]; + $response = $this->call(self::METHOD_DELETE, $route, $headers, $params, true, 30); + $status = $response['headers']['status-code']; + if ($status >= 400) { + throw new \Exception('Error deleting deplyoment: ' . $document->getId() , $status); + } + } catch (Throwable $th) { + Console::error($th->getMessage()); + } + } + } /** @@ -389,16 +385,34 @@ class DeletesV1 extends Worker { $dbForProject = $this->getProjectDB($projectId); - $builds = $dbForProject->find('builds', [ - new Query('deploymentId', Query::TYPE_EQUAL, [$document->getId()]) - ], 999); - - $buildIds = []; - foreach ($builds as $build) { - $buildIds[] = $build['$id']; - } /** - * Request executor to delete the deployment containers + * Delete deployment files + */ + $storageFunctions = new Local(APP_STORAGE_FUNCTIONS . '/app-' . $projectId); + if ($storageFunctions->delete($document->getAttribute('path', ''), true)) { + Console::success('Deleted deployment files: ' . $document->getAttribute('path', '')); + } else { + Console::error('Failed to delete deployment files: ' . $document->getAttribute('path', '')); + } + + /** + * Delete builds + */ + $buildIds = []; + $storageBuilds = new Local(APP_STORAGE_BUILDS . '/app-' . $projectId); + $this->deleteByGroup('builds', [ + new Query('deploymentId', Query::TYPE_EQUAL, [$document->getId()]) + ], $dbForProject, function (Document $document) use ($storageBuilds) { + $buildIds[] = $document->getId(); + 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', '')); + } + }); + + /** + * Request executor to delete the deployment container */ try { $route = "/deployments/{$document->getId()}"; @@ -416,35 +430,9 @@ class DeletesV1 extends Worker if ($status >= 400) { throw new \Exception('Error deleting deplyoment: ' . $document->getId() , $status); } - } catch (Throwable $th) { Console::error($th->getMessage()); } - - /** - * Delete deployment files - */ - $storageFunctions = new Local(APP_STORAGE_FUNCTIONS . '/app-' . $projectId); - if ($storageFunctions->delete($document->getAttribute('path', ''), true)) { - Console::success('Deleted 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, [$document->getId()]) - ], $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', '')); - } - }); - }