From b431eb4a51743ba5e988143952c82fc50d3b8228 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Fri, 11 Apr 2025 12:13:06 -0700 Subject: [PATCH 1/2] fix(functions): treat 0 as unlimited for CPUs and memory Prior versions of Appwrite treated 0 as unlimited for _APP_FUNCTIONS_CPUS and _APP_FUNCTIONS_MEMORY. As such, this PR updates the specification validator and the list specifications endpoint to also treat the default 0 value as unlimited, allowing any specification. --- app/controllers/api/functions.php | 14 +++++++++----- .../Functions/Validator/RuntimeSpecification.php | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index c7f24f984a..652591a8a6 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -184,8 +184,8 @@ App::post('/v1/functions') ->param('specification', APP_FUNCTION_SPECIFICATION_DEFAULT, fn (array $plan) => new RuntimeSpecification( $plan, Config::getParam('runtime-specifications', []), - App::getEnv('_APP_FUNCTIONS_CPUS', APP_FUNCTION_CPUS_DEFAULT), - App::getEnv('_APP_FUNCTIONS_MEMORY', APP_FUNCTION_MEMORY_DEFAULT) + System::getEnv('_APP_FUNCTIONS_CPUS', 0), + System::getEnv('_APP_FUNCTIONS_MEMORY', 0) ), 'Runtime specification for the function and builds.', true, ['plan']) ->inject('request') ->inject('response') @@ -559,8 +559,12 @@ App::get('/v1/functions/specifications') $spec['enabled'] = in_array($spec['slug'], $plan['runtimeSpecifications']); } + $maxCpus = System::getEnv('_APP_FUNCTIONS_CPUS', 0); + $maxMemory = System::getEnv('_APP_FUNCTIONS_MEMORY', 0); + // Only add specs that are within the limits set by environment variables - if ($spec['cpus'] <= System::getEnv('_APP_FUNCTIONS_CPUS', 1) && $spec['memory'] <= System::getEnv('_APP_FUNCTIONS_MEMORY', 512)) { + // Treat 0 as no limit + if ((empty($maxCpus) || $spec['cpus'] <= $maxCpus) && (empty($maxMemory) || $spec['memory'] <= $maxMemory)) { $runtimeSpecs[] = $spec; } } @@ -858,8 +862,8 @@ App::put('/v1/functions/:functionId') ->param('specification', APP_FUNCTION_SPECIFICATION_DEFAULT, fn (array $plan) => new RuntimeSpecification( $plan, Config::getParam('runtime-specifications', []), - App::getEnv('_APP_FUNCTIONS_CPUS', APP_FUNCTION_CPUS_DEFAULT), - App::getEnv('_APP_FUNCTIONS_MEMORY', APP_FUNCTION_MEMORY_DEFAULT) + System::getEnv('_APP_FUNCTIONS_CPUS', 0), + System::getEnv('_APP_FUNCTIONS_MEMORY', 0) ), 'Runtime specification for the function and builds.', true, ['plan']) ->inject('request') ->inject('response') diff --git a/src/Appwrite/Functions/Validator/RuntimeSpecification.php b/src/Appwrite/Functions/Validator/RuntimeSpecification.php index 22311838f6..b9185d832a 100644 --- a/src/Appwrite/Functions/Validator/RuntimeSpecification.php +++ b/src/Appwrite/Functions/Validator/RuntimeSpecification.php @@ -34,7 +34,7 @@ class RuntimeSpecification extends Validator $allowedSpecifications = []; foreach ($this->specifications as $size => $values) { - if ($values['cpus'] <= $this->maxCpus && $values['memory'] <= $this->maxMemory) { + 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'])) { continue; From 928f535800de4ba45efeae3d88ae9a6a60aa2656 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Thu, 24 Apr 2025 17:30:03 -0700 Subject: [PATCH 2/2] chore(functions): update desc for CPU and mem env vars Clarify that 0 for _APP_FUNCTIONS_CPUS and _APP_FUNCTIONS_MEMORY means unlimited. --- app/config/variables.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/config/variables.php b/app/config/variables.php index a828ceda61..bfdceee027 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -800,7 +800,7 @@ return [ ], [ 'name' => '_APP_FUNCTIONS_CPUS', - 'description' => 'The maximum number of CPU core a single cloud function is allowed to use. Please note that setting a value higher than available cores will result in a function error, which might result in an error. The default value is empty. When it\'s empty, CPU limit will be disabled.', + 'description' => 'The maximum number of CPU core a single cloud function is allowed to use. Please note that setting a value higher than available cores will result in a function error, which might result in an error. The default value is empty. When it\'s empty or 0, CPU limit will be disabled.', 'introduction' => '0.7.0', 'default' => '0', 'required' => false, @@ -809,7 +809,7 @@ return [ ], [ 'name' => '_APP_FUNCTIONS_MEMORY', - 'description' => 'The maximum amount of memory a single cloud function is allowed to use in megabytes. The default value is empty. When it\'s empty, memory limit will be disabled.', + 'description' => 'The maximum amount of memory a single cloud function is allowed to use in megabytes. The default value is empty. When it\'s empty or 0, memory limit will be disabled.', 'introduction' => '0.7.0', 'default' => '0', 'required' => false,