Sync with dbpool changes
This commit is contained in:
parent
14909a04f2
commit
0152e3b69c
120
app/http.php
120
app/http.php
|
@ -52,55 +52,83 @@ include __DIR__ . '/controllers/general.php';
|
|||
|
||||
$http->on('start', function (Server $http) use ($payloadSize, $register) {
|
||||
$app = new App('UTC');
|
||||
$dbForConsole = $app->getResource('dbForConsole'); /** @var Utopia\Database\Database $dbForConsole */
|
||||
|
||||
if(!$dbForConsole->exists()) {
|
||||
Console::success('[Setup] - Server database init started...');
|
||||
|
||||
$collections = Config::getParam('collections2', []); /** @var array $collections */
|
||||
|
||||
$register->get('cache')->flushAll();
|
||||
|
||||
$dbForConsole->create();
|
||||
|
||||
|
||||
$audit = new Audit($dbForConsole);
|
||||
$audit->setup();
|
||||
|
||||
$adapter = new TimeLimit("", 0, 1, $dbForConsole);
|
||||
$adapter->setup();
|
||||
|
||||
foreach ($collections as $key => $collection) {
|
||||
$attributes = [];
|
||||
$indexes = [];
|
||||
|
||||
foreach ($collection['attributes'] as $attribute) {
|
||||
$attributes[] = new Document([
|
||||
'$id' => $attribute['$id'],
|
||||
'type' => $attribute['type'],
|
||||
'size' => $attribute['size'],
|
||||
'required' => $attribute['required'],
|
||||
'signed' => $attribute['signed'],
|
||||
'array' => $attribute['array'],
|
||||
'filters' => $attribute['filters'],
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($collection['indexes'] as $index) {
|
||||
$indexes[] = new Document([
|
||||
'$id' => $index['$id'],
|
||||
'type' => $index['type'],
|
||||
'attributes' => $index['attributes'],
|
||||
'lengths' => $index['lengths'],
|
||||
'orders' => $index['orders'],
|
||||
]);
|
||||
}
|
||||
|
||||
$dbForConsole->createCollection($key, $attributes, $indexes);
|
||||
go(function() use ($register, $app) {
|
||||
// Only retry connection once before throwing exception
|
||||
try {
|
||||
$db = $register->get('dbPool')->get();
|
||||
} catch (\Exception $exception) {
|
||||
Console::warning('[Setup] - Database not ready. Waiting for five seconds...');
|
||||
sleep(5);
|
||||
}
|
||||
|
||||
Console::success('[Setup] - Server database init completed...');
|
||||
}
|
||||
$db = $register->get('dbPool')->get();
|
||||
$redis = $register->get('redisPool')->get();
|
||||
|
||||
App::setResource('db', function () use (&$db) {
|
||||
return $db;
|
||||
});
|
||||
|
||||
App::setResource('cache', function () use (&$redis) {
|
||||
return $redis;
|
||||
});
|
||||
|
||||
App::setResource('app', function() use (&$app) {
|
||||
return $app;
|
||||
});
|
||||
|
||||
$dbForConsole = $app->getResource('dbForConsole'); /** @var Utopia\Database\Database $dbForConsole */
|
||||
|
||||
if(!$dbForConsole->exists()) {
|
||||
Console::success('[Setup] - Server database init started...');
|
||||
|
||||
$collections = Config::getParam('collections2', []); /** @var array $collections */
|
||||
|
||||
$redis->flushAll();
|
||||
|
||||
$dbForConsole->create();
|
||||
|
||||
$audit = new Audit($dbForConsole);
|
||||
$audit->setup();
|
||||
|
||||
$adapter = new TimeLimit("", 0, 1, $dbForConsole);
|
||||
$adapter->setup();
|
||||
|
||||
foreach ($collections as $key => $collection) {
|
||||
Console::success('[Setup] - Creating collection: ' . $collection['$id'] . '...');
|
||||
|
||||
$attributes = [];
|
||||
$indexes = [];
|
||||
|
||||
foreach ($collection['attributes'] as $attribute) {
|
||||
$attributes[] = new Document([
|
||||
'$id' => $attribute['$id'],
|
||||
'type' => $attribute['type'],
|
||||
'size' => $attribute['size'],
|
||||
'required' => $attribute['required'],
|
||||
'signed' => $attribute['signed'],
|
||||
'array' => $attribute['array'],
|
||||
'filters' => $attribute['filters'],
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($collection['indexes'] as $index) {
|
||||
$indexes[] = new Document([
|
||||
'$id' => $index['$id'],
|
||||
'type' => $index['type'],
|
||||
'attributes' => $index['attributes'],
|
||||
'lengths' => $index['lengths'],
|
||||
'orders' => $index['orders'],
|
||||
]);
|
||||
}
|
||||
|
||||
$dbForConsole->createCollection($key, $attributes, $indexes);
|
||||
|
||||
}
|
||||
|
||||
Console::success('[Setup] - Server database init completed...');
|
||||
}
|
||||
});
|
||||
|
||||
Console::success('Server started succefully (max payload is '.number_format($payloadSize).' bytes)');
|
||||
|
||||
|
|
24
app/init.php
24
app/init.php
|
@ -591,32 +591,32 @@ App::setResource('projectDB', function($db, $cache, $project) {
|
|||
return $projectDB;
|
||||
}, ['db', 'cache', 'project']);
|
||||
|
||||
App::setResource('dbForInternal', function($register, $project) {
|
||||
$cache = new Cache(new RedisCache($register->get('cache')));
|
||||
App::setResource('dbForInternal', function($db, $cache, $project) {
|
||||
$cache = new Cache(new RedisCache($cache));
|
||||
|
||||
$database = new Database2(new MariaDB($register->get('db')), $cache);
|
||||
$database = new Database2(new MariaDB($db), $cache);
|
||||
$database->setNamespace('project_'.$project->getId().'_internal');
|
||||
|
||||
return $database;
|
||||
}, ['register', 'project']);
|
||||
}, ['db', 'cache', 'project']);
|
||||
|
||||
App::setResource('dbForExternal', function($register, $project) {
|
||||
$cache = new Cache(new RedisCache($register->get('cache')));
|
||||
App::setResource('dbForExternal', function($db, $cache, $project) {
|
||||
$cache = new Cache(new RedisCache($cache));
|
||||
|
||||
$database = new Database2(new MariaDB($register->get('db')), $cache);
|
||||
$database = new Database2(new MariaDB($db), $cache);
|
||||
$database->setNamespace('project_'.$project->getId().'_external');
|
||||
|
||||
return $database;
|
||||
}, ['register', 'project']);
|
||||
}, ['db', 'cache', 'project']);
|
||||
|
||||
App::setResource('dbForConsole', function($register) {
|
||||
$cache = new Cache(new RedisCache($register->get('cache')));
|
||||
App::setResource('dbForConsole', function($db, $cache) {
|
||||
$cache = new Cache(new RedisCache($cache));
|
||||
|
||||
$database = new Database2(new MariaDB($register->get('db')), $cache);
|
||||
$database = new Database2(new MariaDB($db), $cache);
|
||||
$database->setNamespace('project_console_internal');
|
||||
|
||||
return $database;
|
||||
}, ['register']);
|
||||
}, ['db', 'cache']);
|
||||
|
||||
App::setResource('mode', function($request) {
|
||||
/** @var Utopia\Swoole\Request $request */
|
||||
|
|
Loading…
Reference in a new issue