From 7844a74c1a22ac394b50aa7ade0ce2016a088a88 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 24 Aug 2022 22:14:40 +0530 Subject: [PATCH] feat: update worker class --- app/tasks/usage.php | 44 +++++++++------------------------- src/Appwrite/Resque/Worker.php | 10 ++++++-- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/app/tasks/usage.php b/app/tasks/usage.php index e7237ad22..28ddef08b 100644 --- a/app/tasks/usage.php +++ b/app/tasks/usage.php @@ -7,11 +7,7 @@ use Appwrite\Stats\Usage; use Appwrite\Stats\UsageDB; use InfluxDB\Database as InfluxDatabase; use Utopia\App; -use Utopia\Cache\Adapter\Redis as RedisCache; -use Utopia\Cache\Cache; use Utopia\CLI\Console; -use Utopia\Database\Adapter\MariaDB; -use Utopia\Database\Database; use Utopia\Database\Validator\Authorization; use Utopia\Registry\Registry; use Utopia\Logger\Log; @@ -19,34 +15,6 @@ use Utopia\Logger\Log; Authorization::disable(); Authorization::setDefaultStatus(false); -function getDatabase(Registry &$register): Database -{ - $attempts = 0; - $dbPool = $register->get('dbPool'); - $redis = $register->get('cache'); - $database = $dbPool->getConsoleDB(); - do { - try { - $attempts++; - $pdo = $dbPool->getPDO($database); - $database = DatabasePool::getDatabase($pdo, $redis, '_console'); - - if (!$database->exists($database->getDefaultDatabase(), 'projects')) { - throw new Exception('Projects collection not ready'); - } - break; // leave loop if successful - } catch (\Exception$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 $database; -} - function getInfluxDB(Registry &$register): InfluxDatabase { /** @var InfluxDB\Client $client */ @@ -115,7 +83,17 @@ $cli Console::success(APP_NAME . ' usage aggregation process v1 has started'); $interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '30'); // 30 seconds (by default) - $database = getDatabase($register); + + $redis = $register->get('cache'); + $dbPool = $register->get('dbPool'); + + $database = $dbPool->getConsoleDB(); + $pdo = $dbPool->getPDO($database); + $database = DatabasePool::wait( + DatabasePool::getDatabase($pdo, $redis, '_console'), + 'projects', + ); + $influxDB = getInfluxDB($register); $usage = new Usage($database, $influxDB, $logError); diff --git a/src/Appwrite/Resque/Worker.php b/src/Appwrite/Resque/Worker.php index e9ddc1f7c..9f42bfb09 100644 --- a/src/Appwrite/Resque/Worker.php +++ b/src/Appwrite/Resque/Worker.php @@ -180,7 +180,10 @@ abstract class Worker $dbPool = $register->get('dbPool'); $namespace = "_$internalId"; $pdo = $dbPool->getPDO($database); - $dbForProject = DatabasePool::getDatabase($pdo, $cache, $namespace); + $dbForProject = DatabasePool::wait( + DatabasePool::getDatabase($pdo, $cache, $namespace), + 'projects' + ); return $dbForProject; } @@ -201,7 +204,10 @@ abstract class Worker $namespace = "_console"; $pdo = $dbPool->getPDO($database); - $dbForConsole = DatabasePool::getDatabase($pdo, $cache, $namespace); + $dbForConsole = DatabasePool::wait( + DatabasePool::getDatabase($pdo, $cache, $namespace), + '_metadata' + ); return $dbForConsole; }