From ed3d598f2e6e7f75648130b673d893c4dcbf8110 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Sun, 18 Feb 2024 18:01:25 +0530 Subject: [PATCH] Added cancel build endpoint --- app/controllers/api/functions.php | 49 ++++++++++++++++++++++++ src/Appwrite/Platform/Workers/Builds.php | 5 +++ 2 files changed, 54 insertions(+) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index d3913d180d..75ed72c524 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1475,6 +1475,55 @@ App::post('/v1/functions/:functionId/deployments/:deploymentId/builds/:buildId') $response->noContent(); }); +App::patch('/v1/functions/:functionId/deployments/:deploymentId/builds/:buildId') + ->groups(['api', 'functions']) + ->desc('Cancel build') + ->label('scope', 'functions.write') + ->label('audits.event', 'deployment.update') + ->label('audits.resource', 'function/{request.functionId}') + ->label('sdk.auth', [APP_AUTH_TYPE_KEY]) + ->label('sdk.namespace', 'functions') + ->label('sdk.method', 'cancelBuild') + ->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT) + ->label('sdk.response.model', Response::MODEL_NONE) + ->param('functionId', '', new UID(), 'Function ID.') + ->param('deploymentId', '', new UID(), 'Deployment ID.') + ->param('buildId', '', new UID(), 'Build unique ID.') + ->inject('response') + ->inject('dbForProject') + ->inject('project') + ->inject('queueForEvents') + ->action(function (string $functionId, string $deploymentId, string $buildId, Response $response, Database $dbForProject, Document $project, Event $queueForEvents) { + $function = $dbForProject->getDocument('functions', $functionId); + + if ($function->isEmpty()) { + throw new Exception(Exception::FUNCTION_NOT_FOUND); + } + + $deployment = $dbForProject->getDocument('deployments', $deploymentId); + + if ($deployment->isEmpty()) { + throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); + } + + $build = Authorization::skip(fn () => $dbForProject->getDocument('builds', $buildId)); + + if ($build->isEmpty()) { + throw new Exception(Exception::BUILD_NOT_FOUND); + } + + $executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST')); + $deleteBuild = $executor->deleteRuntime($project->getId(), $deploymentId . "-build"); + + $build = $dbForProject->updateDocument('builds', $build->getId(), $build->setAttribute('status', 'cancelled')); + + $queueForEvents + ->setParam('functionId', $function->getId()) + ->setParam('deploymentId', $deployment->getId()); + + $response->noContent(); + }); + App::post('/v1/functions/:functionId/executions') ->groups(['api', 'functions']) ->desc('Create execution') diff --git a/src/Appwrite/Platform/Workers/Builds.php b/src/Appwrite/Platform/Workers/Builds.php index 120a514fc4..9dbcf15371 100644 --- a/src/Appwrite/Platform/Workers/Builds.php +++ b/src/Appwrite/Platform/Workers/Builds.php @@ -390,6 +390,9 @@ class Builds extends Action $response = null; $err = null; + if ($build->getAttribute('status') === 'cancelled') + return; + Co::join([ Co\go(function () use ($executor, &$response, $project, $deployment, $source, $function, $runtime, $vars, $command, &$err) { try { @@ -456,6 +459,8 @@ class Builds extends Action ]); if ($err) { + if ($build->getAttribute('status') === 'cancelled') + return; throw $err; }