2020-05-05 01:34:31 +12:00
|
|
|
<?php
|
|
|
|
|
2020-05-10 10:12:00 +12:00
|
|
|
use Utopia\CLI\Console;
|
2020-05-05 01:34:31 +12:00
|
|
|
use Utopia\Config\Config;
|
|
|
|
use Appwrite\Database\Database;
|
|
|
|
use Appwrite\Database\Validator\Authorization;
|
2020-05-09 18:26:18 +12:00
|
|
|
|
2020-05-10 04:39:50 +12:00
|
|
|
require_once __DIR__.'/../init.php';
|
|
|
|
|
|
|
|
cli_set_process_title('Functions V1 Worker');
|
|
|
|
|
|
|
|
Console::success(APP_NAME.' functions worker v1 has started');
|
|
|
|
|
2020-05-10 10:12:00 +12:00
|
|
|
$environments = Config::getParam('environments');
|
2020-05-09 18:26:18 +12:00
|
|
|
|
2020-05-10 10:12:00 +12:00
|
|
|
foreach($environments as $environment) {
|
2020-05-09 18:26:18 +12:00
|
|
|
$stdout = '';
|
|
|
|
$stderr = '';
|
2020-05-10 22:30:07 +12:00
|
|
|
|
|
|
|
Console::info('Warming up '.$environment['name'].' environment');
|
|
|
|
|
2020-05-10 10:12:00 +12:00
|
|
|
Console::execute('docker pull '.$environment['image'], null, $stdout, $stderr);
|
2020-05-09 18:26:18 +12:00
|
|
|
|
2020-05-10 10:12:00 +12:00
|
|
|
if(!empty($stdout)) {
|
|
|
|
Console::success($stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!empty($stderr)) {
|
|
|
|
Console::error($stderr);
|
|
|
|
}
|
2020-05-09 18:26:18 +12:00
|
|
|
}
|
2020-05-05 01:34:31 +12:00
|
|
|
|
|
|
|
class FunctionsV1
|
|
|
|
{
|
|
|
|
public $args = [];
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function perform()
|
|
|
|
{
|
2020-05-10 22:30:07 +12:00
|
|
|
global $environments;
|
|
|
|
|
2020-05-13 10:00:48 +12:00
|
|
|
/*
|
|
|
|
* 1. Get Original Task
|
|
|
|
* 2. Check for updates
|
|
|
|
* If has updates skip task and don't reschedule
|
|
|
|
* If status not equal to play skip task
|
|
|
|
* 3. Check next run date, update task and add new job at the given date
|
|
|
|
* 4. Execute task (set optional timeout)
|
|
|
|
* 5. Update task response to log
|
|
|
|
* On success reset error count
|
|
|
|
* On failure add error count
|
|
|
|
* If error count bigger than allowed change status to pause
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-05-10 22:30:07 +12:00
|
|
|
/**
|
|
|
|
* 1. Get event args
|
|
|
|
* 2. Unpackage code in an isolated folder
|
2020-05-13 10:00:48 +12:00
|
|
|
* 3. Execute in container with timeout
|
|
|
|
* + messure execution time
|
|
|
|
* + pass env vars
|
|
|
|
* + pass one-time api key
|
2020-05-10 22:30:07 +12:00
|
|
|
* 4. Update execution status
|
|
|
|
* 5. Update execution stdout & stderr
|
|
|
|
* 6. Trigger audit log
|
|
|
|
* 7. Trigger usage log
|
|
|
|
*/
|
2020-05-11 07:56:36 +12:00
|
|
|
$stdout = '';
|
|
|
|
$stderr = '';
|
|
|
|
$image = 'php:7.4-cli';
|
|
|
|
$timeout = 15;
|
|
|
|
|
|
|
|
$start = microtime(true);
|
|
|
|
|
2020-05-13 10:00:48 +12:00
|
|
|
//TODO aviod scheduled execution if delay is bigger than X offest
|
|
|
|
|
2020-05-11 18:48:26 +12:00
|
|
|
/**
|
|
|
|
* Limit CPU Usage
|
|
|
|
* Limit Memory Usage
|
|
|
|
* 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
|
|
|
|
*/
|
2020-05-11 07:56:36 +12:00
|
|
|
Console::execute("docker run \
|
|
|
|
--rm \
|
|
|
|
-v $(pwd):/app \
|
|
|
|
-w /app \
|
|
|
|
{$image} \
|
|
|
|
php -v", null, $stdout, $stderr, $timeout);
|
2020-05-10 22:30:07 +12:00
|
|
|
|
2020-05-11 07:56:36 +12:00
|
|
|
$end = microtime(true);
|
|
|
|
|
|
|
|
echo "The code took " . ($end - $start) . " seconds to complete.";
|
2020-05-05 01:34:31 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|