diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index fa84ff6c1..c7f24bc88 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -442,6 +442,10 @@ App::post('/v1/functions/:functionId/executions') if((bool)$async) { // Issue a TLS certificate when domain is verified Resque::enqueue('v1-functions', 'FunctionsV1', [ + 'functionId' => $function->getId(), + 'functionTag' => $tag->getId(), + 'functionEnv' => $function->getAttribute('env', ''), + 'functionCommand' => $tag->getAttribute('command', ''), ]); } diff --git a/app/views/console/functions/function.phtml b/app/views/console/functions/function.phtml index 927ddfd06..ecef9861e 100644 --- a/app/views/console/functions/function.phtml +++ b/app/views/console/functions/function.phtml @@ -63,11 +63,8 @@ $events = array_keys($this->getParam('events', [])); data-failure="alert" data-failure-param-alert-text="Failed to execute function" data-failure-param-alert-classname="error"> - +   - - -   diff --git a/app/workers/functions.php b/app/workers/functions.php index 58e500c77..b2049fad5 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -51,6 +51,17 @@ class FunctionsV1 { global $environments; + $functionId = $this->args['functionId']; + $functionTag = $this->args['functionTag']; + $functionCommand = $this->args['functionCommand']; + $functionEnv = $this->args['functionEnv']; + + $environment = (isset($environments[$functionEnv])) ? $environments[$functionEnv] : null; + + if(is_null($environment)) { + throw new Exception('Environment "'.$functionEnv.' is not supported'); + } + /* * 1. Get Original Task * 2. Check for updates @@ -67,7 +78,7 @@ class FunctionsV1 /** * 1. Get event args - * 2. Unpackage code in an isolated folder + * 2. Unpackage code in the isolated container * 3. Execute in container with timeout * + messure execution time * + pass env vars @@ -79,7 +90,6 @@ class FunctionsV1 */ $stdout = ''; $stderr = ''; - $image = 'php:7.4-cli'; $timeout = 15; $start = microtime(true); @@ -87,23 +97,38 @@ class FunctionsV1 //TODO aviod scheduled execution if delay is bigger than X offest /** - * Limit CPU Usage - * Limit Memory Usage + * Limit CPU Usage - DONE + * Limit Memory Usage - DONE * Limit Network Usage * Make sure no access to redis, mariadb, influxdb or other system services * Make sure no access to NFS server / storage volumes * Access Appwrite REST from internal network for improved performance */ - Console::execute("docker run \ + + //--storage-opt size=120m \ + $exitCode = Console::execute("docker run \ + --cpus=1 \ + --memory=50m \ + --memory-swap=50m \ --rm \ - -v $(pwd):/app \ - -w /app \ - {$image} \ - php -v", null, $stdout, $stderr, $timeout); + --name=appwrite-function- \ + --volume $(pwd):/app \ + --workdir /app \ + --env APPWRITE_FUNCTION_ID={$functionId} \ + --env APPWRITE_FUNCTION_TAG={$functionTag} \ + --env APPWRITE_FUNCTION_ENV_NAME={$environment['name']} \ + --env APPWRITE_FUNCTION_ENV_VERSION={$environment['version']} \ + {$environment['image']} \ + {$functionCommand}", null, $stdout, $stderr, $timeout); $end = microtime(true); - echo "The code took " . ($end - $start) . " seconds to complete."; + Console::info("Code executed in " . ($end - $start) . " seconds with exit code {$exitCode}"); + + // Double-check Cleanup + + var_dump($stdout); + var_dump($stderr); } public function tearDown()