1
0
Fork 0
mirror of synced 2024-07-04 06:00:53 +12:00

feat: merge and fix conflicts

This commit is contained in:
Christy Jacob 2022-07-15 13:54:27 +04:00
parent 6662fa7b7b
commit 42f0bddd60
5 changed files with 43 additions and 76 deletions

View file

@ -64,7 +64,10 @@ App::get('/v1/health/db')
$checkStart = \microtime(true); $checkStart = \microtime(true);
try { try {
$consoleDB = $utopia->getResource('consoleDB'); /* @var $db PDO */ $dbPool = $utopia->getResource('dbPool');
$name = $dbPool->getConsoleDB();
/* @var $consoleDB PDO */
$consoleDB = $dbPool->getPDO($name);
// Run a small test to check the connection // Run a small test to check the connection
$statement = $consoleDB->prepare("SELECT 1;"); $statement = $consoleDB->prepare("SELECT 1;");

View file

@ -93,63 +93,15 @@ $logError = function (Throwable $error, string $action) use ($register) {
$server->error($logError); $server->error($logError);
function getDatabase(Registry &$register, string $namespace) function getDatabase(Registry &$register, string $projectID)
{ {
$redis = $register->get('redisPool')->get(); $redis = $register->get('redisPool')->get();
$cache = new Cache(new RedisCache($redis)); [$database, $returnDatabase] = $register->get('dbPool')->getDBFromPool($projectID, $redis);
$consoleDB = $register->get('dbPool')->getConsoleDBFromPool();
$db = $consoleDB;
$dbName = '';
if ($namespace != '_console') {
$cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($consoleDB), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace('_console'); // Main DB
$project = $database->getDocument('projects', ltrim($namespace, '_'));
$dbName = $project->getAttribute('database', '');
if (!empty($dbName)) {
$projectDB = $register->get('dbPool')->getDBFromPool($dbName);
$db = $projectDB;
}
}
$attempts = 0;
do {
try {
$attempts++;
$database = new Database(new MariaDB($db), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace($namespace);
if (!$database->exists($database->getDefaultDatabase(), 'realtime')) {
throw new Exception('Collection not ready');
}
break; // leave loop if successful
} catch (\Throwable $e) {
Console::warning("Database not ready. Retrying connection ({$attempts})...");
if ($attempts >= DATABASE_RECONNECT_MAX_ATTEMPTS) {
throw new \Exception('Failed to connect to database: ' . $e->getMessage());
}
sleep(DATABASE_RECONNECT_SLEEP);
}
} while ($attempts < DATABASE_RECONNECT_MAX_ATTEMPTS);
return [ return [
$database, $database,
function () use ($register, $db, $dbName, $redis) { function () use ($register, $returnDatabase, $redis) {
if (empty($dbName)) { call_user_func($returnDatabase);
$register->get('dbPool')->putConsoleDb($db);
} else {
$register->get('dbPool')->put($db, $dbName);
}
$register->get('redisPool')->put($redis); $register->get('redisPool')->put($redis);
} }
]; ];
@ -164,7 +116,7 @@ $server->onStart(function () use ($stats, $register, $containerId, &$statsDocume
*/ */
go(function () use ($register, $containerId, &$statsDocument) { go(function () use ($register, $containerId, &$statsDocument) {
$attempts = 0; $attempts = 0;
[$database, $returnDatabase] = getDatabase($register, '_console'); [$database, $returnDatabase] = getDatabase($register, 'console');
do { do {
try { try {
$attempts++; $attempts++;
@ -201,7 +153,7 @@ $server->onStart(function () use ($stats, $register, $containerId, &$statsDocume
} }
try { try {
[$database, $returnDatabase] = getDatabase($register, '_console'); [$database, $returnDatabase] = getDatabase($register, 'console');
$statsDocument $statsDocument
->setAttribute('timestamp', time()) ->setAttribute('timestamp', time())
@ -227,7 +179,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
* Sending current connections to project channels on the console project every 5 seconds. * Sending current connections to project channels on the console project every 5 seconds.
*/ */
if ($realtime->hasSubscriber('console', 'role:member', 'project')) { if ($realtime->hasSubscriber('console', 'role:member', 'project')) {
[$database, $returnDatabase] = getDatabase($register, '_console'); [$database, $returnDatabase] = getDatabase($register, 'console');
$payload = []; $payload = [];
@ -327,9 +279,9 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
if ($realtime->hasSubscriber($projectId, 'user:' . $userId)) { if ($realtime->hasSubscriber($projectId, 'user:' . $userId)) {
$connection = array_key_first(reset($realtime->subscriptions[$projectId]['user:' . $userId])); $connection = array_key_first(reset($realtime->subscriptions[$projectId]['user:' . $userId]));
[$consoleDatabase, $returnConsoleDatabase] = getDatabase($register, '_console'); [$consoleDatabase, $returnConsoleDatabase] = getDatabase($register, 'console');
$project = Authorization::skip(fn() => $consoleDatabase->getDocument('projects', $projectId)); $project = Authorization::skip(fn() => $consoleDatabase->getDocument('projects', $projectId));
[$database, $returnDatabase] = getDatabase($register, "_{$project->getInternalId()}"); [$database, $returnDatabase] = getDatabase($register, $project->getId());
$user = $database->getDocument('users', $userId); $user = $database->getDocument('users', $userId);

View file

@ -77,6 +77,8 @@ class DatabaseV1 extends Worker
* Fetch attribute from the database, since with Resque float values are loosing informations. * Fetch attribute from the database, since with Resque float values are loosing informations.
*/ */
$attribute = $dbForProject->getDocument('attributes', $attribute->getId()); $attribute = $dbForProject->getDocument('attributes', $attribute->getId());
var_dump($attribute);
var_dump($attribute->getId());
$collectionId = $collection->getId(); $collectionId = $collection->getId();
$key = $attribute->getAttribute('key', ''); $key = $attribute->getAttribute('key', '');
@ -97,6 +99,8 @@ class DatabaseV1 extends Worker
} }
$dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available')); $dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available'));
} catch (\Throwable $th) { } catch (\Throwable $th) {
var_dump($th->getTraceAsString());
var_dump($attribute->getArrayCopy());
Console::error($th->getMessage()); Console::error($th->getMessage());
$dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'failed')); $dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'failed'));
} finally { } finally {

View file

@ -318,6 +318,7 @@ services:
volumes: volumes:
- ./app:/usr/src/code/app - ./app:/usr/src/code/app
- ./src:/usr/src/code/src - ./src:/usr/src/code/src
- ./vendor/utopia-php/database:/usr/src/code/vendor/utopia-php/database
depends_on: depends_on:
- redis - redis
- mariadb - mariadb

View file

@ -72,6 +72,11 @@ class DatabasePool {
->withPassword($dsn->getPassword()) ->withPassword($dsn->getPassword())
->withOptions([ ->withOptions([
PDO::ATTR_ERRMODE => App::isDevelopment() ? PDO::ERRMODE_WARNING : PDO::ERRMODE_SILENT, // If in production mode, warnings are not displayed PDO::ATTR_ERRMODE => App::isDevelopment() ? PDO::ERRMODE_WARNING : PDO::ERRMODE_SILENT, // If in production mode, warnings are not displayed
PDO::ATTR_TIMEOUT => 3, // Seconds
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_STRINGIFY_FETCHES => true
]), ]),
64 64
); );
@ -86,7 +91,7 @@ class DatabasePool {
* @param string $name * @param string $name
* @return ?PDO * @return ?PDO
*/ */
private function getPDO(string $name): ?PDO public function getPDO(string $name): ?PDO
{ {
$dsn = $this->dsns[$name] ?? throw new Exception("Database with name : $name not found.", 500); $dsn = $this->dsns[$name] ?? throw new Exception("Database with name : $name not found.", 500);
@ -125,7 +130,8 @@ class DatabasePool {
$cache = new Cache(new RedisCache($redis)); $cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($pdo), $cache); $database = new Database(new MariaDB($pdo), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_console"); $namespace = "_project_console";
$database->setNamespace($namespace);
$project = Authorization::skip(fn() => $database->getDocument('projects', $projectID)); $project = Authorization::skip(fn() => $database->getDocument('projects', $projectID));
$internalID = $project->getInternalId(); $internalID = $project->getInternalId();
@ -141,7 +147,7 @@ class DatabasePool {
* *
* @return ?Database * @return ?Database
*/ */
public function getDB(string $projectID, \Redis $redis): ?Database public function getDB(string $projectID, ?\Redis $redis): ?Database
{ {
/** Get DB name from the console database */ /** Get DB name from the console database */
[$name, $internalID] = $this->getName($projectID, $redis); [$name, $internalID] = $this->getName($projectID, $redis);
@ -155,7 +161,8 @@ class DatabasePool {
$cache = new Cache(new RedisCache($redis)); $cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($pdo), $cache); $database = new Database(new MariaDB($pdo), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_{$internalID}"); $namespace = "_project_$internalID";
$database->setNamespace($namespace);
return $database; return $database;
} }
@ -179,7 +186,7 @@ class DatabasePool {
[$name, $internalID] = $this->getName($projectID, $redis); [$name, $internalID] = $this->getName($projectID, $redis);
$pool = $this->pools[$name] ?? throw new Exception("Database pool with name : $name not found. Check the value of _APP_PROJECT_DB in .env", 500); $pool = $this->pools[$name] ?? throw new Exception("Database pool with name : $name not found. Check the value of _APP_PROJECT_DB in .env", 500);
$namespace = "_$internalID"; $namespace = "_project_$internalID";
$attempts = 0; $attempts = 0;
do { do {
try { try {
@ -273,19 +280,19 @@ class DatabasePool {
// * Convenience methods for console DB // * Convenience methods for console DB
// */ // */
// /** /**
// * Function to get a single instace of the console DB * Function to get the name of the console DB
// * *
// * @return ?PDO * @return ?string
// */ */
// public function getConsoleDB(): ?PDO public function getConsoleDB(): ?string
// { {
// if (empty($this->consoleDB)) { if (empty($this->consoleDB)) {
// throw new Exception('Console DB is not defined', 500); throw new Exception('Console DB is not defined', 500);
// }; };
// return $this->getDB($this->consoleDB); return $this->consoleDB;
// } }
// /** // /**
// * Function to get an instance of the console DB from the database pool // * Function to get an instance of the console DB from the database pool