diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 0d8bb0f9ed..bbd97b0aad 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -509,7 +509,7 @@ App::post('/v1/functions/:functionId/tags') /** @var Utopia\Database\Database $dbForProject */ /** @var Appwrite\Event\Event $usage */ /** @var Appwrite\Auth\User $user */ - /** @var Appwrite\Project\Project $project */ + /** @var Appwrite\Database\Document $project */ $function = $dbForProject->getDocument('functions', $functionId); @@ -556,6 +556,7 @@ App::post('/v1/functions/:functionId/tags') // Remove automaticDeploy for all other tags. $tags = $dbForProject->find('tags', [ new Query('automaticDeploy', Query::TYPE_EQUAL, [true]), + new Query('functionId', Query::TYPE_EQUAL, [$functionId]) ]); foreach ($tags as $tag) { @@ -567,8 +568,8 @@ App::post('/v1/functions/:functionId/tags') $tagId = $dbForProject->getId(); $tag = $dbForProject->createDocument('tags', new Document([ '$id' => $tagId, - '$read' => [], - '$write' => [], + '$read' => ['role:all'], + '$write' => ['role:all'], 'functionId' => $function->getId(), 'dateCreated' => time(), 'entrypoint' => $entrypoint, @@ -592,7 +593,7 @@ App::post('/v1/functions/:functionId/tags') \curl_setopt($ch, CURLOPT_URL, "http://appwrite-executor:8080/v1/tag"); \curl_setopt($ch, CURLOPT_POST, true); \curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ - 'functionId' => $functionId, + 'functionId' => $function->getId(), 'tagId' => $tag->getId(), 'userId' => $user->getId(), ])); @@ -873,7 +874,7 @@ App::post('/v1/functions/:functionId/executions') $execution = Authorization::skip(fn() => $dbForProject->createDocument('executions', new Document([ '$id' => $executionId, '$read' => (!$user->isEmpty()) ? ['user:' . $user->getId()] : [], - '$write' => [], + '$write' => ['role:all'], 'dateCreated' => time(), 'functionId' => $function->getId(), 'tagId' => $tag->getId(), diff --git a/app/executor.php b/app/executor.php index fe16c78ceb..5a633b9835 100644 --- a/app/executor.php +++ b/app/executor.php @@ -142,16 +142,12 @@ $createRuntimeServer = function(string $functionId, string $projectId, string $t global $activeFunctions; // Grab Function Document - $function = Authorization::skip(function () use ($database, $functionId) { - return $database->getDocument('functions', $functionId); - }); - - $tag = Authorization::skip(function () use ($database, $tagId) { - return $database->getDocument('tags', $tagId); - }); + /** @var Document $function */ + $function = Authorization::skip(fn () => $database->getDocument('functions', $functionId)); + /** @var Document $tag */ + $tag = Authorization::skip(fn () => $database->getDocument('tags', $tagId)); if ($tag->getAttribute('buildId') === null) { - var_dump($tag->getArrayCopy()); throw new Exception('Tag has no buildId'); } @@ -482,8 +478,7 @@ $execute = function(string $trigger, string $projectId, string $executionId, str ]; } - $internalFunction = $activeFunctions->get('appwrite-function-' . $tag->getId()); - $key = $internalFunction['key']; + $key = $activeFunctions->get('appwrite-function-' . $tag->getId(), 'key'); // Process environment variables $vars = \array_merge($function->getAttribute('vars', []), [ @@ -571,7 +566,7 @@ $execute = function(string $trigger, string $projectId, string $executionId, str } // 110 is the Swoole error code for timeout, see: https://www.swoole.co.uk/docs/swoole-error-code - if ($errNo !== 0 && $errNo != CURLE_COULDNT_CONNECT && $errNo != CURLE_OPERATION_TIMEDOUT && $errNo != 110) { + if ($errNo !== 0 && $errNo !== CURLE_COULDNT_CONNECT && $errNo !== CURLE_OPERATION_TIMEDOUT && $errNo !== 110) { throw new Exception('An internal curl error has occurred within the executor! Error Msg: ' . $error, 500); } @@ -824,17 +819,13 @@ App::post('/v1/tag') ->inject('dbForProject') ->inject('projectID') ->action(function ($functionId, $tagId, $userId, $autoDeploy, $response, $dbForProject, $projectID) use ($createRuntimeServer) { + /** @var Utopia\Database\Database $dbForProject */ global $runtimes; // Get function document - $function = Authorization::skip(function () use ($functionId, $dbForProject) { - return $dbForProject->getDocument('functions', $functionId); - }); - + $function = Authorization::skip(fn () => $dbForProject->getDocument('functions', $functionId)); // Get tag document - $tag = Authorization::skip(function () use ($tagId, $dbForProject) { - return $dbForProject->getDocument('tags', $tagId); - }); + $tag = Authorization::skip(fn () => $dbForProject->getDocument('tags', $tagId)); // Check if both documents exist if ($function->isEmpty()) { @@ -842,6 +833,7 @@ App::post('/v1/tag') } if ($tag->isEmpty()) { + var_dump($tag->getArrayCopy()); throw new Exception('Tag not found', 404); } @@ -856,32 +848,37 @@ App::post('/v1/tag') $buildId = $tag->getAttribute('buildId'); } else { Authorization::skip(function () use ($buildId, $dbForProject, $tag, $userId, $function, $projectID, $runtime) { - $dbForProject->createDocument('builds', new Document([ - '$id' => $buildId, - '$read' => (!empty($userId)) ? ['user:' . $userId] : [], - '$write' => ['role:all'], - 'dateCreated' => time(), - 'status' => 'processing', - 'runtime' => $function->getAttribute('runtime'), - 'outputPath' => '', - 'source' => $tag->getAttribute('path'), - 'sourceType' => Storage::DEVICE_LOCAL, - 'stdout' => '', - 'stderr' => '', - 'buildTime' => 0, - 'envVars' => [ - 'ENTRYPOINT_NAME' => $tag->getAttribute('entrypoint'), - 'APPWRITE_FUNCTION_ID' => $function->getId(), - 'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''), - 'APPWRITE_FUNCTION_RUNTIME_NAME' => $runtime['name'], - 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $runtime['version'], - 'APPWRITE_FUNCTION_PROJECT_ID' => $projectID, - ] - ])); - - $tag->setAttribute('buildId', $buildId); - - $dbForProject->updateDocument('tags', $tag->getId(), $tag); + try { + $dbForProject->createDocument('builds', new Document([ + '$id' => $buildId, + '$read' => (!empty($userId)) ? ['user:' . $userId] : [], + '$write' => ['role:all'], + 'dateCreated' => time(), + 'status' => 'processing', + 'runtime' => $function->getAttribute('runtime'), + 'outputPath' => '', + 'source' => $tag->getAttribute('path'), + 'sourceType' => Storage::DEVICE_LOCAL, + 'stdout' => '', + 'stderr' => '', + 'buildTime' => 0, + 'envVars' => [ + 'ENTRYPOINT_NAME' => $tag->getAttribute('entrypoint'), + 'APPWRITE_FUNCTION_ID' => $function->getId(), + 'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''), + 'APPWRITE_FUNCTION_RUNTIME_NAME' => $runtime['name'], + 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $runtime['version'], + 'APPWRITE_FUNCTION_PROJECT_ID' => $projectID, + ] + ])); + + $tag->setAttribute('buildId', $buildId); + + $dbForProject->updateDocument('tags', $tag->getId(), $tag); + } catch (\Throwable $th) { + var_dump($tag->getArrayCopy()); + throw $th; + } }); } @@ -1213,21 +1210,18 @@ function runBuildStage(string $buildId, string $projectID, Database $database): ->setAttribute('buildTime', $buildTime); // Update build with built code attribute - $build = Authorization::skip(function () use ($build, $buildId, $database) { - return $database->updateDocument('builds', $buildId, $build); - }); + $build = Authorization::skip(fn () => $database->updateDocument('builds', $buildId, $build)); $buildEnd = \microtime(true); Console::info('Build Stage Ran in ' . ($buildEnd - $buildStart) . ' seconds'); } catch (Exception $e) { + var_dump($e->getTraceAsString()); $build->setAttribute('status', 'failed') ->setAttribute('stdout', \utf8_encode(\mb_substr($buildStdout, -4096))) ->setAttribute('stderr', \utf8_encode(\mb_substr($e->getMessage(), -4096))); - $build = Authorization::skip(function () use ($build, $buildId, $database) { - return $database->updateDocument('builds', $buildId, $build); - }); + $build = Authorization::skip(fn () => $database->updateDocument('builds', $buildId, $build)); // also remove the container if it exists if ($id) {