From 05c01dc1a7280ecf1c519872880912beaf07797b Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Tue, 4 Aug 2020 23:09:01 +0300 Subject: [PATCH] Added event exectuions for cloud functions --- app/config/collections.php | 9 ------ app/controllers/api/functions.php | 3 +- app/workers/functions.php | 51 +++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index ed170a57e..1702bd897 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1404,15 +1404,6 @@ $collections = [ 'required' => false, 'array' => false, ], - [ - '$collection' => Database::SYSTEM_COLLECTION_RULES, - 'label' => 'Trigger', - 'key' => 'trigger', - 'type' => Database::SYSTEM_VAR_TYPE_TEXT, - 'default' => '', - 'required' => false, - 'array' => false, - ], [ '$collection' => Database::SYSTEM_COLLECTION_RULES, 'label' => 'Exit Code', diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index e08801f07..346c144fa 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -577,9 +577,8 @@ App::post('/v1/functions/:functionId/executions') ], 'dateCreated' => time(), 'functionId' => $function->getId(), - 'trigger' => 'http', - 'status' => 'waiting', // waiting / processing / completed / failed 'trigger' => 'http', // http / schedule / event + 'status' => 'waiting', // waiting / processing / completed / failed 'exitCode' => 0, 'stdout' => '', 'stderr' => '', diff --git a/app/workers/functions.php b/app/workers/functions.php index f773bb7c6..85c7a88da 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -113,6 +113,7 @@ class FunctionsV1 $functionId = $this->args['functionId']; $executionId = $this->args['executionId']; $trigger = $this->args['trigger']; + $event = $this->args['event']; $database = new Database(); $database->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)); @@ -121,7 +122,43 @@ class FunctionsV1 switch ($trigger) { case 'event': - # code... + + $limit = 30; + $sum = 30; + $offset = 0; + $functions = []; /** @var Document[] $functions */ + + while ($sum >= $limit) { + foreach($functions as $function) { + $events = $function->getAttribute('events', []); + + if(!\in_array($event, $events)) { + continue; + } + + $this->execute('event', $projectId, $executionId, $database, $function); + } + + Authorization::disable(); + + $functions = $database->getCollection([ + 'limit' => $limit, + 'offset' => $offset, + 'orderField' => 'name', + 'orderType' => 'ASC', + 'orderCast' => 'string', + 'filters' => [ + '$collection='.Database::SYSTEM_COLLECTION_FUNCTIONS, + ], + ]); + + Authorization::reset(); + + $sum = \count($functions); + $offset = $offset + $limit; + + Console::log('Fetched '.$sum.' functions...'); + } break; case 'schedule': @@ -148,6 +185,8 @@ class FunctionsV1 public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function) { + global $register; + $environments = Config::getParam('environments'); Authorization::disable(); @@ -339,8 +378,8 @@ class FunctionsV1 'tagId' => $tag->getId(), 'status' => $functionStatus, 'exitCode' => $exitCode, - 'stdout' => mb_substr($stdout, -4000), // log last 4000 chars output - 'stderr' => mb_substr($stderr, -4000), // log last 4000 chars output + 'stdout' => \mb_substr($stdout, -4000), // log last 4000 chars output + 'stderr' => \mb_substr($stderr, -4000), // log last 4000 chars output 'time' => $executionTime, ])); @@ -401,14 +440,14 @@ class FunctionsV1 public function filterEnvKey(string $string): string { if(empty($this->allowed)) { - $this->allowed = array_fill_keys(str_split('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_'), true); + $this->allowed = array_fill_keys(\str_split('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_'), true); } - $string = str_split($string); + $string = \str_split($string); $output = ''; foreach ($string as $char) { - if(array_key_exists($char, $this->allowed)) { + if(\array_key_exists($char, $this->allowed)) { $output .= $char; } }