From ceb11f839fcb57fb12ea7153dd4c69eabeb33fbc Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 24 Aug 2022 14:21:26 +0530 Subject: [PATCH] feat: fix realtime tests --- app/http.php | 1 - app/realtime.php | 61 +++++++++++++++++++++++++-------- composer.lock | 62 +++++++++++++++++----------------- src/Appwrite/Resque/Worker.php | 3 +- 4 files changed, 79 insertions(+), 48 deletions(-) diff --git a/app/http.php b/app/http.php index 86efc60b8d..1c0c26f966 100644 --- a/app/http.php +++ b/app/http.php @@ -345,7 +345,6 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo $swooleResponse->end(\json_encode($output)); } finally { $dbPool->reset(); - /** @var RedisPool $redisPool */ $redisPool = $register->get('redisPool'); $redisPool->put($redis); diff --git a/app/realtime.php b/app/realtime.php index 729e9b987a..d1646c1426 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -19,6 +19,10 @@ use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Registry\Registry; use Appwrite\Utopia\Request; +use Utopia\Cache\Adapter\Redis as RedisCache; +use Utopia\Cache\Cache; +use Utopia\Database\Adapter\MariaDB; +use Utopia\Database\Database; use Utopia\WebSocket\Server; use Utopia\WebSocket\Adapter; @@ -88,10 +92,27 @@ $logError = function (Throwable $error, string $action) use ($register) { $server->error($logError); -function getDatabase(Registry &$register, string $projectID) +function getDatabase(Registry &$register, string $projectId) { $redis = $register->get('redisPool')->get(); - $database = $register->get('dbPool')->getDBFromPool($projectID, $redis); + $dbPool = $register->get('dbPool'); + + /** Get the console DB */ + $database = $dbPool->getConsoleDB(); + $pdo = $dbPool->getDBFromPool($database); + $cache = new Cache(new RedisCache($redis)); + $database = new Database(new MariaDB($pdo->getConnection()), $cache); + $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); + $database->setNamespace("_console"); + + if ($projectId !== 'console') { + $project = Authorization::skip(fn() => $database->getDocument('projects', $projectId)); + $database = $project->getAttribute('database', ''); + $pdo = $dbPool->getDBFromPool($database); + $database = new Database(new MariaDB($pdo->getConnection()), $cache); + $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); + $database->setNamespace("_{$project->getInternalId()}"); + } return [ $database, @@ -341,9 +362,6 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, App::setResource('response', fn() => $response); try { - /** @var \Utopia\Database\Document $console */ - $console = $app->getResource('console'); - /** @var \Utopia\Database\Document $project */ $project = $app->getResource('project'); @@ -355,7 +373,9 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, } $dbForProject = $app->getResource('dbForProject'); - + + /** @var \Utopia\Database\Document $console */ + $console = $app->getResource('console'); /** @var \Utopia\Database\Document $user */ $user = $app->getResource('user'); @@ -452,20 +472,33 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, $server->onMessage(function (int $connection, string $message) use ($server, $register, $realtime, $containerId) { try { $response = new Response(new SwooleResponse()); - - $projectId = $realtime->connections[$connection]['projectId']; - $redis = $register->get('redisPool')->get(); - $dbPool = $register->get('dbPool'); - $dbForProject = $dbPool->getDBFromPool($projectId, $redis); + $projectId = $realtime->connections[$connection]['projectId']; + + /** Get the console DB */ + $database = $dbPool->getConsoleDB(); + $pdo = $dbPool->getDBFromPool($database); + $cache = new Cache(new RedisCache($redis)); + $database = new Database(new MariaDB($pdo->getConnection()), $cache); + $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); + $database->setNamespace("_console"); + + if ($projectId !== 'console') { + $project = Authorization::skip(fn() => $database->getDocument('projects', $projectId)); + $database = $project->getAttribute('database', ''); + $pdo = $dbPool->getDBFromPool($database); + $database = new Database(new MariaDB($pdo->getConnection()), $cache); + $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); + $database->setNamespace("_{$project->getInternalId()}"); + } /* * Abuse Check * * Abuse limits are sending 32 times per minute and connection. */ - $timeLimit = new TimeLimit('url:{url},connection:{connection}', 32, 60, $dbForProject); + $timeLimit = new TimeLimit('url:{url},connection:{connection}', 32, 60, $database); $timeLimit ->setParam('{connection}', $connection) @@ -496,7 +529,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re Auth::$unique = $session['id'] ?? ''; Auth::$secret = $session['secret'] ?? ''; - $user = $dbForProject->getDocument('users', Auth::$unique); + $user = $database->getDocument('users', Auth::$unique); if ( empty($user->getId()) // Check a document has been found in the DB @@ -541,7 +574,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re $server->close($connection, $th->getCode()); } } finally { - call_user_func($returnProjectDB); + $dbPool->reset(); $register->get('redisPool')->put($redis); } }); diff --git a/composer.lock b/composer.lock index 454e14ea0d..64c2d40a16 100644 --- a/composer.lock +++ b/composer.lock @@ -1894,16 +1894,16 @@ }, { "name": "utopia-php/cache", - "version": "0.6.0", + "version": "0.6.1", "source": { "type": "git", "url": "https://github.com/utopia-php/cache.git", - "reference": "8ea1353a4bbab617e23c865a7c97b60d8074aee3" + "reference": "9889235a6d3da6cbb1f435201529da4d27c30e79" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/cache/zipball/8ea1353a4bbab617e23c865a7c97b60d8074aee3", - "reference": "8ea1353a4bbab617e23c865a7c97b60d8074aee3", + "url": "https://api.github.com/repos/utopia-php/cache/zipball/9889235a6d3da6cbb1f435201529da4d27c30e79", + "reference": "9889235a6d3da6cbb1f435201529da4d27c30e79", "shasum": "" }, "require": { @@ -1941,9 +1941,9 @@ ], "support": { "issues": "https://github.com/utopia-php/cache/issues", - "source": "https://github.com/utopia-php/cache/tree/0.6.0" + "source": "https://github.com/utopia-php/cache/tree/0.6.1" }, - "time": "2022-04-04T12:30:05+00:00" + "time": "2022-08-10T08:12:46+00:00" }, { "name": "utopia-php/cli", @@ -2051,16 +2051,16 @@ }, { "name": "utopia-php/database", - "version": "0.18.7", + "version": "0.18.9", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "d542ee433f1a545d926ffaf707bdf952dc18a52e" + "reference": "227b3ca919149b7b0d6556c8effe9ee46ed081e6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/d542ee433f1a545d926ffaf707bdf952dc18a52e", - "reference": "d542ee433f1a545d926ffaf707bdf952dc18a52e", + "url": "https://api.github.com/repos/utopia-php/database/zipball/227b3ca919149b7b0d6556c8effe9ee46ed081e6", + "reference": "227b3ca919149b7b0d6556c8effe9ee46ed081e6", "shasum": "" }, "require": { @@ -2109,9 +2109,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.18.7" + "source": "https://github.com/utopia-php/database/tree/0.18.9" }, - "time": "2022-07-11T10:20:33+00:00" + "time": "2022-07-19T09:42:53+00:00" }, { "name": "utopia-php/domains", @@ -2948,16 +2948,16 @@ }, { "name": "matthiasmullie/minify", - "version": "1.3.68", + "version": "1.3.69", "source": { "type": "git", "url": "https://github.com/matthiasmullie/minify.git", - "reference": "c00fb02f71b2ef0a5f53fe18c5a8b9aa30f48297" + "reference": "a61c949cccd086808063611ef9698eabe42ef22f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/matthiasmullie/minify/zipball/c00fb02f71b2ef0a5f53fe18c5a8b9aa30f48297", - "reference": "c00fb02f71b2ef0a5f53fe18c5a8b9aa30f48297", + "url": "https://api.github.com/repos/matthiasmullie/minify/zipball/a61c949cccd086808063611ef9698eabe42ef22f", + "reference": "a61c949cccd086808063611ef9698eabe42ef22f", "shasum": "" }, "require": { @@ -3006,7 +3006,7 @@ ], "support": { "issues": "https://github.com/matthiasmullie/minify/issues", - "source": "https://github.com/matthiasmullie/minify/tree/1.3.68" + "source": "https://github.com/matthiasmullie/minify/tree/1.3.69" }, "funding": [ { @@ -3014,7 +3014,7 @@ "type": "github" } ], - "time": "2022-04-19T08:28:56+00:00" + "time": "2022-08-01T09:00:18+00:00" }, { "name": "matthiasmullie/path-converter", @@ -3524,23 +3524,23 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.15", + "version": "9.2.16", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "2e9da11878c4202f97915c1cb4bb1ca318a63f5f" + "reference": "2593003befdcc10db5e213f9f28814f5aa8ac073" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2e9da11878c4202f97915c1cb4bb1ca318a63f5f", - "reference": "2e9da11878c4202f97915c1cb4bb1ca318a63f5f", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2593003befdcc10db5e213f9f28814f5aa8ac073", + "reference": "2593003befdcc10db5e213f9f28814f5aa8ac073", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.13.0", + "nikic/php-parser": "^4.14", "php": ">=7.3", "phpunit/php-file-iterator": "^3.0.3", "phpunit/php-text-template": "^2.0.2", @@ -3589,7 +3589,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.15" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.16" }, "funding": [ { @@ -3597,7 +3597,7 @@ "type": "github" } ], - "time": "2022-03-07T09:28:20+00:00" + "time": "2022-08-20T05:26:47+00:00" }, { "name": "phpunit/php-file-iterator", @@ -5271,16 +5271,16 @@ }, { "name": "twig/twig", - "version": "v3.4.1", + "version": "v3.4.2", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "e939eae92386b69b49cfa4599dd9bead6bf4a342" + "reference": "e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/e939eae92386b69b49cfa4599dd9bead6bf4a342", - "reference": "e939eae92386b69b49cfa4599dd9bead6bf4a342", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077", + "reference": "e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077", "shasum": "" }, "require": { @@ -5331,7 +5331,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.4.1" + "source": "https://github.com/twigphp/Twig/tree/v3.4.2" }, "funding": [ { @@ -5343,7 +5343,7 @@ "type": "tidelift" } ], - "time": "2022-05-17T05:48:52+00:00" + "time": "2022-08-12T06:47:24+00:00" } ], "aliases": [], diff --git a/src/Appwrite/Resque/Worker.php b/src/Appwrite/Resque/Worker.php index b3d93c8f4b..a7930b1cb5 100644 --- a/src/Appwrite/Resque/Worker.php +++ b/src/Appwrite/Resque/Worker.php @@ -177,8 +177,8 @@ abstract class Worker $cache = $register->get('cache'); $dbPool = $register->get('dbPool'); + $dbForProject = $dbPool->getDB($database, $cache); - $namespace = "_$internalId"; $dbForProject->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $dbForProject->setNamespace($namespace); @@ -201,7 +201,6 @@ abstract class Worker } $dbForConsole = $dbPool->getDB($database, $cache); - $namespace = "_console"; $dbForConsole->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $dbForConsole->setNamespace($namespace);