diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index b0efac3829..2dfbd3a6fb 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -139,18 +139,6 @@ class Base extends Action 'activate' => $activate, ])); - $function = $function - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('functions', $function->getId(), new Document([ - 'latestDeploymentId' => $deployment->getId(), - 'latestDeploymentInternalId' => $deployment->getSequence(), - 'latestDeploymentCreatedAt' => $deployment->getCreatedAt(), - 'latestDeploymentStatus' => $deployment->getAttribute('status', ''), - ])); - $publisherForBuilds->enqueue(new BuildMessage( project: $project, resource: $function, @@ -253,18 +241,6 @@ class Base extends Action 'activate' => $activate, ])); - $site = $site - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('sites', $site->getId(), new Document([ - 'latestDeploymentId' => $deployment->getId(), - 'latestDeploymentInternalId' => $deployment->getSequence(), - 'latestDeploymentCreatedAt' => $deployment->getCreatedAt(), - 'latestDeploymentStatus' => $deployment->getAttribute('status', ''), - ])); - $sitesDomain = $platform['sitesDomain']; $domain = ID::unique() . "." . $sitesDomain; diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php index 25863d424c..7094e912c5 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php @@ -303,12 +303,6 @@ class Create extends Action 'type' => $type ])); - $function = $dbForProject->updateDocument('functions', $function->getId(), new Document([ - 'latestDeploymentId' => $deployment->getId(), - 'latestDeploymentInternalId' => $deployment->getSequence(), - 'latestDeploymentCreatedAt' => $deployment->getCreatedAt(), - 'latestDeploymentStatus' => $deployment->getAttribute('status', ''), - ])); } else { $deployment = $dbForProject->updateDocument('deployments', $deploymentId, new Document([ 'sourceSize' => $fileSize, @@ -350,12 +344,6 @@ class Create extends Action 'type' => $type ])); - $function = $dbForProject->updateDocument('functions', $function->getId(), new Document([ - 'latestDeploymentId' => $deployment->getId(), - 'latestDeploymentInternalId' => $deployment->getSequence(), - 'latestDeploymentCreatedAt' => $deployment->getCreatedAt(), - 'latestDeploymentStatus' => $deployment->getAttribute('status', ''), - ])); } else { $deployment = $dbForProject->updateDocument('deployments', $deploymentId, new Document([ 'sourceChunksUploaded' => $chunksUploaded, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Delete.php index be4437ffe3..6862b0d776 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Delete.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Delete.php @@ -13,6 +13,7 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Exception\Transaction as TransactionException; use Utopia\Database\Query; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; @@ -89,8 +90,16 @@ class Delete extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if (!$dbForProject->deleteDocument('deployments', $deployment->getId())) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove deployment from DB'); + try { + if (!$dbForProject->deleteDocument('deployments', $deployment->getId())) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove deployment from DB'); + } + } catch (TransactionException) { + $deploymentExists = !$dbForProject->getDocument('deployments', $deployment->getId())->isEmpty(); + + if ($deploymentExists && !$dbForProject->deleteDocument('deployments', $deployment->getId())) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove deployment from DB'); + } } if (!empty($deployment->getAttribute('sourcePath', ''))) { diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php index 76070c8bf5..93f34ebb05 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php @@ -120,18 +120,6 @@ class Create extends Action 'buildLogs' => '', ])); - $function = $function - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('functions', $function->getId(), new Document([ - 'latestDeploymentId' => $function->getAttribute('latestDeploymentId'), - 'latestDeploymentInternalId' => $function->getAttribute('latestDeploymentInternalId'), - 'latestDeploymentCreatedAt' => $function->getAttribute('latestDeploymentCreatedAt'), - 'latestDeploymentStatus' => $function->getAttribute('latestDeploymentStatus'), - ])); - $publisherForBuilds->enqueue(new BuildMessage( project: $project, resource: $function, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php index dab477ef1f..85d93c14c1 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php @@ -12,6 +12,7 @@ use Executor\Executor; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; +use Utopia\Database\Exception\Transaction as TransactionException; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -91,17 +92,30 @@ class Update extends Action $endTime = new \DateTime('now'); $duration = $endTime->getTimestamp() - $startTime->getTimestamp(); - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ - 'buildEndedAt' => DateTime::now(), - 'buildDuration' => $duration, - 'status' => 'canceled' - ])); - - if ($deployment->getSequence() === $function->getAttribute('latestDeploymentInternalId', '')) { - $function = $function->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('functions', $function->getId(), new Document([ - 'latestDeploymentStatus' => $function->getAttribute('latestDeploymentStatus'), + try { + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ + 'buildEndedAt' => DateTime::now(), + 'buildDuration' => $duration, + 'status' => 'canceled' ])); + } catch (TransactionException) { + $deployment = $dbForProject->getDocument('deployments', $deployment->getId()); + + if ($deployment->isEmpty()) { + throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); + } + + if (\in_array($deployment->getAttribute('status'), ['ready', 'failed'])) { + throw new Exception(Exception::BUILD_ALREADY_COMPLETED); + } + + if ($deployment->getAttribute('status') !== 'canceled') { + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ + 'buildEndedAt' => DateTime::now(), + 'buildDuration' => $duration, + 'status' => 'canceled' + ])); + } } try { diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php index 87d1282d8b..136549e9c9 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php @@ -174,19 +174,6 @@ class Create extends Base 'activate' => $activate, ])); - $function = $function - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('functions', $function->getId(), new Document([ - 'latestDeploymentId' => $function->getAttribute('latestDeploymentId'), - 'latestDeploymentInternalId' => $function->getAttribute('latestDeploymentInternalId'), - 'latestDeploymentCreatedAt' => $function->getAttribute('latestDeploymentCreatedAt'), - 'latestDeploymentStatus' => $function->getAttribute('latestDeploymentStatus'), - ])); - - $this->updateEmptyManualRule($project, $function, $deployment, $dbForPlatform, $authorization); $publisherForBuilds->enqueue(new BuildMessage( diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index eaafe561df..6c09de8a41 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -344,12 +344,6 @@ class Create extends Base referenceType: 'branch' ); - $function = $dbForProject->updateDocument('functions', $function->getId(), new Document([ - 'latestDeploymentId' => $deployment->getId(), - 'latestDeploymentInternalId' => $deployment->getSequence(), - 'latestDeploymentCreatedAt' => $deployment->getCreatedAt(), - 'latestDeploymentStatus' => $deployment->getAttribute('status', ''), - ])); } elseif (!$template->isEmpty()) { // Deploy non-VCS from template $deploymentId = ID::unique(); @@ -370,13 +364,6 @@ class Create extends Base 'activate' => true, ])); - $function = $dbForProject->updateDocument('functions', $function->getId(), new Document([ - 'latestDeploymentId' => $deployment->getId(), - 'latestDeploymentInternalId' => $deployment->getSequence(), - 'latestDeploymentCreatedAt' => $deployment->getCreatedAt(), - 'latestDeploymentStatus' => $deployment->getAttribute('status', ''), - ])); - $publisherForBuilds->enqueue(new BuildMessage( project: $project, resource: $function, diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 07a7b1f89c..77ba3c89b9 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -30,6 +30,7 @@ use Utopia\Database\Exception\Conflict; use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Restricted; use Utopia\Database\Exception\Structure; +use Utopia\Database\Exception\Transaction as TransactionException; use Utopia\Database\Query; use Utopia\Detector\Detection\Rendering\SSR; use Utopia\Detector\Detection\Rendering\XStatic; @@ -247,6 +248,7 @@ class Builds extends Action ->setParam('deploymentId', $deployment->getId()); if ($deployment->getAttribute('status') === 'canceled') { + $resource = $this->updateLatestDeployment($dbForProject, $resource); $this->cancelDeployment($deployment->getId(), $dbForProject, $queueForRealtime); return; @@ -261,9 +263,7 @@ class Builds extends Action 'status' => 'processing', ])); - if ($deployment->getSequence() === $resource->getAttribute('latestDeploymentInternalId', '')) { - $resource = $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document(['latestDeploymentStatus' => $deployment->getAttribute('status', '')])); - } + $resource = $this->updateLatestDeployment($dbForProject, $resource); Span::add('deployment.status', 'processing'); @@ -537,9 +537,7 @@ class Builds extends Action ])); Span::add('deployment.status', 'building'); - if ($deployment->getSequence() === $resource->getAttribute('latestDeploymentInternalId', '')) { - $resource = $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document(['latestDeploymentStatus' => $deployment->getAttribute('status', '')])); - } + $resource = $this->updateLatestDeployment($dbForProject, $resource); $queueForRealtime ->setPayload($deployment->getArrayCopy()) @@ -898,16 +896,21 @@ class Builds extends Action $deployment->setAttribute('buildLogs', $logs); /** Update the status */ + $endTime = DateTime::now(); + $durationEnd = \microtime(true); + $deployment->setAttribute('buildEndedAt', $endTime); + $deployment->setAttribute('buildDuration', \intval(\ceil($durationEnd - $durationStart))); $deployment->setAttribute('status', 'ready'); $deployment = $dbForProject->updateDocument('deployments', $deploymentId, new Document([ + 'buildEndedAt' => $deployment->getAttribute('buildEndedAt'), + 'buildDuration' => $deployment->getAttribute('buildDuration'), 'buildLogs' => $deployment->getAttribute('buildLogs'), 'status' => 'ready', ])); Span::add('deployment.status', 'ready'); + Span::add('build.duration', $deployment->getAttribute('buildDuration')); - if ($deployment->getSequence() === $resource->getAttribute('latestDeploymentInternalId', '')) { - $resource = $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document(['latestDeploymentStatus' => $deployment->getAttribute('status', '')])); - } + $resource = $this->updateLatestDeployment($dbForProject, $resource); if ($isVcsEnabled) { $this->runGitAction('ready', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); @@ -994,6 +997,8 @@ class Builds extends Action Span::add('build.activated', true); } + $resource = $this->updateLatestDeployment($dbForProject, $resource); + $this->afterDeploymentSuccess( $project, $deployment, @@ -1063,13 +1068,6 @@ class Builds extends Action } } - $endTime = DateTime::now(); - $durationEnd = \microtime(true); - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ - 'buildEndedAt' => $endTime, - 'buildDuration' => \intval(\ceil($durationEnd - $durationStart)), - ])); - Span::add('build.duration', $deployment->getAttribute('buildDuration')); $queueForRealtime ->setPayload($deployment->getArrayCopy()) ->trigger(); @@ -1150,9 +1148,7 @@ class Builds extends Action 'buildLogs' => $message, ])); - if ($deployment->getSequence() === $resource->getAttribute('latestDeploymentInternalId', '')) { - $resource = $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document(['latestDeploymentStatus' => $deployment->getAttribute('status', '')])); - } + $resource = $this->updateLatestDeployment($dbForProject, $resource); $queueForRealtime ->setPayload($deployment->getArrayCopy()) @@ -1496,20 +1492,61 @@ class Builds extends Action } } + private function updateLatestDeployment(Database $dbForProject, Document $resource): Document + { + $latestDeployment = $dbForProject->findOne('deployments', [ + Query::equal('resourceType', [$resource->getCollection()]), + Query::equal('resourceInternalId', [$resource->getSequence()]), + Query::orderDesc('$createdAt'), + ]); + + $updates = $latestDeployment->isEmpty() + ? [ + 'latestDeploymentCreatedAt' => '', + 'latestDeploymentInternalId' => '', + 'latestDeploymentId' => '', + 'latestDeploymentStatus' => '', + ] + : [ + 'latestDeploymentCreatedAt' => $latestDeployment->getCreatedAt(), + 'latestDeploymentInternalId' => $latestDeployment->getSequence(), + 'latestDeploymentId' => $latestDeployment->getId(), + 'latestDeploymentStatus' => $latestDeployment->getAttribute('status', ''), + ]; + + return $dbForProject->updateDocument( + $resource->getCollection(), + $resource->getId(), + new Document($updates) + ); + } + private function cancelDeployment(string $deploymentId, Database $dbForProject, Realtime $queueForRealtime) { Span::add('deployment.status', 'canceled'); - $deployment = $dbForProject->getDocument('deployments', $deploymentId); + $attempts = 0; - $logs = $deployment->getAttribute('buildLogs', ''); - $date = \date('H:i:s'); - $logs .= "\033[90m[$date] \033[90m[\033[0mappwrite\033[90m]\033[33m Build has been canceled. \033[0m\n"; + while (true) { + try { + $deployment = $dbForProject->getDocument('deployments', $deploymentId); - $deployment->setAttribute('buildLogs', $logs); - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ - 'buildLogs' => $deployment->getAttribute('buildLogs'), - ])); + $logs = $deployment->getAttribute('buildLogs', ''); + $date = \date('H:i:s'); + $logs .= "\033[90m[$date] \033[90m[\033[0mappwrite\033[90m]\033[33m Build has been canceled. \033[0m\n"; + + $deployment->setAttribute('buildLogs', $logs); + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ + 'buildLogs' => $deployment->getAttribute('buildLogs'), + ])); + + break; + } catch (TransactionException $exception) { + if (++$attempts >= 5) { + throw $exception; + } + } + } $queueForRealtime ->setPayload($deployment->getArrayCopy()) diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php index 5fd724346c..53137ff000 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php @@ -311,18 +311,6 @@ class Create extends Action 'type' => $type, ])); - $site = $site - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('sites', $site->getId(), new Document([ - 'latestDeploymentId' => $deployment->getId(), - 'latestDeploymentInternalId' => $deployment->getSequence(), - 'latestDeploymentCreatedAt' => $deployment->getCreatedAt(), - 'latestDeploymentStatus' => $deployment->getAttribute('status', ''), - ])); - $sitesDomain = $platform['sitesDomain']; $domain = ID::unique() . "." . $sitesDomain; @@ -393,18 +381,6 @@ class Create extends Action 'type' => $type, ])); - $site = $site - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('sites', $site->getId(), new Document([ - 'latestDeploymentId' => $site->getAttribute('latestDeploymentId'), - 'latestDeploymentInternalId' => $site->getAttribute('latestDeploymentInternalId'), - 'latestDeploymentCreatedAt' => $site->getAttribute('latestDeploymentCreatedAt'), - 'latestDeploymentStatus' => $site->getAttribute('latestDeploymentStatus'), - ])); - $sitesDomain = $platform['sitesDomain']; $domain = ID::unique() . "." . $sitesDomain; $ruleId = md5($domain); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Delete.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Delete.php index b50e9b54f4..a1ccf6cad5 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Delete.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Delete.php @@ -13,6 +13,7 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Exception\Transaction as TransactionException; use Utopia\Database\Query; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; @@ -89,8 +90,16 @@ class Delete extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if (!$dbForProject->deleteDocument('deployments', $deployment->getId())) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove deployment from DB'); + try { + if (!$dbForProject->deleteDocument('deployments', $deployment->getId())) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove deployment from DB'); + } + } catch (TransactionException) { + $deploymentExists = !$dbForProject->getDocument('deployments', $deployment->getId())->isEmpty(); + + if ($deploymentExists && !$dbForProject->deleteDocument('deployments', $deployment->getId())) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove deployment from DB'); + } } if (!empty($deployment->getAttribute('sourcePath', ''))) { diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php index b3619c6017..261f1df648 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php @@ -138,18 +138,6 @@ class Create extends Action 'type' => $request->getHeader('x-sdk-language') === 'cli' ? 'cli' : 'manual' ])); - $site = $site - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('sites', $site->getId(), new Document([ - 'latestDeploymentId' => $site->getAttribute('latestDeploymentId'), - 'latestDeploymentInternalId' => $site->getAttribute('latestDeploymentInternalId'), - 'latestDeploymentCreatedAt' => $site->getAttribute('latestDeploymentCreatedAt'), - 'latestDeploymentStatus' => $site->getAttribute('latestDeploymentStatus'), - ])); - // Preview deployments for sites $sitesDomain = $platform['sitesDomain']; $domain = ID::unique() . "." . $sitesDomain; diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php index 7ea5cc87cb..a28a7465e6 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php @@ -12,6 +12,7 @@ use Executor\Executor; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; +use Utopia\Database\Exception\Transaction as TransactionException; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -89,17 +90,30 @@ class Update extends Action $endTime = new \DateTime('now'); $duration = $endTime->getTimestamp() - $startTime->getTimestamp(); - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ - 'buildEndedAt' => DateTime::now(), - 'buildDuration' => $duration, - 'status' => 'canceled' - ])); - - if ($deployment->getSequence() === $site->getAttribute('latestDeploymentInternalId', '')) { - $site = $site->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('sites', $site->getId(), new Document([ - 'latestDeploymentStatus' => $site->getAttribute('latestDeploymentStatus'), + try { + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ + 'buildEndedAt' => DateTime::now(), + 'buildDuration' => $duration, + 'status' => 'canceled' ])); + } catch (TransactionException) { + $deployment = $dbForProject->getDocument('deployments', $deployment->getId()); + + if ($deployment->isEmpty()) { + throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); + } + + if (\in_array($deployment->getAttribute('status'), ['ready', 'failed'])) { + throw new Exception(Exception::BUILD_ALREADY_COMPLETED); + } + + if ($deployment->getAttribute('status') !== 'canceled') { + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ + 'buildEndedAt' => DateTime::now(), + 'buildDuration' => $duration, + 'status' => 'canceled' + ])); + } } try { diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index b0d39ad6f9..2d6b441616 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -184,18 +184,6 @@ class Create extends Base 'activate' => $activate, ])); - $site = $site - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $dbForProject->updateDocument('sites', $site->getId(), new Document([ - 'latestDeploymentId' => $site->getAttribute('latestDeploymentId'), - 'latestDeploymentInternalId' => $site->getAttribute('latestDeploymentInternalId'), - 'latestDeploymentCreatedAt' => $site->getAttribute('latestDeploymentCreatedAt'), - 'latestDeploymentStatus' => $site->getAttribute('latestDeploymentStatus'), - ])); - $sitesDomain = $platform['sitesDomain']; $domain = ID::unique() . "." . $sitesDomain; diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index cde39e9292..d172fdc688 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -392,18 +392,6 @@ trait Deployment 'activate' => $activate, ]))); - $resource = $resource - ->setAttribute('latestDeploymentId', $deployment->getId()) - ->setAttribute('latestDeploymentInternalId', $deployment->getSequence()) - ->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt()) - ->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', '')); - $authorization->skip(fn () => $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document([ - 'latestDeploymentId' => $resource->getAttribute('latestDeploymentId'), - 'latestDeploymentInternalId' => $resource->getAttribute('latestDeploymentInternalId'), - 'latestDeploymentCreatedAt' => $resource->getAttribute('latestDeploymentCreatedAt'), - 'latestDeploymentStatus' => $resource->getAttribute('latestDeploymentStatus'), - ]))); - if ($resource->getCollection() === 'sites') { $projectId = $project->getId(); diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 1df10da690..e837fd1baf 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -2540,12 +2540,14 @@ final class SitesCustomServerTest extends Scope $this->assertNotEmpty($response['cookies']['a_jwt_console']); $this->assertEquals($jwt['body']['jwt'], $response['cookies']['a_jwt_console']); - $response = $proxyClient->call(Client::METHOD_GET, '/contact', headers: [ - 'cookie' => 'a_jwt_console=' . $jwt['body']['jwt'] - ], followRedirects: false); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertStringContainsString("Contact page", (string) $response['body']); - $this->assertStringContainsString("Preview by", (string) $response['body']); + $this->assertEventually(function () use ($proxyClient, $jwt) { + $response = $proxyClient->call(Client::METHOD_GET, '/contact', headers: [ + 'cookie' => 'a_jwt_console=' . $jwt['body']['jwt'] + ], followRedirects: false); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertStringContainsString("Contact page", (string) $response['body']); + $this->assertStringContainsString("Preview by", (string) $response['body']); + }); // Failure: Session missing (old bad, new ok) $session = $this->client->call(Client::METHOD_DELETE, '/account/sessions/current', array_merge([ @@ -2573,12 +2575,14 @@ final class SitesCustomServerTest extends Scope $this->assertEquals(201, $jwt['headers']['status-code']); $this->assertNotEmpty($jwt['body']['jwt']); - $response = $proxyClient->call(Client::METHOD_GET, '/contact', headers: [ - 'cookie' => 'a_jwt_console=' . $jwt['body']['jwt'] - ], followRedirects: false); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertStringContainsString("Contact page", (string) $response['body']); - $this->assertStringContainsString("Preview by", (string) $response['body']); + $this->assertEventually(function () use ($proxyClient, $jwt) { + $response = $proxyClient->call(Client::METHOD_GET, '/contact', headers: [ + 'cookie' => 'a_jwt_console=' . $jwt['body']['jwt'] + ], followRedirects: false); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertStringContainsString("Contact page", (string) $response['body']); + $this->assertStringContainsString("Preview by", (string) $response['body']); + }); $user = $this->client->call(Client::METHOD_PATCH, '/account/status', array_merge([ 'origin' => 'http://localhost',