From 4286e8d70b0a810beda744d9fab5035be725130f Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 20 Jan 2022 11:34:50 +0000 Subject: [PATCH] Fix database stuff --- app/controllers/shared/api.php | 4 +-- app/executor.php | 31 ++++++++++++++------ composer.lock | 32 ++++++++++----------- src/Appwrite/Utopia/Response/Model/Func.php | 10 +++---- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 0d37599a4..281e68a80 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -78,8 +78,8 @@ App::init(function ($utopia, $request, $response, $project, $user, $events, $aud ; } - if (($abuse->check() // Route is rate-limited - && App::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled') // Abuse is not disabled + if ((App::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled' // Route is rate-limited + && $abuse->check()) // Abuse is not disabled && (!$isAppUser && !$isPrivilegedUser)) // User is not an admin or API key { throw new Exception('Too many requests', 429); diff --git a/app/executor.php b/app/executor.php index 998dfdab1..fe16c78ce 100644 --- a/app/executor.php +++ b/app/executor.php @@ -36,10 +36,14 @@ use Utopia\Logger\Log; require_once __DIR__ . '/init.php'; +Swoole\Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL); + global $register; $logError = function(Throwable $error, string $action, Utopia\Route $route = null) use ($register) { $logger = $register->get('logger'); + var_dump($error->getTraceAsString()); + if($logger) { $version = App::getEnv('_APP_VERSION', 'UNKNOWN'); @@ -85,8 +89,6 @@ try { $runtimes = Config::getParam('runtimes'); - Swoole\Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL); - // Warmup: make sure images are ready to run fast 🚀 Co\run(function () use ($runtimes, $orchestration) { foreach ($runtimes as $runtime) { @@ -149,6 +151,7 @@ $createRuntimeServer = function(string $functionId, string $projectId, string $t }); if ($tag->getAttribute('buildId') === null) { + var_dump($tag->getArrayCopy()); throw new Exception('Tag has no buildId'); } @@ -337,7 +340,7 @@ $execute = function(string $trigger, string $projectId, string $executionId, str return (!empty($executionId)) ? $database->getDocument('executions', $executionId) : $database->createDocument('executions', new Document([ '$id' => $executionId, '$read' => (!$userId == '') ? ['user:' . $userId] : [], - '$write' => [], + '$write' => ['role:all'], 'dateCreated' => time(), 'functionId' => $function->getId(), 'tagId' => $tag->getId(), @@ -407,7 +410,7 @@ $execute = function(string $trigger, string $projectId, string $executionId, str $database->createDocument('builds', new Document([ '$id' => $buildId, '$read' => (!$userId == '') ? ['user:' . $userId] : [], - '$write' => [], + '$write' => ['role:all'], 'dateCreated' => time(), 'status' => 'processing', 'outputPath' => '', @@ -856,7 +859,7 @@ App::post('/v1/tag') $dbForProject->createDocument('builds', new Document([ '$id' => $buildId, '$read' => (!empty($userId)) ? ['user:' . $userId] : [], - '$write' => [], + '$write' => ['role:all'], 'dateCreated' => time(), 'status' => 'processing', 'runtime' => $function->getAttribute('runtime'), @@ -995,10 +998,19 @@ function runBuildStage(string $buildId, string $projectID, Database $database): { global $runtimes; global $orchestration; + global $register; $buildStdout = ''; $buildStderr = ''; + $db = $register->get('dbPool')->get(); + $redis = $register->get('redisPool')->get(); + $cache = new Cache(new RedisCache($redis)); + + $database = new Database(new MariaDB($db), $cache); + $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); + $database->setNamespace('_project_'.$projectID); + // Check if build has already been run $build = Authorization::skip(function () use ($buildId, $database) { return $database->getDocument('builds', $buildId); @@ -1223,6 +1235,9 @@ function runBuildStage(string $buildId, string $projectID, Database $database): } throw new Exception('Build failed: ' . $e->getMessage()); + } finally { + $register->get('dbPool')->put($db); + $register->get('redisPool')->put($redis); } return $build; @@ -1252,9 +1267,9 @@ $handleShutdown = function() use($logError) $cache = $register->get('redisPool')->get(); $cache = new Cache(new RedisCache($cache)); - $database = new Database(new MariaDB($db), $cache); - $database->setNamespace('project_'.$container->getLabels()["appwrite-project"].'_internal'); + $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); + $database->setNamespace('_project_'.$container->getLabels()["appwrite-project"]); // Get list of all processing executions $executions = Authorization::skip(function () use ($database, $container) { @@ -1386,7 +1401,7 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo ->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate') ->addHeader('Expires', '0') ->addHeader('Pragma', 'no-cache') - ->setStatusCode($code); + ->setStatusCode(500); $response->dynamic( new Document($output), diff --git a/composer.lock b/composer.lock index 4a1c97a55..19daaff15 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0674f59e4259487551f294c7cc8ef1ee", + "content-hash": "adf8727742248da9d7143546e513f96d", "packages": [ { "name": "adhocore/jwt", @@ -118,8 +118,8 @@ "version": "dev-refactor", "source": { "type": "git", - "url": "https://github.com/appwrite/php-runtimes.git", - "reference": "30bf6781acee1e3f32c89821e5706ac6cf7d7c0e" + "url": "https://github.com/appwrite/runtimes.git", + "reference": "96a536e0ad7e3788cd2493e39cdf94b44206411d" }, "require": { "php": ">=8.0", @@ -160,7 +160,7 @@ "php", "runtimes" ], - "time": "2022-01-05T13:09:45+00:00" + "time": "2022-01-19T09:26:05+00:00" }, { "name": "chillerlan/php-qrcode", @@ -349,16 +349,16 @@ }, { "name": "composer/package-versions-deprecated", - "version": "1.11.99.4", + "version": "1.11.99.5", "source": { "type": "git", "url": "https://github.com/composer/package-versions-deprecated.git", - "reference": "b174585d1fe49ceed21928a945138948cb394600" + "reference": "b4f54f74ef3453349c24a845d22392cd31e65f1d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/package-versions-deprecated/zipball/b174585d1fe49ceed21928a945138948cb394600", - "reference": "b174585d1fe49ceed21928a945138948cb394600", + "url": "https://api.github.com/repos/composer/package-versions-deprecated/zipball/b4f54f74ef3453349c24a845d22392cd31e65f1d", + "reference": "b4f54f74ef3453349c24a845d22392cd31e65f1d", "shasum": "" }, "require": { @@ -402,7 +402,7 @@ "description": "Composer plugin that provides efficient querying for installed package versions (no runtime IO)", "support": { "issues": "https://github.com/composer/package-versions-deprecated/issues", - "source": "https://github.com/composer/package-versions-deprecated/tree/1.11.99.4" + "source": "https://github.com/composer/package-versions-deprecated/tree/1.11.99.5" }, "funding": [ { @@ -418,7 +418,7 @@ "type": "tidelift" } ], - "time": "2021-09-13T08:41:34+00:00" + "time": "2022-01-17T14:14:24+00:00" }, { "name": "dragonmantank/cron-expression", @@ -3077,16 +3077,16 @@ }, { "name": "appwrite/sdk-generator", - "version": "0.17.0", + "version": "0.17.1", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator.git", - "reference": "a68e072170a81532cfb0ff914864d8f074a73a37" + "reference": "3542c6ed0f808b6a9f6735a8aad7ccda961bea29" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/a68e072170a81532cfb0ff914864d8f074a73a37", - "reference": "a68e072170a81532cfb0ff914864d8f074a73a37", + "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/3542c6ed0f808b6a9f6735a8aad7ccda961bea29", + "reference": "3542c6ed0f808b6a9f6735a8aad7ccda961bea29", "shasum": "" }, "require": { @@ -3120,9 +3120,9 @@ "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", "support": { "issues": "https://github.com/appwrite/sdk-generator/issues", - "source": "https://github.com/appwrite/sdk-generator/tree/0.17.0" + "source": "https://github.com/appwrite/sdk-generator/tree/0.17.1" }, - "time": "2022-01-04T09:24:44+00:00" + "time": "2022-01-07T12:55:37+00:00" }, { "name": "composer/pcre", diff --git a/src/Appwrite/Utopia/Response/Model/Func.php b/src/Appwrite/Utopia/Response/Model/Func.php index fe1245545..15abb8348 100644 --- a/src/Appwrite/Utopia/Response/Model/Func.php +++ b/src/Appwrite/Utopia/Response/Model/Func.php @@ -128,13 +128,13 @@ class Func extends Model */ public function filter(Document $document): Document { - $prefs = $document->getAttribute('vars'); - if($prefs instanceof Document) { - $prefs = $prefs->getArrayCopy(); + $vars = $document->getAttribute('vars'); + if($vars instanceof Document) { + $vars = $vars->getArrayCopy(); } - if(is_array($prefs) && empty($prefs)) { - $document->setAttribute('vars', new stdClass); + if(is_array($vars) && empty($vars)) { + $document->setAttribute('vars', new stdClass()); } return $document; }