diff --git a/app/app.php b/app/app.php index 5d28266f1e..c5b5ee0078 100644 --- a/app/app.php +++ b/app/app.php @@ -224,8 +224,7 @@ App::init(function ($utopia, $request, $response, $console, $project, $user, $lo ->setParam('payload', []) ->setParam('functionId', null) ->setParam('executionId', null) - ->setParam('functionTag', null) - ->setParam('functionTrigger', 'event') + ->setParam('trigger', 'event') ; $webhooks diff --git a/app/config/collections.php b/app/config/collections.php index 2f630e0ba1..1702bd8979 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1386,6 +1386,15 @@ $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' => 'Status', diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 6118fa6dcc..4d5da270bd 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -444,6 +444,7 @@ App::post('/v1/functions/:functionId/executions') ], 'dateCreated' => time(), 'functionId' => $function->getId(), + 'trigger' => 'http', 'status' => 'waiting', // waiting / processing / completed / failed 'exitCode' => 0, 'stdout' => '', @@ -461,8 +462,7 @@ App::post('/v1/functions/:functionId/executions') 'projectId' => $project->getId(), 'functionId' => $function->getId(), 'executionId' => $execution->getId(), - 'functionTag' => $tag->getId(), - 'functionTrigger' => 'API', + 'trigger' => 'http', ]); } diff --git a/app/workers/functions.php b/app/workers/functions.php index 902fa479f7..547115c14f 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -4,6 +4,7 @@ ini_set('display_startup_errors', 1); error_reporting(E_ALL); use Appwrite\Database\Database; +use Appwrite\Database\Document; use Appwrite\Database\Adapter\MySQL as MySQLAdapter; use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Database\Validator\Authorization; @@ -105,29 +106,51 @@ class FunctionsV1 public function perform() { - global $environments, $register; + global $register; $projectId = $this->args['projectId']; $functionId = $this->args['functionId']; - $functionTag = $this->args['functionTag']; $executionId = $this->args['executionId']; - $functionTrigger = $this->args['functionTrigger']; + $trigger = $this->args['trigger']; - $projectDB = new Database(); - $projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)); - $projectDB->setNamespace('app_'.$projectId); - $projectDB->setMocks(Config::getParam('collections', [])); + $database = new Database(); + $database->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)); + $database->setNamespace('app_'.$projectId); + $database->setMocks(Config::getParam('collections', [])); - Authorization::disable(); - $function = $projectDB->getDocument($functionId); - Authorization::reset(); + switch ($trigger) { + case 'event': + # code... + break; - if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) { - throw new Exception('Function not found', 404); + case 'schedule': + # code... + break; + + case 'http': + Authorization::disable(); + $function = $database->getDocument($functionId); + Authorization::reset(); + + if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) { + throw new Exception('Function not found'); + } + + $this->execute($trigger, $projectId, $executionId, $database, $function); + break; + + default: + # code... + break; } + } + + public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function) + { + $environments = Config::getParam('environments'); Authorization::disable(); - $tag = $projectDB->getDocument($functionTag); + $tag = $database->getDocument($function->getAttribute('tag', '')); Authorization::reset(); if($tag->getAttribute('functionId') !== $function->getId()) { @@ -136,10 +159,10 @@ class FunctionsV1 Authorization::disable(); - $execution = $projectDB->getDocument($executionId); + $execution = $database->getDocument($executionId); if (empty($execution->getId()) || Database::SYSTEM_COLLECTION_EXECUTIONS != $execution->getCollection()) { - $execution = $projectDB->createDocument([ + $execution = $database->createDocument([ '$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS, '$permissions' => [ 'read' => [], @@ -170,10 +193,10 @@ class FunctionsV1 } $vars = \array_merge($function->getAttribute('vars', []), [ - 'APPWRITE_FUNCTION_ID' => $functionId, + 'APPWRITE_FUNCTION_ID' => $function->getId(), 'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''), - 'APPWRITE_FUNCTION_TAG' => $functionTag, - 'APPWRITE_FUNCTION_TRIGGER' => $functionTrigger, + 'APPWRITE_FUNCTION_TAG' => $tag->getId(), + 'APPWRITE_FUNCTION_TRIGGER' => $trigger, 'APPWRITE_FUNCTION_ENV_NAME' => $environment['name'], 'APPWRITE_FUNCTION_ENV_VERSION' => $environment['version'], ]); @@ -305,7 +328,7 @@ class FunctionsV1 Authorization::disable(); - $execution = $projectDB->updateDocument(array_merge($execution->getArrayCopy(), [ + $execution = $database->updateDocument(array_merge($execution->getArrayCopy(), [ 'tagId' => $tag->getId(), 'status' => ($exitCode === 0) ? 'completed' : 'failed', 'exitCode' => $exitCode,