From 3d463c5c81cd32e5306a17f1370e86ab7f9cdba4 Mon Sep 17 00:00:00 2001 From: Fabian Gruber Date: Fri, 25 Oct 2024 15:50:56 +0200 Subject: [PATCH] fix: revert function execution order --- src/Appwrite/Platform/Workers/Functions.php | 90 ++++++++------------- 1 file changed, 34 insertions(+), 56 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index 3dc3e65eee..1da8c88b92 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -52,21 +52,6 @@ class Functions extends Action ->callback(fn (Document $project, Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log, callable $isResourceBlocked) => $this->action($project, $message, $dbForProject, $queueForFunctions, $queueForEvents, $queueForUsage, $log, $isResourceBlocked)); } - /** - * @param Document $project - * @param Message $message - * @param Database $dbForProject - * @param Func $queueForFunctions - * @param Event $queueForEvents - * @param Usage $queueForUsage - * @param Log $log - * @param callable $isResourceBlocked - * @return void - * @throws Authorization - * @throws Structure - * @throws \Utopia\Database\Exception - * @throws Conflict - */ public function action(Document $project, Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log, callable $isResourceBlocked): void { $payload = $message->getPayload() ?? []; @@ -84,9 +69,41 @@ class Functions extends Action return; } - $eventData = $payload['payload'] ?? ''; - $user = new Document($payload['user'] ?? []); $events = $payload['events'] ?? []; + $data = $payload['body'] ?? ''; + $eventData = $payload['payload'] ?? ''; + $function = new Document($payload['function'] ?? []); + $functionId = $payload['functionId'] ?? ''; + $user = new Document($payload['user'] ?? []); + $userId = $payload['userId'] ?? ''; + $method = $payload['method'] ?? 'POST'; + $headers = $payload['headers'] ?? []; + $path = $payload['path'] ?? '/'; + $jwt = $payload['jwt'] ?? ''; + + if ($user->isEmpty() && !empty($userId)) { + $user = $dbForProject->getDocument('users', $userId); + } + + if (empty($jwt) && !$user->isEmpty()) { + $jwtExpiry = $function->getAttribute('timeout', 900); + $jwtObj = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $jwtExpiry, 0); + $jwt = $jwtObj->encode([ + 'userId' => $user->getId(), + ]); + } + + if ($project->getId() === 'console') { + return; + } + + if ($function->isEmpty() && !empty($functionId)) { + $function = $dbForProject->getDocument('functions', $functionId); + } + + $log->addTag('functionId', $function->getId()); + $log->addTag('projectId', $project->getId()); + $log->addTag('type', $type); if (!empty($events)) { $limit = 30; @@ -144,50 +161,11 @@ class Functions extends Action return; } - $data = $payload['body'] ?? ''; - $function = new Document($payload['function'] ?? []); - $functionId = $payload['functionId'] ?? ''; - $userId = $payload['userId'] ?? ''; - $method = $payload['method'] ?? 'POST'; - $headers = $payload['headers'] ?? []; - $path = $payload['path'] ?? '/'; - $jwt = $payload['jwt'] ?? ''; - - if ($user->isEmpty() && !empty($userId)) { - $user = $dbForProject->getDocument('users', $userId); - } - - if (empty($jwt) && !$user->isEmpty()) { - $jwtExpiry = $function->getAttribute('timeout', 900); - $jwtObj = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $jwtExpiry, 0); - $jwt = $jwtObj->encode([ - 'userId' => $user->getId(), - ]); - } - - if ($project->getId() === 'console') { - return; - } - - if ($function->isEmpty() && !empty($functionId)) { - $function = $dbForProject->getDocument('functions', $functionId); - } - - // $function still empty, we can't execute this - if ($function->isEmpty()) { - Console::warning('Got empty function without functionId.'); - return; - } - if ($isResourceBlocked($project, 'functions', $function->getId())) { Console::log('Function ' . $function->getId() . ' is blocked, skipping execution.'); return; } - $log->addTag('functionId', $function->getId()); - $log->addTag('projectId', $project->getId()); - $log->addTag('type', $type); - /** * Handle Schedule and HTTP execution. */