1
0
Fork 0
mirror of synced 2024-07-12 18:05:55 +12:00

feat: update worker class

This commit is contained in:
Christy Jacob 2022-08-24 22:14:40 +05:30
parent 47b206c445
commit 7844a74c1a
2 changed files with 19 additions and 35 deletions

View file

@ -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);

View file

@ -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;
}