1
0
Fork 0
mirror of synced 2024-05-21 05:02:37 +12:00

Manual function execution

This commit is contained in:
Eldad Fux 2020-07-15 23:29:55 +03:00
parent 4c022e96f1
commit 3c194b5076
3 changed files with 40 additions and 14 deletions

View file

@ -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', ''),
]);
}

View file

@ -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">
<button class="margin-bottom-small">Execute Now</button>
<button class="margin-bottom-small">Execute Now</button> &nbsp; <button class="reverse">View Logs</button>
</form>
&nbsp; <button class="reverse">View Logs</button>
</div>
</div>

View file

@ -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()