1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00

Fixed event triggers

This commit is contained in:
Eldad Fux 2020-08-05 08:18:45 +03:00
parent 05c01dc1a7
commit a361b3d181
3 changed files with 38 additions and 32 deletions

View file

@ -220,7 +220,7 @@ App::init(function ($utopia, $request, $response, $console, $project, $user, $lo
*/ */
$functions $functions
->setParam('projectId', $project->getId()) ->setParam('projectId', $project->getId())
->setParam('event', $route->getLabel('events', '')) ->setParam('event', $route->getLabel('event', ''))
->setParam('payload', []) ->setParam('payload', [])
->setParam('functionId', null) ->setParam('functionId', null)
->setParam('executionId', null) ->setParam('executionId', null)
@ -229,7 +229,7 @@ App::init(function ($utopia, $request, $response, $console, $project, $user, $lo
$webhooks $webhooks
->setParam('projectId', $project->getId()) ->setParam('projectId', $project->getId())
->setParam('event', $route->getLabel('events', '')) ->setParam('event', $route->getLabel('event', ''))
->setParam('payload', []) ->setParam('payload', [])
; ;
@ -266,6 +266,7 @@ App::shutdown(function ($utopia, $request, $response, $project, $webhooks, $audi
/** @var Appwrite\Event\Event $functions */ /** @var Appwrite\Event\Event $functions */
/** @var bool $mode */ /** @var bool $mode */
var_dump($functions->getParam('event'));
if (!empty($functions->getParam('event'))) { if (!empty($functions->getParam('event'))) {
$functions->trigger(); $functions->trigger();
} }

View file

@ -129,15 +129,6 @@ class FunctionsV1
$functions = []; /** @var Document[] $functions */ $functions = []; /** @var Document[] $functions */
while ($sum >= $limit) { 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(); Authorization::disable();
@ -158,6 +149,21 @@ class FunctionsV1
$offset = $offset + $limit; $offset = $offset + $limit;
Console::log('Fetched '.$sum.' functions...'); Console::log('Fetched '.$sum.' functions...');
foreach($functions as $function) {
$events = $function->getAttribute('events', []);
$tag = $function->getAttribute('tag', []);
Console::success('Itterating function: '.$function->getAttribute('name'));
if(!\in_array($event, $events) || empty($tag)) {
continue;
}
Console::success('Triggered function: '.$event);
$this->execute('event', $projectId, '', $database, $function);
}
} }
break; break;
@ -199,27 +205,24 @@ class FunctionsV1
Authorization::disable(); Authorization::disable();
$execution = $database->getDocument($executionId); $execution = (!empty($executionId)) ? $database->getDocument($executionId) : $database->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS,
'$permissions' => [
'read' => [],
'write' => [],
],
'dateCreated' => time(),
'functionId' => $function->getId(),
'trigger' => $trigger, // http / schedule / event
'status' => 'processing', // waiting / processing / completed / failed
'exitCode' => 0,
'stdout' => '',
'stderr' => '',
'time' => 0,
]);
if (empty($execution->getId()) || Database::SYSTEM_COLLECTION_EXECUTIONS != $execution->getCollection()) { if(false === $execution) {
$execution = $database->createDocument([ throw new Exception('Failed to create execution');
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS,
'$permissions' => [
'read' => [],
'write' => [],
],
'dateCreated' => \time(),
'functionId' => $function->getId(),
'status' => 'processing', // waiting / processing / completed / failed
'exitCode' => 0,
'stdout' => '',
'stderr' => '',
'time' => 0,
]);
if (false === $execution) {
throw new Exception('Failed saving execution to DB', 500);
}
} }
Authorization::reset(); Authorization::reset();
@ -232,6 +235,8 @@ class FunctionsV1
throw new Exception('Environment "'.$function->getAttribute('env', '').' is not supported'); throw new Exception('Environment "'.$function->getAttribute('env', '').' is not supported');
} }
var_dump($function->getAttribute('name', ''));
var_dump($function->getAttribute('vars', []));
$vars = \array_merge($function->getAttribute('vars', []), [ $vars = \array_merge($function->getAttribute('vars', []), [
'APPWRITE_FUNCTION_ID' => $function->getId(), 'APPWRITE_FUNCTION_ID' => $function->getId(),
'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''), 'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''),

View file

@ -157,7 +157,7 @@ class Database
$results = $this->adapter->getCollection($options); $results = $this->adapter->getCollection($options);
foreach ($results as &$node) { foreach ($results as &$node) {
$node = new Document($node); $node = $this->decode(new Document($node));
} }
return $results; return $results;