From b727b8c100b67965c6bbdcae7bc26e03a9fa241d Mon Sep 17 00:00:00 2001 From: Khushboo Verma Date: Thu, 22 May 2025 18:01:34 +0530 Subject: [PATCH 1/2] Add configurable deployment and build size --- app/worker.php | 4 ++++ .../Functions/Http/Deployments/Create.php | 12 ++++++++-- .../Modules/Functions/Workers/Builds.php | 23 +++++++++++++++---- .../Modules/Sites/Http/Deployments/Create.php | 12 ++++++++-- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/worker.php b/app/worker.php index 79cfffc8bb0..1277162bfa3 100644 --- a/app/worker.php +++ b/app/worker.php @@ -240,6 +240,10 @@ Server::setResource('log', fn () => new Log()); +Server::setResource('plan', function (array $plan = []) { + return []; +}); + Server::setResource('publisher', function (Group $pools) { return new BrokerPool(publisher: $pools->get('publisher')); }, ['pools']); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php index 54894f2caa7..cda6dc38bfb 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php @@ -86,6 +86,7 @@ public function __construct() ->inject('deviceForFunctions') ->inject('deviceForLocal') ->inject('queueForBuilds') + ->inject('plan') ->callback([$this, 'action']); } @@ -102,7 +103,8 @@ public function action( Document $project, Device $deviceForFunctions, Device $deviceForLocal, - Build $queueForBuilds + Build $queueForBuilds, + array $plan ) { $activate = \strval($activate) === 'true' || \strval($activate) === '1'; @@ -135,8 +137,14 @@ public function action( throw new Exception(Exception::STORAGE_FILE_EMPTY, 'No file sent'); } + $functionSizeLimit = (int) System::getEnv('_APP_COMPUTE_SIZE_LIMIT', '30000000'); + + if (isset($plan['deploymentSize'])) { + $functionSizeLimit = $plan['deploymentSize'] * 1000 * 1000; + } + $fileExt = new FileExt([FileExt::TYPE_GZIP]); - $fileSizeValidator = new FileSize(System::getEnv('_APP_COMPUTE_SIZE_LIMIT', '30000000')); + $fileSizeValidator = new FileSize($functionSizeLimit); $upload = new Upload(); // Make sure we handle a single file and multiple files the same way diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 163623241c2..6ff2161c1af 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -73,6 +73,7 @@ public function __construct() ->inject('deviceForFiles') ->inject('log') ->inject('executor') + ->inject('plan') ->callback([$this, 'action']); } @@ -92,6 +93,7 @@ public function __construct() * @param Device $deviceForFiles * @param Log $log * @param Executor $executor + * @param array $plan * @return void * @throws \Utopia\Database\Exception */ @@ -111,7 +113,8 @@ public function action( callable $isResourceBlocked, Device $deviceForFiles, Log $log, - Executor $executor + Executor $executor, + array $plan ): void { $payload = $message->getPayload() ?? []; @@ -150,7 +153,8 @@ public function action( $template, $isResourceBlocked, $log, - $executor + $executor, + $plan ); break; @@ -177,6 +181,7 @@ public function action( * @param Document $template * @param Log $log * @param Executor $executor + * @param array $plan * @return void * @throws \Utopia\Database\Exception * @@ -200,7 +205,8 @@ protected function buildDeployment( Document $template, callable $isResourceBlocked, Log $log, - Executor $executor + Executor $executor, + array $plan ): void { $resourceKey = match ($resource->getCollection()) { 'functions' => 'functionId', @@ -476,8 +482,12 @@ protected function buildDeployment( $directorySize = $localDevice->getDirectorySize($tmpDirectory); $sizeLimit = (int)System::getEnv('_APP_COMPUTE_SIZE_LIMIT', '30000000'); + if (isset($plan['deploymentSize'])) { + $sizeLimit = (int) $plan['deploymentSize'] * 1000 * 1000; + } + if ($directorySize > $sizeLimit) { - throw new \Exception('Repository directory size should be less than ' . number_format($sizeLimit / 1048576, 2) . ' MBs.'); + throw new \Exception('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); @@ -803,8 +813,11 @@ protected function buildDeployment( $durationEnd = \microtime(true); $buildSizeLimit = (int)System::getEnv('_APP_COMPUTE_BUILD_SIZE_LIMIT', '2000000000'); + if (isset($plan['buildSize'])) { + $buildSizeLimit = $plan['buildSize'] * 1000 * 1000; + } if ($response['size'] > $buildSizeLimit) { - throw new \Exception('Build size should be less than ' . number_format($buildSizeLimit / 1048576, 2) . ' MBs.'); + throw new \Exception('Build size should be less than ' . number_format($buildSizeLimit / (1000 * 1000), 2) . ' MBs.'); } /** Update the build document */ diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php index 7400d86e740..afa1dba7d89 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php @@ -85,6 +85,7 @@ public function __construct() ->inject('deviceForSites') ->inject('deviceForLocal') ->inject('queueForBuilds') + ->inject('plan') ->callback([$this, 'action']); } @@ -103,7 +104,8 @@ public function action( Event $queueForEvents, Device $deviceForSites, Device $deviceForLocal, - Build $queueForBuilds + Build $queueForBuilds, + array $plan ) { $activate = \strval($activate) === 'true' || \strval($activate) === '1'; @@ -136,8 +138,14 @@ public function action( throw new Exception(Exception::STORAGE_FILE_EMPTY, 'No file sent'); } + $siteSizeLimit = (int) System::getEnv('_APP_COMPUTE_SIZE_LIMIT', '30000000'); + + if (isset($plan['deploymentSize'])) { + $siteSizeLimit = $plan['deploymentSize'] * 1000 * 1000; + } + $fileExt = new FileExt([FileExt::TYPE_GZIP]); - $fileSizeValidator = new FileSize(System::getEnv('_APP_COMPUTE_SIZE_LIMIT', '30000000')); + $fileSizeValidator = new FileSize($siteSizeLimit); $upload = new Upload(); // Make sure we handle a single file and multiple files the same way From 9321f585f5d67e23944a3c979fd27629957e3439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 22 May 2025 15:22:40 +0200 Subject: [PATCH 2/2] Update app/worker.php --- app/worker.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/worker.php b/app/worker.php index 1277162bfa3..597e8a9943b 100644 --- a/app/worker.php +++ b/app/worker.php @@ -240,9 +240,6 @@ Server::setResource('log', fn () => new Log()); -Server::setResource('plan', function (array $plan = []) { - return []; -}); Server::setResource('publisher', function (Group $pools) { return new BrokerPool(publisher: $pools->get('publisher'));