refactor: global to registry

This commit is contained in:
Binyamin Yawitz
2024-09-05 13:20:44 -04:00
parent 5d70884e6c
commit fbb23b227b
6 changed files with 32 additions and 32 deletions
+4 -4
View File
@@ -18,12 +18,12 @@ use Utopia\Queue\Connection;
use Utopia\Registry\Registry;
use Utopia\System\System;
global $global, $container;
global $registry, $container;
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
/**
* @var Registry $global
* @var Registry $registry
* @var Container $container
*/
$context = new Dependency();
@@ -39,8 +39,8 @@ $context
$register
->setName('register')
->setCallback(function () use (&$global): Registry {
return $global;
->setCallback(function () use (&$registry): Registry {
return $registry;
});
$queueForFunctions
+1 -1
View File
@@ -23,7 +23,7 @@ use Utopia\Http\Adapter\Swoole\Server;
use Utopia\Http\Http;
use Utopia\System\System;
global $global, $container;
global $registry, $container;
$payloadSize = 12 * (1024 * 1024); // 12MB - adding slight buffer for headers and other data that might be sent with the payload - update later with valid testing
$workerNumber = swoole_cpu_num() * intval(System::getEnv('_APP_WORKER_PER_CORE', 6));
+13 -13
View File
@@ -91,7 +91,7 @@ ini_set('display_startup_errors', 1);
ini_set('default_socket_timeout', -1);
error_reporting(E_ALL);
global $http, $container, $global;
global $http, $container, $registry;
Http::setMode(System::getEnv('_APP_ENV', Http::MODE_TYPE_PRODUCTION));
@@ -185,9 +185,9 @@ function getDevice($root): Device
}
$container = new Container();
$global = new Registry();
$registry = new Registry();
$global->set('logger', function () {
$registry->set('logger', function () {
// Register error logger
$providerName = System::getEnv('_APP_LOGGING_PROVIDER', '');
$providerConfig = System::getEnv('_APP_LOGGING_CONFIG', '');
@@ -243,18 +243,18 @@ $global->set('logger', function () {
return $logger;
});
$global->set('geodb', function () {
$registry->set('geodb', function () {
/**
* @disregard P1009 Undefined type
*/
return new Reader(__DIR__ . '/assets/dbip/dbip-country-lite-2024-09.mmdb');
});
$global->set('hooks', function () {
$registry->set('hooks', function () {
return new Hooks();
});
$global->set(
$registry->set(
'pools',
(function () {
$fallbackForDB = 'db_main=' . URL::unparse([
@@ -398,7 +398,7 @@ $global->set(
})()
);
$global->set('smtp', function () {
$registry->set('smtp', function () {
$mail = new PHPMailer(true);
$mail->isSMTP();
@@ -427,11 +427,11 @@ $global->set('smtp', function () {
return $mail;
});
$global->set('promiseAdapter', function () {
$registry->set('promiseAdapter', function () {
return new Swoole();
});
$global->set('db', function () {
$registry->set('db', function () {
// This is usually for our workers or CLI commands scope
$dbHost = System::getEnv('_APP_DB_HOST', '');
$dbPort = System::getEnv('_APP_DB_PORT', '');
@@ -885,8 +885,8 @@ $authentication
$registry
->setName('registry')
->setCallback(function () use (&$global): Registry {
return $global;
->setCallback(function () use (&$registry): Registry {
return $registry;
});
$pools
@@ -1229,8 +1229,8 @@ $getProjectDB
$promiseAdapter
->setName('promiseAdapter')
->setCallback(function () use ($global) {
return $global->get('promiseAdapter');
->setCallback(function () use ($registry) {
return $registry->get('promiseAdapter');
});
$schemaVariable
+10 -10
View File
@@ -36,10 +36,10 @@ use Utopia\WebSocket\Adapter;
use Utopia\WebSocket\Server;
/**
* @var Registry $global
* @var Registry $registry
* @var Container $container
*/
global $global, $container;
global $registry, $container;
require_once __DIR__ . '/init.php';
@@ -70,8 +70,8 @@ $adapter
$server = new Server($adapter);
$logError = function (Throwable $error, string $action) use ($global) {
$logger = $global->get('logger');
$logError = function (Throwable $error, string $action) use ($registry) {
$logger = $registry->get('logger');
if ($logger && !$error instanceof Exception) {
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');
@@ -138,7 +138,7 @@ $server->onStart(function () use ($stats, $container, $containerId, &$statsDocum
sleep(DATABASE_RECONNECT_SLEEP);
}
} while (true);
// TODO NOW $global->get('pools')->reclaim();
// TODO NOW $registry->get('pools')->reclaim();
});
/**
@@ -166,7 +166,7 @@ $server->onStart(function () use ($stats, $container, $containerId, &$statsDocum
} catch (Throwable $th) {
call_user_func($logError, $th, "updateWorkerDocument");
} finally {
// TODO NOW $global->get('pools')->reclaim();
// TODO NOW $registry->get('pools')->reclaim();
}
});
}
@@ -231,7 +231,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $container, $stats
'data' => $event['data']
]));
}
// TODO NOW $global->get('pools')->reclaim();
// TODO NOW $registry->get('pools')->reclaim();
}
}
/**
@@ -311,7 +311,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $container, $stats
$realtime->unsubscribe($connection);
$realtime->subscribe($projectId, $connection, $roles, $channels);
//TODO NOW $global->get('pools')->reclaim();
//TODO NOW $registry->get('pools')->reclaim();
}
}
@@ -343,7 +343,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $container, $stats
sleep(DATABASE_RECONNECT_SLEEP);
continue;
} finally {
//$global->get('pools')->reclaim();
//$registry->get('pools')->reclaim();
// TODO eldad add connections reclaim
}
}
@@ -586,7 +586,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $co
$server->close($connection, $th->getCode());
}
} finally {
// TODO NOW $global->get('pools')->reclaim();
// TODO NOW $registry->get('pools')->reclaim();
}
});
+2 -2
View File
@@ -21,7 +21,7 @@ use Utopia\Queue\Worker;
use Utopia\Storage\Device\Local;
use Utopia\System\System;
global $global, $container;
global $registry, $container;
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
@@ -37,7 +37,7 @@ $deviceForLocalFiles = new Dependency();
$register
->setName('register')
->setCallback(fn () => $global);
->setCallback(fn () => $registry);
$project
->setName('project')
+2 -2
View File
@@ -67,8 +67,8 @@ class EventTest extends TestCase
$this->assertEquals('eventValue2', $this->object->getParam('eventKey2'));
$this->assertEquals(null, $this->object->getParam('eventKey3'));
global $global;
$pools = $global->get('pools');
global $registry;
$pools = $registry->get('pools');
$dsn = $pools['pools-queue-queue']['dsn'];
$queue = new Queue\Connection\Redis($dsn->getHost(), $dsn->getPort());