From 0e3a354f1f95da1e723d2132d5a616641444f1bc Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Sun, 25 Feb 2024 19:13:55 +0530 Subject: [PATCH] Create build document in API instead of worker --- app/config/collections.php | 11 +++++++++ app/controllers/api/functions.php | 23 ++++++++++++++++++ src/Appwrite/Platform/Workers/Builds.php | 31 ++++++------------------ 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 3bc58eff56..2d5aa5945a 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -2590,6 +2590,17 @@ $projectCollections = array_merge([ '$id' => ID::custom('builds'), 'name' => 'Builds', 'attributes' => [ + [ + '$id' => ID::custom('creationTime'), + 'type' => Database::VAR_DATETIME, + 'format' => '', + 'size' => 0, + 'signed' => false, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['datetime'], + ], [ '$id' => ID::custom('startTime'), 'type' => Database::VAR_DATETIME, diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 1fdfdeecc1..5b933f6aad 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1184,6 +1184,29 @@ App::post('/v1/functions/:functionId/deployments') $deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment->setAttribute('size', $fileSize)->setAttribute('metadata', $metadata)); } + $buildId = ID::unique(); + $build = $dbForProject->createDocument('builds', new Document([ + '$id' => $buildId, + '$permissions' => [], + 'creationTime' => DateTime::now(), + 'startTime' => null, + 'deploymentInternalId' => $deployment->getInternalId(), + 'deploymentId' => $deployment->getId(), + 'status' => 'waiting', + 'path' => '', + 'runtime' => $function->getAttribute('runtime'), + 'source' => $deployment->getAttribute('path', ''), + 'sourceType' => strtolower($deviceFunctions->getType()), + 'logs' => '', + 'endTime' => null, + 'duration' => 0, + 'size' => 0 + ])); + + $deployment->setAttribute('buildId', $build->getId()); + $deployment->setAttribute('buildInternalId', $build->getInternalId()); + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment); + // Start the build $queueForBuilds ->setType(BUILD_TYPE_DEPLOYMENT) diff --git a/src/Appwrite/Platform/Workers/Builds.php b/src/Appwrite/Platform/Workers/Builds.php index b2f57dac23..0bc7b53ab2 100644 --- a/src/Appwrite/Platform/Workers/Builds.php +++ b/src/Appwrite/Platform/Workers/Builds.php @@ -147,32 +147,15 @@ class Builds extends Action $isNewBuild = empty($buildId); $deviceFunctions = $getFunctionsDevice($project->getId()); - if ($isNewBuild) { - $buildId = ID::unique(); - $build = $dbForProject->createDocument('builds', new Document([ - '$id' => $buildId, - '$permissions' => [], - 'startTime' => $startTime, - 'deploymentInternalId' => $deployment->getInternalId(), - 'deploymentId' => $deployment->getId(), - 'status' => 'processing', - 'path' => '', - 'runtime' => $function->getAttribute('runtime'), - 'source' => $deployment->getAttribute('path', ''), - 'sourceType' => strtolower($deviceFunctions->getType()), - 'logs' => '', - 'endTime' => null, - 'duration' => 0, - 'size' => 0 - ])); - - $deployment->setAttribute('buildId', $build->getId()); - $deployment->setAttribute('buildInternalId', $build->getInternalId()); - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment); - } else { - $build = $dbForProject->getDocument('builds', $buildId); + $build = $dbForProject->getDocument('builds', $buildId); + if ($build->getAttribute('status') === 'cancelled') { + return; } + $build->setAttribute('status', 'processing'); + $build->setAttribute('startTime', $startTime); + $build = $dbForProject->updateDocument('builds', $buildId, $build); + $source = $deployment->getAttribute('path', ''); $installationId = $deployment->getAttribute('installationId', ''); $providerRepositoryId = $deployment->getAttribute('providerRepositoryId', '');