Merge branch 'main' into realtime-tail-logs

This commit is contained in:
ArnabChatterjee20k authored and GitHub committed 2026-06-24 10:20:20 +05:30
commit 75aebac841
35 files changed
+229 -56

No files matched your search

+1 -1
View File
@@ -1306,7 +1306,7 @@ return [
'size' => 128,
'signed' => false,
'required' => false,
'default' => APP_COMPUTE_SPECIFICATION_DEFAULT,
'default' => APP_SITES_BUILD_SPECIFICATION_DEFAULT,
'filters' => [],
],
[
+1
View File
@@ -98,6 +98,7 @@ const APP_SOCIAL_YOUTUBE = 'https://www.youtube.com/c/appwrite?sub_confirmation=
const APP_COMPUTE_CPUS_DEFAULT = 0.5;
const APP_COMPUTE_MEMORY_DEFAULT = 512;
const APP_COMPUTE_SPECIFICATION_DEFAULT = Specification::S_1VCPU_512MB;
const APP_SITES_BUILD_SPECIFICATION_DEFAULT = Specification::S_2VCPU_2GB;
const APP_COMPUTE_DEPLOYMENT_MAX_RETENTION = 100 * 365; // 100 years
const APP_SDK_PLATFORM_SERVER = 'server';
const APP_SDK_PLATFORM_CLIENT = 'client';
+14 -5
View File
@@ -33,29 +33,38 @@ class Base extends Action
* @param array $plan The billing plan configuration
* @return string The appropriate default specification
*/
protected function getDefaultSpecification(array $plan): string
protected function getDefaultSpecification(array $plan, string $planKey = 'runtimeSpecifications', string $fallback = APP_COMPUTE_SPECIFICATION_DEFAULT, bool $preferFallback = false): string
{
$specifications = Config::getParam('specifications', []);
if (empty($specifications)) {
return APP_COMPUTE_SPECIFICATION_DEFAULT;
return $fallback;
}
$specificationValidator = new SpecificationValidator(
$plan,
$specifications,
System::getEnv('_APP_COMPUTE_CPUS', 0),
System::getEnv('_APP_COMPUTE_MEMORY', 0)
System::getEnv('_APP_COMPUTE_MEMORY', 0),
$planKey
);
$allowedSpecifications = $specificationValidator->getAllowedSpecifications();
if (empty($allowedSpecifications)) {
return $fallback;
}
if ($preferFallback && !array_key_exists($planKey, $plan) && \in_array($fallback, $allowedSpecifications)) {
return $fallback;
}
// If there is no plan use the highest specification
if (empty($plan)) {
return end($allowedSpecifications) ?? APP_COMPUTE_SPECIFICATION_DEFAULT;
return end($allowedSpecifications);
}
// Otherwise, use the lowest specification available in the plan
return $allowedSpecifications[0] ?? APP_COMPUTE_SPECIFICATION_DEFAULT;
return $allowedSpecifications[0];
}
public function redeployVcsFunction(Request $request, Document $function, Document $project, Document $installation, Database $dbForProject, BuildPublisher $publisherForBuilds, Document $template, GitHub $github, bool $activate, array $platform = [], string $referenceType = 'branch', string $reference = ''): Document
@@ -14,12 +14,15 @@ class Specification extends Validator
private int $maxMemory;
public function __construct(array $plan, array $specifications, float $maxCpus, int $maxMemory)
private string $planKey;
public function __construct(array $plan, array $specifications, float $maxCpus, int $maxMemory, string $planKey = 'runtimeSpecifications')
{
$this->plan = $plan;
$this->specifications = $specifications;
$this->maxCpus = $maxCpus;
$this->maxMemory = $maxMemory;
$this->planKey = $planKey;
}
/**
@@ -35,8 +38,8 @@ class Specification extends Validator
foreach ($this->specifications as $size => $values) {
if ((empty($this->maxCpus) || $values['cpus'] <= $this->maxCpus) && (empty($this->maxMemory) || $values['memory'] <= $this->maxMemory)) {
if (!empty($this->plan) && array_key_exists('runtimeSpecifications', $this->plan)) {
if (!\in_array($size, $this->plan['runtimeSpecifications'])) {
if (!empty($this->plan) && array_key_exists($this->planKey, $this->plan)) {
if (!\in_array($size, $this->plan[$this->planKey])) {
continue;
}
}
@@ -98,11 +98,12 @@ class Create extends Base
->param('providerRootDirectory', '', new Text(128, 0), 'Path to function code in the linked repo.', true)
->param('providerBranches', [], new ArrayList(new Text(128), APP_LIMIT_ARRAY_PARAMS_SIZE), 'List of branch name patterns to trigger automatic deployments. Supports wildcards. Leave empty to deploy on all branches.', true)
->param('providerPaths', [], new ArrayList(new Text(128), APP_LIMIT_ARRAY_PARAMS_SIZE), 'List of file path patterns to trigger automatic deployments. Supports wildcards. Leave empty to deploy on all file changes.', true)
->param('buildSpecification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
->param('buildSpecification', fn (array $plan) => $this->getDefaultSpecification($plan, 'buildSpecifications'), fn (array $plan) => new Specification(
$plan,
Config::getParam('specifications', []),
System::getEnv('_APP_COMPUTE_CPUS', 0),
System::getEnv('_APP_COMPUTE_MEMORY', 0)
System::getEnv('_APP_COMPUTE_MEMORY', 0),
'buildSpecifications'
), 'Build specification for the function deployments.', true, ['plan'])
->param('runtimeSpecification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
$plan,
@@ -90,12 +90,13 @@ class Update extends Base
->param('providerRootDirectory', '', new Text(128, 0), 'Path to function code in the linked repo.', true)
->param('providerBranches', null, new Nullable(new ArrayList(new Text(128), APP_LIMIT_ARRAY_PARAMS_SIZE)), 'List of branch name patterns to trigger automatic deployments. Supports wildcards. Leave empty to deploy on all branches.', true)
->param('providerPaths', null, new Nullable(new ArrayList(new Text(128), APP_LIMIT_ARRAY_PARAMS_SIZE)), 'List of file path patterns to trigger automatic deployments. Supports wildcards. Leave empty to deploy on all file changes.', true)
->param('buildSpecification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
->param('buildSpecification', null, fn (array $plan) => new Nullable(new Specification(
$plan,
Config::getParam('specifications', []),
System::getEnv('_APP_COMPUTE_CPUS', 0),
System::getEnv('_APP_COMPUTE_MEMORY', 0)
), 'Build specification for the function deployments.', true, ['plan'])
System::getEnv('_APP_COMPUTE_MEMORY', 0),
'buildSpecifications'
)), 'Build specification for the function deployments.', true, ['plan'])
->param('runtimeSpecification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
$plan,
Config::getParam('specifications', []),
@@ -137,7 +138,7 @@ class Update extends Base
string $providerRootDirectory,
?array $providerBranches,
?array $providerPaths,
string $buildSpecification,
?string $buildSpecification,
string $runtimeSpecification,
int $deploymentRetention,
Request $request,
@@ -173,6 +174,8 @@ class Update extends Base
$runtime = $function->getAttribute('runtime');
}
$buildSpecification ??= $function->getAttribute('buildSpecification', APP_COMPUTE_SPECIFICATION_DEFAULT);
$repositoryId = $function->getAttribute('repositoryId', '');
$repositoryInternalId = $function->getAttribute('repositoryInternalId', '');
@@ -12,6 +12,7 @@ use Utopia\Database\Document;
use Utopia\Platform\Action;
use Utopia\Platform\Scope\HTTP;
use Utopia\System\System;
use Utopia\Validator\WhiteList;
class XList extends Base
{
@@ -46,21 +47,23 @@ class XList extends Base
)
]
))
->param('type', 'runtimes', new WhiteList(['runtimes', 'builds']), 'Specification type to list. Can be one of: runtimes, builds.', true)
->inject('response')
->inject('plan')
->callback($this->action(...));
}
public function action(Response $response, array $plan)
public function action(string $type, Response $response, array $plan)
{
$allSpecs = Config::getParam('specifications', []);
$planKey = $type === 'builds' ? 'buildSpecifications' : 'runtimeSpecifications';
$specs = [];
foreach ($allSpecs as $spec) {
$spec['enabled'] = true;
if (array_key_exists('runtimeSpecifications', $plan)) {
$spec['enabled'] = in_array($spec['slug'], $plan['runtimeSpecifications']);
if (array_key_exists($planKey, $plan)) {
$spec['enabled'] = in_array($spec['slug'], $plan[$planKey]);
}
$maxCpus = System::getEnv('_APP_COMPUTE_CPUS', 0);
@@ -0,0 +1,23 @@
<?php
namespace Appwrite\Platform\Modules\Functions\Workers;
use Appwrite\Extend\Exception;
/**
* A build failure whose message is safe to surface in user-visible build logs.
*
* Every build error the worker raises intentionally is a BuildException, so the
* failure handler can show its message to users while masking any other
* throwable (e.g. database or executor failures) behind a generic message.
*/
class BuildException extends Exception
{
public function __construct(
?string $message = null,
string $type = Exception::BUILD_FAILED,
?\Throwable $previous = null,
) {
parent::__construct($type, $message, previous: $previous);
}
}
@@ -195,6 +195,7 @@ class Builds extends Action
$startTime = DateTime::now();
$durationStart = \microtime(true);
$phaseStart = $durationStart;
$resourceKey = match ($resource->getCollection()) {
'functions' => 'functionId',
@@ -215,7 +216,7 @@ class Builds extends Action
}
if ($isResourceBlocked($project, $resource->getCollection() === 'functions' ? RESOURCE_TYPE_FUNCTIONS : RESOURCE_TYPE_SITES, $resource->getId())) {
throw new \Exception('Resource is blocked');
throw new BuildException('Resource is blocked');
}
$log->addTag('deploymentId', $deployment->getId());
@@ -226,7 +227,7 @@ class Builds extends Action
}
if ($resource->getCollection() === 'functions' && empty($deployment->getAttribute('entrypoint', ''))) {
throw new \Exception('Entrypoint for your Appwrite Function is missing. Please specify it when making deployment or update the entrypoint under your function\'s "Settings" > "Configuration" > "Entrypoint".');
throw new BuildException('Entrypoint for your Appwrite Function is missing. Please specify it when making deployment or update the entrypoint under your function\'s "Settings" > "Configuration" > "Entrypoint".');
}
$version = $this->getVersion($resource);
@@ -297,6 +298,9 @@ class Builds extends Action
$github->initializeVariables($providerInstallationId, $privateKey, $githubAppId);
}
Span::add('timings.setup', \round(\microtime(true) - $phaseStart, 3));
$phaseStart = \microtime(true);
try {
if (! $isVcsEnabled) {
// Non-VCS + Template
@@ -406,7 +410,7 @@ class Builds extends Action
$exit = Console::execute($gitCloneCommand, '', $stdout, $stderr);
if ($exit !== 0) {
throw new \Exception('Unable to clone code repository: ' . $stderr);
throw new BuildException('Unable to clone code repository: ' . $stderr);
}
// Local refactoring for function folder with spaces
@@ -501,7 +505,7 @@ class Builds extends Action
}
if ($directorySize > $sizeLimit && $sizeLimit !== 0) {
throw new \Exception('Repository directory size should be less than ' . number_format($sizeLimit / (1000 * 1000), 2) . ' MBs.');
throw new BuildException('Repository directory size should be less than ' . number_format($sizeLimit / (1000 * 1000), 2) . ' MBs.');
}
Console::execute('find ' . \escapeshellarg($tmpDirectory) . ' -type d -name ".git" -exec rm -rf {} +', '', $stdout, $stderr);
@@ -539,6 +543,9 @@ class Builds extends Action
$this->runGitAction('processing', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform);
}
Span::add('timings.source', \round(\microtime(true) - $phaseStart, 3));
$phaseStart = \microtime(true);
/** Request the executor to build the code... */
$updated = $dbForProject->updateDocuments('deployments', new Document([
'status' => 'building',
@@ -739,7 +746,7 @@ class Builds extends Action
$span?->set('build.runtime.timed_out', true);
$span?->set('build.runtime.error_type', $error::class);
$span?->set('build.runtime.error_message', $error->getMessage());
$err = new AppwriteException(AppwriteException::BUILD_TIMEOUT, previous: $error);
$err = new BuildException(type: AppwriteException::BUILD_TIMEOUT, previous: $error);
} catch (\Throwable $error) {
$span?->set('build.runtime.error_type', $error::class);
$span?->set('build.runtime.error_message', $error->getMessage());
@@ -849,6 +856,9 @@ class Builds extends Action
}),
]);
Span::add('timings.build', \round(\microtime(true) - $phaseStart, 3));
$phaseStart = \microtime(true);
$latestDeployment = $dbForProject->getDocument('deployments', $deploymentId);
if ($latestDeployment->getAttribute('status') === 'canceled') {
$this->finalizeCanceledDeployment($deployment->getId(), $dbForProject, $queueForRealtime);
@@ -865,7 +875,7 @@ class Builds extends Action
$buildSizeLimit = $plan['buildSize'] * 1000 * 1000;
}
if ($response['size'] > $buildSizeLimit && $buildSizeLimit !== 0) {
throw new \Exception('Build size should be less than ' . number_format($buildSizeLimit / (1000 * 1000), 2) . ' MBs.');
throw new BuildException('Build size should be less than ' . number_format($buildSizeLimit / (1000 * 1000), 2) . ' MBs.');
}
$deployment->setAttribute('buildPath', $response['path']);
@@ -896,7 +906,7 @@ class Builds extends Action
Span::add('build.adapter', $deployment->getAttribute('adapter'));
Span::add('build.fallback_file', $deployment->getAttribute('fallbackFile'));
} elseif ($adapter === 'ssr' && $detection->getName() === 'static') {
throw new \Exception('Adapter mismatch. Detected: ' . $detection->getName() . ' does not match with the set adapter: ' . $adapter);
throw new BuildException('Adapter mismatch. Detected: ' . $detection->getName() . ' does not match with the set adapter: ' . $adapter);
}
}
@@ -1127,6 +1137,8 @@ class Builds extends Action
Span::add('build.screenshot_queued', true);
}
Span::add('timings.finalize', \round(\microtime(true) - $phaseStart, 3));
} catch (\Throwable $th) {
if ($dbForProject->getDocument('deployments', $deploymentId)->getAttribute('status') === 'canceled') {
$this->finalizeCanceledDeployment($deployment->getId(), $dbForProject, $queueForRealtime);
@@ -1134,14 +1146,19 @@ class Builds extends Action
return;
}
Span::add('build.error.stage', 'deployment');
Span::add('build.error.type', $th::class);
Span::add('build.error.message', $th->getMessage());
Span::add('build.error.file', $th->getFile());
Span::add('build.error.line', $th->getLine());
$isUserFacing = $th instanceof BuildException;
$message = $isUserFacing
? $th->getMessage()
: 'An internal error occurred while building. Please try again, and contact support if the problem persists.';
// Record user-facing failures on the span here, since they're not
// re-raised to the harness (which records internal errors via setError).
if ($isUserFacing) {
Span::add('build.exception.type', $th->getType());
Span::add('build.exception.message', $th->getMessage());
}
// Color message red
$message = $th->getMessage();
if (! \str_contains($message, '')) {
$message = '' . $message;
}
@@ -1181,6 +1198,11 @@ class Builds extends Action
if ($isVcsEnabled) {
$this->runGitAction('failed', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform, true);
}
// Let the worker harness record internal errors via the span and logger.
if (! $isUserFacing) {
throw $th;
}
} finally {
$queueForRealtime
->setPayload($deployment->getArrayCopy())
@@ -1277,7 +1299,7 @@ class Builds extends Action
default => null
};
if (\is_null($runtime)) {
throw new \Exception('Runtime "' . $resource->getAttribute('runtime', '') . '" is not supported');
throw new BuildException('Runtime "' . $resource->getAttribute('runtime', '') . '" is not supported');
}
return $runtime;
@@ -82,6 +82,7 @@ class Update extends Action
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'auths' => $auths,
])));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents->setParam('methodId', $methodId);
@@ -425,7 +425,10 @@ abstract class Base extends Action
'oAuthProviders' => $oAuthProviders
]);
return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
return $project;
}
public function action(
@@ -103,6 +103,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -75,6 +75,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -83,6 +83,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -76,6 +76,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -103,6 +103,7 @@ class Update extends Action
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'auths' => $auths,
])));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -75,6 +75,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -75,6 +75,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -75,6 +75,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -81,6 +81,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -81,6 +81,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents
->setParam('projectId', $project->getId())
@@ -79,6 +79,7 @@ class Update extends Action
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'apis' => $protocols,
])));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents->setParam('protocolId', $protocolId);
@@ -170,6 +170,7 @@ class Update extends Action
]);
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$response->dynamic($project, Response::MODEL_PROJECT);
}
@@ -79,6 +79,7 @@ class Update extends Action
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'services' => $services,
])));
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
$queueForEvents->setParam('serviceId', $serviceId);
@@ -82,11 +82,12 @@ class Create extends Base
->param('providerRootDirectory', '', new Text(128, 0), 'Path to site code in the linked repo.', true)
->param('providerBranches', [], new ArrayList(new Text(128), APP_LIMIT_ARRAY_PARAMS_SIZE), 'List of branch name patterns to trigger automatic deployments. Supports wildcards. Leave empty to deploy on all branches.', true)
->param('providerPaths', [], new ArrayList(new Text(128), APP_LIMIT_ARRAY_PARAMS_SIZE), 'List of file path patterns to trigger automatic deployments. Supports wildcards. Leave empty to deploy on all file changes.', true)
->param('buildSpecification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
->param('buildSpecification', fn (array $plan) => $this->getDefaultSpecification($plan, 'buildSpecifications', APP_SITES_BUILD_SPECIFICATION_DEFAULT, true), fn (array $plan) => new Specification(
$plan,
Config::getParam('specifications', []),
System::getEnv('_APP_COMPUTE_CPUS', 0),
System::getEnv('_APP_COMPUTE_MEMORY', 0)
System::getEnv('_APP_COMPUTE_MEMORY', 0),
'buildSpecifications'
), 'Build specification for the site deployments.', true, ['plan'])
->param('runtimeSpecification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
$plan,
@@ -86,12 +86,13 @@ class Update extends Base
->param('providerRootDirectory', '', new Text(128, 0), 'Path to site code in the linked repo.', true)
->param('providerBranches', null, new Nullable(new ArrayList(new Text(128), APP_LIMIT_ARRAY_PARAMS_SIZE)), 'List of branch name patterns to trigger automatic deployments. Supports wildcards. Leave empty to deploy on all branches.', true)
->param('providerPaths', null, new Nullable(new ArrayList(new Text(128), APP_LIMIT_ARRAY_PARAMS_SIZE)), 'List of file path patterns to trigger automatic deployments. Supports wildcards. Leave empty to deploy on all file changes.', true)
->param('buildSpecification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
->param('buildSpecification', null, fn (array $plan) => new Nullable(new Specification(
$plan,
Config::getParam('specifications', []),
System::getEnv('_APP_COMPUTE_CPUS', 0),
System::getEnv('_APP_COMPUTE_MEMORY', 0)
), 'Build specification for the site deployments.', true, ['plan'])
System::getEnv('_APP_COMPUTE_MEMORY', 0),
'buildSpecifications'
)), 'Build specification for the site deployments.', true, ['plan'])
->param('runtimeSpecification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
$plan,
Config::getParam('specifications', []),
@@ -133,7 +134,7 @@ class Update extends Base
string $providerRootDirectory,
?array $providerBranches,
?array $providerPaths,
string $buildSpecification,
?string $buildSpecification,
string $runtimeSpecification,
int $deploymentRetention,
Request $request,
@@ -177,6 +178,8 @@ class Update extends Base
$framework = $site->getAttribute('framework');
}
$buildSpecification ??= $site->getAttribute('buildSpecification', APP_SITES_BUILD_SPECIFICATION_DEFAULT);
$repositoryId = $site->getAttribute('repositoryId', '');
$repositoryInternalId = $site->getAttribute('repositoryInternalId', '');
@@ -12,6 +12,7 @@ use Utopia\Database\Document;
use Utopia\Platform\Action;
use Utopia\Platform\Scope\HTTP;
use Utopia\System\System;
use Utopia\Validator\WhiteList;
class XList extends Base
{
@@ -46,21 +47,23 @@ class XList extends Base
)
]
))
->param('type', 'runtimes', new WhiteList(['runtimes', 'builds']), 'Specification type to list. Can be one of: runtimes, builds.', true)
->inject('response')
->inject('plan')
->callback($this->action(...));
}
public function action(Response $response, array $plan)
public function action(string $type, Response $response, array $plan)
{
$allSpecs = Config::getParam('specifications', []);
$planKey = $type === 'builds' ? 'buildSpecifications' : 'runtimeSpecifications';
$specs = [];
foreach ($allSpecs as $spec) {
$spec['enabled'] = true;
if (array_key_exists('runtimeSpecifications', $plan)) {
$spec['enabled'] = in_array($spec['slug'], $plan['runtimeSpecifications']);
if (array_key_exists($planKey, $plan)) {
$spec['enabled'] = in_array($spec['slug'], $plan[$planKey]);
}
$maxCpus = System::getEnv('_APP_COMPUTE_CPUS', 0);
@@ -145,7 +145,11 @@ class Delete extends Action
}
if (!$deleted) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove file from DB');
$exists = $authorization->skip(fn () => $dbForProject->getDocument('bucket_' . $bucket->getSequence(), $fileId));
if (!$exists->isEmpty()) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove file from DB');
}
throw new Exception(Exception::STORAGE_FILE_NOT_FOUND);
}
} else {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to delete file from device');
@@ -237,6 +237,8 @@ trait Deployment
$comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, $commentPreviewUrl);
$latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment()));
} catch (\Throwable $e) {
Console::warning("Failed to update PR comment '{$latestCommentId}': " . $e->getMessage());
} finally {
$authorization->skip(fn () => $dbForPlatform->deleteDocument('vcsCommentLocks', $latestCommentId));
}
@@ -308,6 +310,8 @@ trait Deployment
$comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, '');
$latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment()));
} catch (\Throwable $e) {
Console::warning("Failed to update PR comment '{$latestCommentId}': " . $e->getMessage());
} finally {
$authorization->skip(fn () => $dbForPlatform->deleteDocument('vcsCommentLocks', $latestCommentId));
}
@@ -526,6 +530,8 @@ trait Deployment
$comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, $previewUrl);
$github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment());
}
} catch (\Throwable $e) {
Console::warning("Failed to update PR comment '{$latestCommentId}': " . $e->getMessage());
} finally {
$authorization->skip(fn () => $dbForPlatform->deleteDocument('vcsCommentLocks', $latestCommentId));
}
@@ -8,6 +8,7 @@ use Appwrite\Platform\Action;
use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Utopia\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
@@ -207,7 +208,12 @@ class Create extends Action
$github->initializeVariables($providerInstallationId, $privateKey, $githubAppId);
$commitDetails = $github->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash);
try {
$commitDetails = $github->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash);
} catch (\Throwable $e) {
Console::warning("Failed to fetch commit '{$providerCommitHash}': " . $e->getMessage());
$commitDetails = [];
}
$providerCommitAuthor = $commitDetails["commitAuthor"] ?? '';
$providerCommitMessage = $commitDetails["commitMessage"] ?? '';
@@ -494,11 +494,11 @@ trait FunctionsBase
return $deployment;
}
protected function listSpecifications(): mixed
protected function listSpecifications(array $params = []): mixed
{
$specifications = $this->client->call(Client::METHOD_GET, '/functions/specifications', array_merge([
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
], $this->getHeaders()), $params);
return $specifications;
}
@@ -200,7 +200,7 @@ final class FunctionsCustomServerTest extends Scope
public function testListSpecs(): void
{
$specifications = $this->listSpecifications();
$specifications = $this->listSpecifications(['type' => 'runtimes']);
$this->assertEquals(200, $specifications['headers']['status-code']);
$this->assertGreaterThanOrEqual(2, $specifications['body']['total']);
$this->assertArrayHasKey(0, $specifications['body']['specifications']);
@@ -214,20 +214,25 @@ final class FunctionsCustomServerTest extends Scope
$this->assertArrayHasKey('enabled', $specifications['body']['specifications'][1]);
$this->assertArrayHasKey('slug', $specifications['body']['specifications'][1]);
$buildSpecifications = $this->listSpecifications(['type' => 'builds']);
$this->assertEquals(200, $buildSpecifications['headers']['status-code']);
$this->assertEquals($specifications['body']['total'], $buildSpecifications['body']['total']);
$buildSpecification = $buildSpecifications['body']['specifications'][0]['slug'];
$function = $this->createFunction([
'functionId' => ID::unique(),
'name' => 'Specs function',
'runtime' => 'node-22',
'buildSpecification' => $specifications['body']['specifications'][0]['slug'],
'buildSpecification' => $buildSpecification,
'runtimeSpecification' => $specifications['body']['specifications'][1]['slug'],
]);
$this->assertEquals(201, $function['headers']['status-code']);
$this->assertEquals($specifications['body']['specifications'][0]['slug'], $function['body']['buildSpecification']);
$this->assertEquals($buildSpecification, $function['body']['buildSpecification']);
$this->assertEquals($specifications['body']['specifications'][1]['slug'], $function['body']['runtimeSpecification']);
$function = $this->getFunction($function['body']['$id']);
$this->assertEquals(200, $function['headers']['status-code']);
$this->assertEquals($specifications['body']['specifications'][0]['slug'], $function['body']['buildSpecification']);
$this->assertEquals($buildSpecification, $function['body']['buildSpecification']);
$this->assertEquals($specifications['body']['specifications'][1]['slug'], $function['body']['runtimeSpecification']);
$this->cleanupFunction($function['body']['$id']);
+2 -2
View File
@@ -489,11 +489,11 @@ trait SitesBase
return $deployment;
}
protected function listSpecifications(): mixed
protected function listSpecifications(array $params = []): mixed
{
$specifications = $this->client->call(Client::METHOD_GET, '/sites/specifications', array_merge([
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
], $this->getHeaders()), $params);
return $specifications;
}
@@ -26,7 +26,7 @@ final class SitesCustomServerTest extends Scope
public function testListSpecs(): void
{
$specifications = $this->listSpecifications();
$specifications = $this->listSpecifications(['type' => 'runtimes']);
$this->assertEquals(200, $specifications['headers']['status-code']);
$this->assertGreaterThanOrEqual(2, $specifications['body']['total']);
$this->assertArrayHasKey(0, $specifications['body']['specifications']);
@@ -40,21 +40,26 @@ final class SitesCustomServerTest extends Scope
$this->assertArrayHasKey('enabled', $specifications['body']['specifications'][1]);
$this->assertArrayHasKey('slug', $specifications['body']['specifications'][1]);
$buildSpecifications = $this->listSpecifications(['type' => 'builds']);
$this->assertEquals(200, $buildSpecifications['headers']['status-code']);
$this->assertEquals($specifications['body']['total'], $buildSpecifications['body']['total']);
$buildSpecification = $buildSpecifications['body']['specifications'][0]['slug'];
$site = $this->createSite([
'buildRuntime' => 'node-22',
'framework' => 'other',
'name' => 'Specs site',
'siteId' => ID::unique(),
'buildSpecification' => $specifications['body']['specifications'][0]['slug'],
'buildSpecification' => $buildSpecification,
'runtimeSpecification' => $specifications['body']['specifications'][1]['slug'],
]);
$this->assertEquals(201, $site['headers']['status-code']);
$this->assertEquals($specifications['body']['specifications'][0]['slug'], $site['body']['buildSpecification']);
$this->assertEquals($buildSpecification, $site['body']['buildSpecification']);
$this->assertEquals($specifications['body']['specifications'][1]['slug'], $site['body']['runtimeSpecification']);
$site = $this->getSite($site['body']['$id']);
$this->assertEquals(200, $site['headers']['status-code']);
$this->assertEquals($specifications['body']['specifications'][0]['slug'], $site['body']['buildSpecification']);
$this->assertEquals($buildSpecification, $site['body']['buildSpecification']);
$this->assertEquals($specifications['body']['specifications'][1]['slug'], $site['body']['runtimeSpecification']);
$this->cleanupSite($site['body']['$id']);
@@ -80,11 +85,15 @@ final class SitesCustomServerTest extends Scope
public function testCreateSite(): void
{
$buildSpecifications = $this->listSpecifications(['type' => 'builds']);
$buildSpecification = $buildSpecifications['body']['specifications'][0]['slug'];
/**
* Test for SUCCESS
*/
$site = $this->createSite([
'buildRuntime' => 'node-22',
'buildSpecification' => $buildSpecification,
'fallbackFile' => '',
'framework' => 'other',
'name' => 'Test Site',
@@ -102,6 +111,7 @@ final class SitesCustomServerTest extends Scope
$this->assertEquals(true, $dateValidator->isValid($site['body']['$createdAt']));
$this->assertEquals(true, $dateValidator->isValid($site['body']['$updatedAt']));
$this->assertEquals('node-22', $site['body']['buildRuntime']);
$this->assertEquals($buildSpecification, $site['body']['buildSpecification']);
$this->assertEquals(null, $site['body']['fallbackFile']);
$this->assertEquals('./', $site['body']['outputDirectory']);
@@ -716,11 +726,14 @@ final class SitesCustomServerTest extends Scope
public function testListSites(): void
{
$buildSpecifications = $this->listSpecifications(['type' => 'builds']);
/**
* Test for SUCCESS
*/
$siteId = $this->setupSite([
'buildRuntime' => 'node-22',
'buildSpecification' => $buildSpecifications['body']['specifications'][0]['slug'],
'fallbackFile' => '',
'framework' => 'analog',
'name' => 'Test List Sites',
@@ -862,8 +875,11 @@ final class SitesCustomServerTest extends Scope
public function testGetSite(): void
{
$buildSpecifications = $this->listSpecifications(['type' => 'builds']);
$siteId = $this->setupSite([
'buildRuntime' => 'node-22',
'buildSpecification' => $buildSpecifications['body']['specifications'][0]['slug'],
'fallbackFile' => '',
'framework' => 'other',
'name' => 'Test Site',
@@ -1650,8 +1666,12 @@ final class SitesCustomServerTest extends Scope
public function testUpdateSpecs(): void
{
$buildSpecifications = $this->listSpecifications(['type' => 'builds']);
$buildSpecification = $buildSpecifications['body']['specifications'][0]['slug'];
$siteId = $this->setupSite([
'buildRuntime' => 'node-22',
'buildSpecification' => $buildSpecification,
'fallbackFile' => '',
'framework' => 'other',
'name' => 'Test Site',
@@ -1667,6 +1687,7 @@ final class SitesCustomServerTest extends Scope
// Change the function specs
$site = $this->updateSite([
'buildRuntime' => 'node-22',
'buildSpecification' => $buildSpecification,
'fallbackFile' => '',
'framework' => 'other',
'name' => 'Test Site',
@@ -1679,11 +1700,30 @@ final class SitesCustomServerTest extends Scope
$this->assertEquals(200, $site['headers']['status-code']);
$this->assertNotEmpty($site['body']['$id']);
$this->assertEquals($buildSpecification, $site['body']['buildSpecification']);
$this->assertEquals(Specification::S_1VCPU_1GB, $site['body']['runtimeSpecification']);
$site = $this->updateSite([
'buildRuntime' => 'node-22',
'fallbackFile' => '',
'framework' => 'other',
'name' => 'Test Site',
'outputDirectory' => './',
'providerBranch' => 'main',
'providerRootDirectory' => './',
'$id' => $siteId,
'runtimeSpecification' => Specification::S_1VCPU_1GB,
]);
$this->assertEquals(200, $site['headers']['status-code']);
$this->assertNotEmpty($site['body']['$id']);
$this->assertEquals($buildSpecification, $site['body']['buildSpecification']);
$this->assertEquals(Specification::S_1VCPU_1GB, $site['body']['runtimeSpecification']);
// Change the specs to 1vcpu 512mb
$site = $this->updateSite([
'buildRuntime' => 'node-22',
'buildSpecification' => $buildSpecification,
'fallbackFile' => '',
'framework' => 'other',
'name' => 'Test Site',
@@ -1696,6 +1736,7 @@ final class SitesCustomServerTest extends Scope
$this->assertEquals(200, $site['headers']['status-code']);
$this->assertNotEmpty($site['body']['$id']);
$this->assertEquals($buildSpecification, $site['body']['buildSpecification']);
$this->assertEquals(Specification::S_1VCPU_512MB, $site['body']['runtimeSpecification']);
/**
@@ -74,4 +74,26 @@ final class SpecificationTest extends TestCase
$this->assertContains(SpecificationConstants::S_05VCPU_512MB, $allowed);
$this->assertContains(SpecificationConstants::S_1VCPU_512MB, $allowed);
}
public function testGetAllowedSpecificationsWithBuildPlanLimits(): void
{
$plan = [
'buildSpecifications' => [
SpecificationConstants::S_1VCPU_1GB,
SpecificationConstants::S_2VCPU_2GB,
]
];
$validator = new Specification(
plan: $plan,
specifications: $this->specifications,
maxCpus: 0,
maxMemory: 0,
planKey: 'buildSpecifications'
);
$allowed = $validator->getAllowedSpecifications();
$this->assertCount(2, $allowed);
$this->assertContains(SpecificationConstants::S_1VCPU_1GB, $allowed);
$this->assertContains(SpecificationConstants::S_2VCPU_2GB, $allowed);
}
}