From bee844fcdb54d4f37d5259d0317d3b80d63111e5 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 15 Sep 2024 17:40:55 +0200 Subject: [PATCH 01/47] sync against 1.6x --- app/cli.php | 8 +- app/config/bla | 36 +++ app/console | 1 + app/controllers/api/functions.php | 5 +- app/controllers/api/projects.php | 17 +- app/controllers/shared/api.php | 4 +- app/init.php | 48 ++-- app/realtime.php | 16 +- app/worker.php | 41 +-- composer.lock | 235 ++++++++++++------ src/Appwrite/Event/Build.php | 1 + src/Appwrite/Event/Certificate.php | 1 + src/Appwrite/Event/Database.php | 1 + src/Appwrite/Event/Event.php | 7 + src/Appwrite/Event/Migration.php | 2 + src/Appwrite/Messaging/Adapter.php | 4 +- src/Appwrite/Messaging/Adapter/Realtime.php | 8 +- src/Appwrite/Platform/Workers/Builds.php | 24 +- .../Platform/Workers/Certificates.php | 46 ++-- src/Appwrite/Platform/Workers/Databases.php | 72 ++++-- src/Appwrite/Platform/Workers/Deletes.php | 10 +- src/Appwrite/Platform/Workers/Functions.php | 19 +- src/Appwrite/Platform/Workers/Migrations.php | 31 ++- 23 files changed, 448 insertions(+), 189 deletions(-) create mode 100644 app/config/bla create mode 160000 app/console diff --git a/app/cli.php b/app/cli.php index 9a022eaacc..0dec8bad04 100644 --- a/app/cli.php +++ b/app/cli.php @@ -109,8 +109,8 @@ CLI::setResource('getProjectDB', function (Group $pools, Database $dbForConsole, if (isset($databases[$dsn->getHost()])) { $database = $databases[$dsn->getHost()]; - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $database + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -133,8 +133,8 @@ CLI::setResource('getProjectDB', function (Group $pools, Database $dbForConsole, $databases[$dsn->getHost()] = $database; - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $database + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); diff --git a/app/config/bla b/app/config/bla new file mode 100644 index 0000000000..37b151a403 --- /dev/null +++ b/app/config/bla @@ -0,0 +1,36 @@ +version: '3.8' + +services: + db_15: + image: mysql:8.0.36-debian + container_name: mysql_db_15 + environment: + MYSQL_ROOT_PASSWORD: lJVYGNZTOTF4VsGSnz5CWoVp + MYSQL_DATABASE: appwrite + MYSQL_USER: appwrite_user + MYSQL_PASSWORD: Fy8i367HpJd8EB1VGos3vsf + volumes: + - /mnt/db-15:/var/lib/mysql + ports: + - "3307:3306" + networks: + - mysql_network + + db_16: + image: mysql:8.0.36-debian + container_name: mysql_db_16 + environment: + MYSQL_ROOT_PASSWORD: d1RNc7NsnsQ8I1OQd0eeLWr0 + MYSQL_DATABASE: appwrite + MYSQL_USER: appwrite_user + MYSQL_PASSWORD: KlQ4NMyl46YPumqSA8CUOLjO + volumes: + - /mnt/db-16:/var/lib/mysql + ports: + - "3308:3306" + networks: + - mysql_network + +networks: + mysql_network: + driver: bridge \ No newline at end of file diff --git a/app/console b/app/console new file mode 160000 index 0000000000..0959b594b3 --- /dev/null +++ b/app/console @@ -0,0 +1 @@ +Subproject commit 0959b594b32f176819d4afb3a769afea212db789 diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 7c6d89c7f0..6a5e12f4fd 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -183,7 +183,8 @@ App::post('/v1/functions') ->inject('queueForBuilds') ->inject('dbForConsole') ->inject('gitHub') - ->action(function (string $functionId, string $name, string $runtime, array $execute, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, string $entrypoint, string $commands, array $scopes, string $installationId, string $providerRepositoryId, string $providerBranch, bool $providerSilentMode, string $providerRootDirectory, string $templateRepository, string $templateOwner, string $templateRootDirectory, string $templateVersion, string $specification, Request $request, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Build $queueForBuilds, Database $dbForConsole, GitHub $github) use ($redeployVcs) { + ->inject('realtimeConnection') + ->action(function (string $functionId, string $name, string $runtime, array $execute, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, string $entrypoint, string $commands, array $scopes, string $installationId, string $providerRepositoryId, string $providerBranch, bool $providerSilentMode, string $providerRootDirectory, string $templateRepository, string $templateOwner, string $templateRootDirectory, string $templateVersion, string $specification, Request $request, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Build $queueForBuilds, Database $dbForConsole, GitHub $githubgithub, Callable $realtimeConnection) use ($redeployVcs) { $functionId = ($functionId == 'unique()') ? ID::unique() : $functionId; $allowList = \array_filter(\explode(',', System::getEnv('_APP_FUNCTIONS_RUNTIMES', ''))); @@ -374,6 +375,7 @@ App::post('/v1/functions') project: $project ); Realtime::send( + redis: $realtimeConnection($queueForEvents->getSourceRegion()), projectId: 'console', payload: $rule->getArrayCopy(), events: $allEvents, @@ -381,6 +383,7 @@ App::post('/v1/functions') roles: $target['roles'] ); Realtime::send( + redis: $realtimeConnection($queueForEvents->getSourceRegion()), projectId: $project->getId(), payload: $rule->getArrayCopy(), events: $allEvents, diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 3a8c232195..0c640255f8 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -125,6 +125,13 @@ App::post('/v1/projects') if ($index !== false) { $dsn = $databases[$index]; } else { + + if ($region !== 'default') { + $databases = array_filter($databases, function ($value) use ($region) { + return str_contains($value, $region); + }); + } + $dsn = $databases[array_rand($databases)]; } @@ -133,11 +140,13 @@ App::post('/v1/projects') } // TODO: Temporary until all projects are using shared tables. - if ($dsn === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn, $sharedTablesKeys)) { + $schema = 'appwrite'; $database = 'appwrite'; $namespace = System::getEnv('_APP_DATABASE_SHARED_NAMESPACE', ''); - $dsn = $schema . '://' . System::getEnv('_APP_DATABASE_SHARED_TABLES', '') . '?database=' . $database; + $dsn = $schema . '://' . $dsn . '?database=' . $database; if (!empty($namespace)) { $dsn .= '&namespace=' . $namespace; @@ -192,8 +201,8 @@ App::post('/v1/projects') $adapter = $pools->get($dsn->getHost())->pop()->getResource(); $dbForProject = new Database($adapter, $cache); - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $dbForProject + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $dbForProject ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 0cb88d31fa..792cb15138 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -590,7 +590,8 @@ App::shutdown() ->inject('queueForFunctions') ->inject('mode') ->inject('dbForConsole') - ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Audit $queueForAudits, Usage $queueForUsage, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Messaging $queueForMessaging, Database $dbForProject, Func $queueForFunctions, string $mode, Database $dbForConsole) use ($parseLabel) { + ->inject('realtimeConnection') + ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Audit $queueForAudits, Usage $queueForUsage, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Messaging $queueForMessaging, Database $dbForProject, Func $queueForFunctions, string $mode, Database $dbForConsole, callable $realtimeConnection) use ($parseLabel) { $responsePayload = $response->getPayload(); @@ -636,6 +637,7 @@ App::shutdown() ); Realtime::send( + redis: $realtimeConnection($queueForEvents->getSourceRegion()), projectId: $target['projectId'] ?? $project->getId(), payload: $queueForEvents->getRealtimePayload(), events: $allEvents, diff --git a/app/init.php b/app/init.php index 9540de57de..b6c73f9919 100644 --- a/app/init.php +++ b/app/init.php @@ -89,6 +89,10 @@ use Utopia\Validator\Range; use Utopia\Validator\URL; use Utopia\Validator\WhiteList; use Utopia\VCS\Adapter\Git\GitHub as VcsGitHub; +use Appwrite\Messaging\Adapter\Realtime; +use Appwrite\Utopia\Request; + + const APP_NAME = 'Appwrite'; const APP_DOMAIN = 'appwrite.io'; @@ -1390,8 +1394,8 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForConsole, $dsn = new DSN('mysql://' . $project->getAttribute('database')); } - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $database + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -1443,8 +1447,8 @@ App::setResource('getProjectDB', function (Group $pools, Database $dbForConsole, ->setMetadata('project', $project->getId()) ->setTimeout(APP_DATABASE_TIMEOUT_MILLISECONDS); - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $database + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -1494,23 +1498,33 @@ App::setResource('deviceForLocal', function () { return new Local(); }); -App::setResource('deviceForFiles', function ($project) { - return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); -}, ['project']); +App::setResource('deviceForFiles', function ($project, $connectionString) { + return getDevice(APP_STORAGE_UPLOADS.'/app-'.$project->getId(), $connectionString); +}, ['project', 'connectionString']); -App::setResource('deviceForFunctions', function ($project) { - return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); -}, ['project']); +App::setResource('deviceForFunctions', function ($project, $connectionString) { + return getDevice(APP_STORAGE_FUNCTIONS.'/app-'.$project->getId(), $connectionString); +}, ['project', 'connectionString']); -App::setResource('deviceForBuilds', function ($project) { - return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); -}, ['project']); +App::setResource('deviceForBuilds', function ($project, $connectionString) { + return getDevice(APP_STORAGE_BUILDS.'/app-'.$project->getId(), $connectionString); +}, ['project', 'connectionString']); -function getDevice($root): Device +App::setResource('connectionString', function () { + return System::getEnv('_APP_CONNECTIONS_STORAGE', ''); +}); + +App::setResource('realtimeConnection',function ($pools) { + return function () use ($pools) { + return $pools->get('pubsub')->pop()->getResource(); + }; +}, ['pools']); + + +function getDevice(string $root, string $connectionString = ''): Device { - $connection = System::getEnv('_APP_CONNECTIONS_STORAGE', ''); - if (!empty($connection)) { + if (! empty($connectionString)) { $acl = 'private'; $device = Storage::DEVICE_LOCAL; $accessKey = ''; @@ -1519,7 +1533,7 @@ function getDevice($root): Device $region = ''; try { - $dsn = new DSN($connection); + $dsn = new DSN($connectionString); $device = $dsn->getScheme(); $accessKey = $dsn->getUser() ?? ''; $accessSecret = $dsn->getPassword() ?? ''; diff --git a/app/realtime.php b/app/realtime.php index b8fdb2cf21..9499705e4d 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -92,8 +92,8 @@ if (!function_exists("getProjectDB")) { $database = new Database($adapter, getCache()); - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $database + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -135,6 +135,16 @@ if (!function_exists("getCache")) { } } +if (!function_exists("getPubSub")) { + function getPubSub(): \Redis + { + global $register; + + $pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */ + return $pools->get('pubsub')->pop()->getResource(); + } +} + $realtime = new Realtime(); /** @@ -354,7 +364,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats, } $start = time(); - $redis = $register->get('pools')->get('pubsub')->pop()->getResource(); /** @var Redis $redis */ + $redis = getPubSub(); /** @var \Redis $redis */ $redis->setOption(Redis::OPT_READ_TIMEOUT, -1); if ($redis->ping(true)) { diff --git a/app/worker.php b/app/worker.php index 9bcdae78e6..7bd9b23832 100644 --- a/app/worker.php +++ b/app/worker.php @@ -93,8 +93,8 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register, $dsn = new DSN('mysql://' . $project->getAttribute('database')); } - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $database + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -126,8 +126,8 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso if (isset($databases[$dsn->getHost()])) { $database = $databases[$dsn->getHost()]; - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $database + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -150,8 +150,8 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso $databases[$dsn->getHost()] = $database; - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { - $database + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -256,22 +256,27 @@ Server::setResource('pools', function (Registry $register) { return $register->get('pools'); }, ['register']); -Server::setResource('deviceForFunctions', function (Document $project) { - return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); -}, ['project']); +Server::setResource('deviceForFunctions', function (Document $project, $connectionString) { + return getDevice(APP_STORAGE_FUNCTIONS.'/app-'.$project->getId(), $connectionString); +}, ['project', 'connectionString']); -Server::setResource('deviceForFiles', function (Document $project) { - return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); -}, ['project']); +Server::setResource('deviceForFiles', function (Document $project, $connectionString) { + return getDevice(APP_STORAGE_UPLOADS.'/app-'.$project->getId(), $connectionString); +}, ['project', 'connectionString']); -Server::setResource('deviceForBuilds', function (Document $project) { - return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); -}, ['project']); +Server::setResource('deviceForBuilds', function (Document $project, $connectionString) { + return getDevice(APP_STORAGE_BUILDS.'/app-'.$project->getId(), $connectionString); +}, ['project', 'connectionString']); -Server::setResource('deviceForCache', function (Document $project) { - return getDevice(APP_STORAGE_CACHE . '/app-' . $project->getId()); -}, ['project']); +Server::setResource('deviceForCache', function (Document $project, $connectionString) { + return getDevice(APP_STORAGE_CACHE.'/app-'.$project->getId(), $connectionString); +}, ['project', 'connectionString']); +Server::setResource('realtimeConnection',function ($pools) { + return function () use ($pools) { + return $pools->get('pubsub')->pop()->getResource(); + }; +}, ['pools']); $pools = $register->get('pools'); $platform = new Appwrite(); diff --git a/composer.lock b/composer.lock index aac1554dbf..d717482314 100644 --- a/composer.lock +++ b/composer.lock @@ -1130,20 +1130,20 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.30.0", + "version": "v1.31.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "fd22ab50000ef01661e2a31d850ebaa297f8e03c" + "reference": "85181ba99b2345b0ef10ce42ecac37612d9fd341" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/fd22ab50000ef01661e2a31d850ebaa297f8e03c", - "reference": "fd22ab50000ef01661e2a31d850ebaa297f8e03c", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/85181ba99b2345b0ef10ce42ecac37612d9fd341", + "reference": "85181ba99b2345b0ef10ce42ecac37612d9fd341", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "provide": { "ext-mbstring": "*" @@ -1190,7 +1190,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.30.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.31.0" }, "funding": [ { @@ -1206,24 +1206,24 @@ "type": "tidelift" } ], - "time": "2024-06-19T12:30:46+00:00" + "time": "2024-09-09T11:45:10+00:00" }, { "name": "symfony/polyfill-php80", - "version": "v1.30.0", + "version": "v1.31.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "77fa7995ac1b21ab60769b7323d600a991a90433" + "reference": "60328e362d4c2c802a54fcbf04f9d3fb892b4cf8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/77fa7995ac1b21ab60769b7323d600a991a90433", - "reference": "77fa7995ac1b21ab60769b7323d600a991a90433", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/60328e362d4c2c802a54fcbf04f9d3fb892b4cf8", + "reference": "60328e362d4c2c802a54fcbf04f9d3fb892b4cf8", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "type": "library", "extra": { @@ -1270,7 +1270,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.30.0" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.31.0" }, "funding": [ { @@ -1286,7 +1286,7 @@ "type": "tidelift" } ], - "time": "2024-05-31T15:07:36+00:00" + "time": "2024-09-09T11:45:10+00:00" }, { "name": "thecodingmachine/safe", @@ -2993,16 +2993,16 @@ "packages-dev": [ { "name": "appwrite/sdk-generator", - "version": "0.39.19", + "version": "0.39.21", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator.git", - "reference": "d5653a2f744d2c297d44f99ff68bfc26c1a3b804" + "reference": "9754b190d33aaad56fdb8defc94f90248184c5ac" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/d5653a2f744d2c297d44f99ff68bfc26c1a3b804", - "reference": "d5653a2f744d2c297d44f99ff68bfc26c1a3b804", + "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/9754b190d33aaad56fdb8defc94f90248184c5ac", + "reference": "9754b190d33aaad56fdb8defc94f90248184c5ac", "shasum": "" }, "require": { @@ -3011,12 +3011,12 @@ "ext-mbstring": "*", "matthiasmullie/minify": "1.3.*", "php": ">=8.0", - "twig/twig": "v3.8.*" + "twig/twig": "3.14.*" }, "require-dev": { - "brianium/paratest": "v7.4.*", - "phpunit/phpunit": "10.5.*", - "squizlabs/php_codesniffer": "3.9.*" + "brianium/paratest": "7.*", + "phpunit/phpunit": "11.*", + "squizlabs/php_codesniffer": "3.*" }, "type": "library", "autoload": { @@ -3038,22 +3038,22 @@ "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", "support": { "issues": "https://github.com/appwrite/sdk-generator/issues", - "source": "https://github.com/appwrite/sdk-generator/tree/0.39.19" + "source": "https://github.com/appwrite/sdk-generator/tree/0.39.21" }, - "time": "2024-08-30T12:04:18+00:00" + "time": "2024-09-10T08:49:29+00:00" }, { "name": "doctrine/annotations", - "version": "2.0.1", + "version": "2.0.2", "source": { "type": "git", "url": "https://github.com/doctrine/annotations.git", - "reference": "e157ef3f3124bbf6fe7ce0ffd109e8a8ef284e7f" + "reference": "901c2ee5d26eb64ff43c47976e114bf00843acf7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/annotations/zipball/e157ef3f3124bbf6fe7ce0ffd109e8a8ef284e7f", - "reference": "e157ef3f3124bbf6fe7ce0ffd109e8a8ef284e7f", + "url": "https://api.github.com/repos/doctrine/annotations/zipball/901c2ee5d26eb64ff43c47976e114bf00843acf7", + "reference": "901c2ee5d26eb64ff43c47976e114bf00843acf7", "shasum": "" }, "require": { @@ -3065,10 +3065,10 @@ "require-dev": { "doctrine/cache": "^2.0", "doctrine/coding-standard": "^10", - "phpstan/phpstan": "^1.8.0", + "phpstan/phpstan": "^1.10.28", "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", - "symfony/cache": "^5.4 || ^6", - "vimeo/psalm": "^4.10" + "symfony/cache": "^5.4 || ^6.4 || ^7", + "vimeo/psalm": "^4.30 || ^5.14" }, "suggest": { "php": "PHP 8.0 or higher comes with attributes, a native replacement for annotations" @@ -3114,9 +3114,9 @@ ], "support": { "issues": "https://github.com/doctrine/annotations/issues", - "source": "https://github.com/doctrine/annotations/tree/2.0.1" + "source": "https://github.com/doctrine/annotations/tree/2.0.2" }, - "time": "2023-02-02T22:02:53+00:00" + "time": "2024-09-05T10:17:24+00:00" }, { "name": "doctrine/deprecations", @@ -4185,16 +4185,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.30.0", + "version": "1.30.1", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "5ceb0e384997db59f38774bf79c2a6134252c08f" + "reference": "51b95ec8670af41009e2b2b56873bad96682413e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/5ceb0e384997db59f38774bf79c2a6134252c08f", - "reference": "5ceb0e384997db59f38774bf79c2a6134252c08f", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/51b95ec8670af41009e2b2b56873bad96682413e", + "reference": "51b95ec8670af41009e2b2b56873bad96682413e", "shasum": "" }, "require": { @@ -4226,9 +4226,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.30.0" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.30.1" }, - "time": "2024-08-29T09:54:52+00:00" + "time": "2024-09-07T20:13:05+00:00" }, { "name": "phpunit/php-code-coverage", @@ -4756,16 +4756,16 @@ }, { "name": "psr/log", - "version": "3.0.1", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", - "reference": "79dff0b268932c640297f5208d6298f71855c03e" + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/79dff0b268932c640297f5208d6298f71855c03e", - "reference": "79dff0b268932c640297f5208d6298f71855c03e", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", "shasum": "" }, "require": { @@ -4800,9 +4800,9 @@ "psr-3" ], "support": { - "source": "https://github.com/php-fig/log/tree/3.0.1" + "source": "https://github.com/php-fig/log/tree/3.0.2" }, - "time": "2024-08-21T13:31:24+00:00" + "time": "2024-09-11T13:17:53+00:00" }, { "name": "sebastian/cli-parser", @@ -6222,20 +6222,20 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.30.0", + "version": "v1.31.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "0424dff1c58f028c451efff2045f5d92410bd540" + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/0424dff1c58f028c451efff2045f5d92410bd540", - "reference": "0424dff1c58f028c451efff2045f5d92410bd540", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638", + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "provide": { "ext-ctype": "*" @@ -6281,7 +6281,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.30.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.31.0" }, "funding": [ { @@ -6297,24 +6297,24 @@ "type": "tidelift" } ], - "time": "2024-05-31T15:07:36+00:00" + "time": "2024-09-09T11:45:10+00:00" }, { "name": "symfony/polyfill-intl-grapheme", - "version": "v1.30.0", + "version": "v1.31.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-grapheme.git", - "reference": "64647a7c30b2283f5d49b874d84a18fc22054b7a" + "reference": "b9123926e3b7bc2f98c02ad54f6a4b02b91a8abe" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/64647a7c30b2283f5d49b874d84a18fc22054b7a", - "reference": "64647a7c30b2283f5d49b874d84a18fc22054b7a", + "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/b9123926e3b7bc2f98c02ad54f6a4b02b91a8abe", + "reference": "b9123926e3b7bc2f98c02ad54f6a4b02b91a8abe", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "suggest": { "ext-intl": "For best performance" @@ -6359,7 +6359,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.30.0" + "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.31.0" }, "funding": [ { @@ -6375,24 +6375,24 @@ "type": "tidelift" } ], - "time": "2024-05-31T15:07:36+00:00" + "time": "2024-09-09T11:45:10+00:00" }, { "name": "symfony/polyfill-intl-normalizer", - "version": "v1.30.0", + "version": "v1.31.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-normalizer.git", - "reference": "a95281b0be0d9ab48050ebd988b967875cdb9fdb" + "reference": "3833d7255cc303546435cb650316bff708a1c75c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/a95281b0be0d9ab48050ebd988b967875cdb9fdb", - "reference": "a95281b0be0d9ab48050ebd988b967875cdb9fdb", + "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/3833d7255cc303546435cb650316bff708a1c75c", + "reference": "3833d7255cc303546435cb650316bff708a1c75c", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "suggest": { "ext-intl": "For best performance" @@ -6440,7 +6440,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.30.0" + "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.31.0" }, "funding": [ { @@ -6456,7 +6456,83 @@ "type": "tidelift" } ], - "time": "2024-05-31T15:07:36+00:00" + "time": "2024-09-09T11:45:10+00:00" + }, + { + "name": "symfony/polyfill-php81", + "version": "v1.31.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php81.git", + "reference": "4a4cfc2d253c21a5ad0e53071df248ed48c6ce5c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php81/zipball/4a4cfc2d253c21a5ad0e53071df248ed48c6ce5c", + "reference": "4a4cfc2d253c21a5ad0e53071df248ed48c6ce5c", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "type": "library", + "extra": { + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Php81\\": "" + }, + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-php81/tree/v1.31.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-09T11:45:10+00:00" }, { "name": "symfony/process", @@ -6790,30 +6866,37 @@ }, { "name": "twig/twig", - "version": "v3.8.0", + "version": "v3.14.0", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d" + "reference": "126b2c97818dbff0cdf3fbfc881aedb3d40aae72" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/9d15f0ac07f44dc4217883ec6ae02fd555c6f71d", - "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/126b2c97818dbff0cdf3fbfc881aedb3d40aae72", + "reference": "126b2c97818dbff0cdf3fbfc881aedb3d40aae72", "shasum": "" }, "require": { - "php": ">=7.2.5", + "php": ">=8.0.2", + "symfony/deprecation-contracts": "^2.5|^3", "symfony/polyfill-ctype": "^1.8", "symfony/polyfill-mbstring": "^1.3", - "symfony/polyfill-php80": "^1.22" + "symfony/polyfill-php81": "^1.29" }, "require-dev": { "psr/container": "^1.0|^2.0", - "symfony/phpunit-bridge": "^5.4.9|^6.3|^7.0" + "symfony/phpunit-bridge": "^5.4.9|^6.4|^7.0" }, "type": "library", "autoload": { + "files": [ + "src/Resources/core.php", + "src/Resources/debug.php", + "src/Resources/escaper.php", + "src/Resources/string_loader.php" + ], "psr-4": { "Twig\\": "src/" } @@ -6846,7 +6929,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.8.0" + "source": "https://github.com/twigphp/Twig/tree/v3.14.0" }, "funding": [ { @@ -6858,7 +6941,7 @@ "type": "tidelift" } ], - "time": "2023-11-21T18:54:41+00:00" + "time": "2024-09-09T17:55:12+00:00" }, { "name": "webmozart/glob", @@ -6936,5 +7019,5 @@ "platform-overrides": { "php": "8.3" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.2.0" } diff --git a/src/Appwrite/Event/Build.php b/src/Appwrite/Event/Build.php index b8cb62a6f8..af517f2598 100644 --- a/src/Appwrite/Event/Build.php +++ b/src/Appwrite/Event/Build.php @@ -115,6 +115,7 @@ class Build extends Event $client = new Client($this->queue, $this->connection); return $client->enqueue([ + 'sourceRegion' => System::getEnv('_APP_REGION', 'default'), 'project' => $this->project, 'resource' => $this->resource, 'deployment' => $this->deployment, diff --git a/src/Appwrite/Event/Certificate.php b/src/Appwrite/Event/Certificate.php index 85058c96fe..cacfcc867f 100644 --- a/src/Appwrite/Event/Certificate.php +++ b/src/Appwrite/Event/Certificate.php @@ -77,6 +77,7 @@ class Certificate extends Event $client = new Client($this->queue, $this->connection); return $client->enqueue([ + 'sourceRegion' => System::getEnv('_APP_REGION', 'default'), 'project' => $this->project, 'domain' => $this->domain, 'skipRenewCheck' => $this->skipRenewCheck diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index f9eb7d9a7d..b6eea1e748 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -121,6 +121,7 @@ class Database extends Event try { $result = $client->enqueue([ + 'sourceRegion' => System::getEnv('_APP_REGION', 'default'), 'project' => $this->project, 'user' => $this->user, 'type' => $this->type, diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 5e73378743..69cf8ddc2a 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -6,6 +6,7 @@ use InvalidArgumentException; use Utopia\Database\Document; use Utopia\Queue\Client; use Utopia\Queue\Connection; +use Utopia\System\System; class Event { @@ -109,6 +110,11 @@ class Event return $this->event; } + public function getSourceRegion(): string + { + return System::getEnv('_APP_REGION', 'default'); + } + /** * Set project for this event. * @@ -301,6 +307,7 @@ class Event $client = new Client($this->queue, $this->connection); return $client->enqueue([ + 'sourceRegion' => $this->getSourceRegion(), 'project' => $this->project, 'user' => $this->user, 'payload' => $this->payload, diff --git a/src/Appwrite/Event/Migration.php b/src/Appwrite/Event/Migration.php index 478291829b..23b959c880 100644 --- a/src/Appwrite/Event/Migration.php +++ b/src/Appwrite/Event/Migration.php @@ -79,6 +79,8 @@ class Migration extends Event $client = new Client($this->queue, $this->connection); return $client->enqueue([ + 'sourceRegion' => $this->getSourceRegion(), + 'project' => $this->project, 'user' => $this->user, 'migration' => $this->migration diff --git a/src/Appwrite/Messaging/Adapter.php b/src/Appwrite/Messaging/Adapter.php index 27dd7f68eb..6104d27c53 100644 --- a/src/Appwrite/Messaging/Adapter.php +++ b/src/Appwrite/Messaging/Adapter.php @@ -5,6 +5,8 @@ namespace Appwrite\Messaging; abstract class Adapter { abstract public function subscribe(string $projectId, mixed $identifier, array $roles, array $channels): void; + abstract public function unsubscribe(mixed $identifier): void; - abstract public static function send(string $projectId, array $payload, array $events, array $channels, array $roles, array $options): void; + + abstract public static function send(\redis $redis, string $projectId, array $payload, array $events, array $channels, array $roles, array $options): void; } diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index 55d8db2924..9359e2b4b5 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -122,15 +122,17 @@ class Realtime extends Adapter /** * Sends an event to the Realtime Server + * @param \Redis $redis * @param string $projectId * @param array $payload - * @param string $event + * @param array $events * @param array $channels * @param array $roles * @param array $options * @return void + * @throws \RedisException */ - public static function send(string $projectId, array $payload, array $events, array $channels, array $roles, array $options = []): void + public static function send(\Redis $redis, string $projectId, array $payload, array $events, array $channels, array $roles, array $options = []): void { if (empty($channels) || empty($roles) || empty($projectId)) { return; @@ -139,8 +141,6 @@ class Realtime extends Adapter $permissionsChanged = array_key_exists('permissionsChanged', $options) && $options['permissionsChanged']; $userId = array_key_exists('userId', $options) ? $options['userId'] : null; - $redis = new \Redis(); //TODO: make this part of the constructor - $redis->connect(System::getEnv('_APP_REDIS_HOST', ''), System::getEnv('_APP_REDIS_PORT', '')); $redis->publish('realtime', json_encode([ 'project' => $projectId, 'roles' => $roles, diff --git a/src/Appwrite/Platform/Workers/Builds.php b/src/Appwrite/Platform/Workers/Builds.php index 5dd2f7f886..18d4f9bd79 100644 --- a/src/Appwrite/Platform/Workers/Builds.php +++ b/src/Appwrite/Platform/Workers/Builds.php @@ -33,6 +33,11 @@ use Utopia\VCS\Adapter\Git\GitHub; class Builds extends Action { + /** + * @var mixed|string + */ + protected string $sourceRegion; + public static function getName(): string { return 'builds'; @@ -54,7 +59,8 @@ class Builds extends Action ->inject('dbForProject') ->inject('deviceForFunctions') ->inject('log') - ->callback(fn ($message, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Usage $usage, Cache $cache, Database $dbForProject, Device $deviceForFunctions, Log $log) => $this->action($message, $dbForConsole, $queueForEvents, $queueForFunctions, $usage, $cache, $dbForProject, $deviceForFunctions, $log)); + ->inject('realtimeConnection') + ->callback(fn ($message, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Usage $usage, Cache $cache, Database $dbForProject, Device $deviceForFunctions, Log $log, Callable $realtimeConnection) => $this->action($message, $dbForConsole, $queueForEvents, $queueForFunctions, $usage, $cache, $dbForProject, $deviceForFunctions, $log, $realtimeConnection)); } /** @@ -67,10 +73,11 @@ class Builds extends Action * @param Database $dbForProject * @param Device $deviceForFunctions * @param Log $log + * @param callable $realtimeConnection * @return void * @throws \Utopia\Database\Exception */ - public function action(Message $message, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Usage $queueForUsage, Cache $cache, Database $dbForProject, Device $deviceForFunctions, Log $log): void + public function action(Message $message, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Usage $queueForUsage, Cache $cache, Database $dbForProject, Device $deviceForFunctions, Log $log, Callable $realtimeConnection): void { $payload = $message->getPayload() ?? []; @@ -84,6 +91,7 @@ class Builds extends Action $deployment = new Document($payload['deployment'] ?? []); $template = new Document($payload['template'] ?? []); + $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); @@ -92,7 +100,7 @@ class Builds extends Action case BUILD_TYPE_RETRY: Console::info('Creating build for deployment: ' . $deployment->getId()); $github = new GitHub($cache); - $this->buildDeployment($deviceForFunctions, $queueForFunctions, $queueForEvents, $queueForUsage, $dbForConsole, $dbForProject, $github, $project, $resource, $deployment, $template, $log); + $this->buildDeployment($deviceForFunctions, $queueForFunctions, $queueForEvents, $queueForUsage, $dbForConsole, $dbForProject, $github, $project, $resource, $deployment, $template, $log, $realtimeConnection); break; default: @@ -117,7 +125,7 @@ class Builds extends Action * @throws \Utopia\Database\Exception * @throws Exception */ - protected function buildDeployment(Device $deviceForFunctions, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Database $dbForConsole, Database $dbForProject, GitHub $github, Document $project, Document $function, Document $deployment, Document $template, Log $log): void + protected function buildDeployment(Device $deviceForFunctions, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Database $dbForConsole, Database $dbForProject, GitHub $github, Document $project, Document $function, Document $deployment, Document $template, Log $log, Callable $realtimeConnection): void { $executor = new Executor(System::getEnv('_APP_EXECUTOR_HOST')); @@ -376,6 +384,7 @@ class Builds extends Action project: $project ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: 'console', payload: $build->getArrayCopy(), events: $allEvents, @@ -454,6 +463,7 @@ class Builds extends Action ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: 'console', payload: $build->getArrayCopy(), events: $allEvents, @@ -552,12 +562,12 @@ class Builds extends Action $err = $error; } }), - Co\go(function () use ($executor, $project, $deployment, &$response, &$build, $dbForProject, $allEvents, &$err, &$isCanceled) { + Co\go(function () use ($realtimeConnection, $executor, $project, $deployment, &$response, &$build, $dbForProject, $allEvents, &$err, &$isCanceled) { try { $executor->getLogs( deploymentId: $deployment->getId(), projectId: $project->getId(), - callback: function ($logs) use (&$response, &$err, &$build, $dbForProject, $allEvents, $project, &$isCanceled) { + callback: function ($logs) use ($realtimeConnection, &$response, &$err, &$build, $dbForProject, $allEvents, $project, &$isCanceled) { if ($isCanceled) { return; } @@ -591,6 +601,7 @@ class Builds extends Action project: $project ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: 'console', payload: $build->getArrayCopy(), events: $allEvents, @@ -693,6 +704,7 @@ class Builds extends Action project: $project ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: 'console', payload: $build->getArrayCopy(), events: $allEvents, diff --git a/src/Appwrite/Platform/Workers/Certificates.php b/src/Appwrite/Platform/Workers/Certificates.php index 58dc1dd28a..1d95fc6a51 100644 --- a/src/Appwrite/Platform/Workers/Certificates.php +++ b/src/Appwrite/Platform/Workers/Certificates.php @@ -30,6 +30,11 @@ use Utopia\System\System; class Certificates extends Action { + /** + * @var mixed|string + */ + protected string $sourceRegion; + public static function getName(): string { return 'certificates'; @@ -48,7 +53,8 @@ class Certificates extends Action ->inject('queueForEvents') ->inject('queueForFunctions') ->inject('log') - ->callback(fn (Message $message, Database $dbForConsole, Mail $queueForMails, Event $queueForEvents, Func $queueForFunctions, Log $log) => $this->action($message, $dbForConsole, $queueForMails, $queueForEvents, $queueForFunctions, $log)); + ->inject('realtimeConnection') + ->callback(fn (Message $message, Database $dbForConsole, Mail $queueForMails, Event $queueForEvents, Func $queueForFunctions, Log $log, Callable $realtimeConnection) => $this->action($message, $dbForConsole, $queueForMails, $queueForEvents, $queueForFunctions, $log, $realtimeConnection)); } /** @@ -58,11 +64,12 @@ class Certificates extends Action * @param Event $queueForEvents * @param Func $queueForFunctions * @param Log $log + * @param callable $realtimeConnection * @return void * @throws Throwable * @throws \Utopia\Database\Exception */ - public function action(Message $message, Database $dbForConsole, Mail $queueForMails, Event $queueForEvents, Func $queueForFunctions, Log $log): void + public function action(Message $message, Database $dbForConsole, Mail $queueForMails, Event $queueForEvents, Func $queueForFunctions, Log $log, Callable $realtimeConnection): void { $payload = $message->getPayload() ?? []; @@ -74,9 +81,9 @@ class Certificates extends Action $domain = new Domain($document->getAttribute('domain', '')); $skipRenewCheck = $payload['skipRenewCheck'] ?? false; + $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; $log->addTag('domain', $domain->get()); - - $this->execute($domain, $dbForConsole, $queueForMails, $queueForEvents, $queueForFunctions, $log, $skipRenewCheck); + $this->execute($domain, $dbForConsole, $queueForMails, $queueForEvents, $queueForFunctions, $log,$realtimeConnection, $skipRenewCheck); } /** @@ -85,12 +92,17 @@ class Certificates extends Action * @param Mail $queueForMails * @param Event $queueForEvents * @param Func $queueForFunctions + * @param Log $log + * @param callable $realtimeConnection * @param bool $skipRenewCheck * @return void + * @throws Authorization + * @throws Conflict + * @throws Structure * @throws Throwable * @throws \Utopia\Database\Exception */ - private function execute(Domain $domain, Database $dbForConsole, Mail $queueForMails, Event $queueForEvents, Func $queueForFunctions, Log $log, bool $skipRenewCheck = false): void + protected function execute(Domain $domain, Database $dbForConsole, Mail $queueForMails, Event $queueForEvents, Func $queueForFunctions, Log $log, Callable $realtimeConnection, bool $skipRenewCheck = false): void { /** * 1. Read arguments and validate domain @@ -193,7 +205,7 @@ class Certificates extends Action $certificate->setAttribute('updated', DateTime::now()); // Save all changes we made to certificate document into database - $this->saveCertificateDocument($domain->get(), $certificate, $success, $dbForConsole, $queueForEvents, $queueForFunctions); + $this->saveCertificateDocument($domain->get(), $certificate, $success, $dbForConsole, $queueForEvents, $queueForFunctions, $realtimeConnection); } } @@ -212,7 +224,7 @@ class Certificates extends Action * @throws Conflict * @throws Structure */ - private function saveCertificateDocument(string $domain, Document $certificate, bool $success, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions): void + protected function saveCertificateDocument(string $domain, Document $certificate, bool $success, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Callable $realtimeConnection): void { // Check if update or insert required $certificateDocument = $dbForConsole->findOne('certificates', [Query::equal('domain', [$domain])]); @@ -226,7 +238,7 @@ class Certificates extends Action } $certificateId = $certificate->getId(); - $this->updateDomainDocuments($certificateId, $domain, $success, $dbForConsole, $queueForEvents, $queueForFunctions); + $this->updateDomainDocuments($certificateId, $domain, $success, $dbForConsole, $queueForEvents, $queueForFunctions, $realtimeConnection); } /** @@ -234,7 +246,7 @@ class Certificates extends Action * * @return null|string Returns main domain. If null, there is no main domain yet. */ - private function getMainDomain(): ?string + protected function getMainDomain(): ?string { $envDomain = System::getEnv('_APP_DOMAIN', ''); if (!empty($envDomain) && $envDomain !== 'localhost') { @@ -255,7 +267,7 @@ class Certificates extends Action * @return void * @throws Exception */ - private function validateDomain(Domain $domain, bool $isMainDomain, Log $log): void + protected function validateDomain(Domain $domain, bool $isMainDomain, Log $log): void { if (empty($domain->get())) { throw new Exception('Missing certificate domain.'); @@ -299,7 +311,7 @@ class Certificates extends Action * @return bool True, if certificate needs to be renewed * @throws Exception */ - private function isRenewRequired(string $domain, Log $log): bool + protected function isRenewRequired(string $domain, Log $log): bool { $certPath = APP_STORAGE_CERTIFICATES . '/' . $domain . '/cert.pem'; if (\file_exists($certPath)) { @@ -333,7 +345,7 @@ class Certificates extends Action * @return array Named array with keys 'stdout' and 'stderr', both string * @throws Exception */ - private function issueCertificate(string $folder, string $domain, string $email): array + protected function issueCertificate(string $folder, string $domain, string $email): array { $stdout = ''; $stderr = ''; @@ -363,7 +375,7 @@ class Certificates extends Action * @return string * @throws \Utopia\Database\Exception */ - private function getRenewDate(string $domain): string + protected function getRenewDate(string $domain): string { $certPath = APP_STORAGE_CERTIFICATES . '/' . $domain . '/cert.pem'; $certData = openssl_x509_parse(file_get_contents($certPath)); @@ -381,7 +393,7 @@ class Certificates extends Action * @return void * @throws Exception */ - private function applyCertificateFiles(string $folder, string $domain, array $letsEncryptData): void + protected function applyCertificateFiles(string $folder, string $domain, array $letsEncryptData): void { // Prepare folder in storage for domain @@ -432,7 +444,7 @@ class Certificates extends Action * @return void * @throws Exception */ - private function notifyError(string $domain, string $errorMessage, int $attempt, Mail $queueForMails): void + protected function notifyError(string $domain, string $errorMessage, int $attempt, Mail $queueForMails): void { // Log error into console Console::warning('Cannot renew domain (' . $domain . ') on attempt no. ' . $attempt . ' certificate: ' . $errorMessage); @@ -475,7 +487,7 @@ class Certificates extends Action * * @return void */ - private function updateDomainDocuments(string $certificateId, string $domain, bool $success, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions): void + protected function updateDomainDocuments(string $certificateId, string $domain, bool $success, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Callable $realtimeConnection): void { $rule = $dbForConsole->findOne('rules', [ @@ -525,6 +537,7 @@ class Certificates extends Action project: $project ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: 'console', payload: $rule->getArrayCopy(), events: $allEvents, @@ -532,6 +545,7 @@ class Certificates extends Action roles: $target['roles'] ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: $project->getId(), payload: $rule->getArrayCopy(), events: $allEvents, diff --git a/src/Appwrite/Platform/Workers/Databases.php b/src/Appwrite/Platform/Workers/Databases.php index 56f5f012e8..5932deb7d9 100644 --- a/src/Appwrite/Platform/Workers/Databases.php +++ b/src/Appwrite/Platform/Workers/Databases.php @@ -21,6 +21,11 @@ use Utopia\Queue\Message; class Databases extends Action { + /** + * @var array|mixed + */ + protected string $sourceRegion; + public static function getName(): string { return 'databases'; @@ -37,7 +42,8 @@ class Databases extends Action ->inject('dbForConsole') ->inject('dbForProject') ->inject('log') - ->callback(fn (Message $message, Database $dbForConsole, Database $dbForProject, Log $log) => $this->action($message, $dbForConsole, $dbForProject, $log)); + ->inject('realtimeConnection') + ->callback(fn (Message $message, Database $dbForConsole, Database $dbForProject, Log $log, callable $realtimeConnection) => $this->action($message, $dbForConsole, $dbForProject, $log, $realtimeConnection)); } /** @@ -45,10 +51,16 @@ class Databases extends Action * @param Database $dbForConsole * @param Database $dbForProject * @param Log $log + * @param callable $realtimeConnection * @return void - * @throws \Exception + * @throws Authorization + * @throws Conflict + * @throws DatabaseException + * @throws Restricted + * @throws Structure + * @throws Exception */ - public function action(Message $message, Database $dbForConsole, Database $dbForProject, Log $log): void + public function action(Message $message, Database $dbForConsole, Database $dbForProject, Log $log, callable $realtimeConnection): void { $payload = $message->getPayload() ?? []; @@ -62,6 +74,7 @@ class Databases extends Action $document = new Document($payload['document'] ?? []); $database = new Document($payload['database'] ?? []); + $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); @@ -74,10 +87,10 @@ class Databases extends Action match (\strval($type)) { DATABASE_TYPE_DELETE_DATABASE => $this->deleteDatabase($database, $project, $dbForProject), DATABASE_TYPE_DELETE_COLLECTION => $this->deleteCollection($database, $collection, $project, $dbForProject), - DATABASE_TYPE_CREATE_ATTRIBUTE => $this->createAttribute($database, $collection, $document, $project, $dbForConsole, $dbForProject), - DATABASE_TYPE_DELETE_ATTRIBUTE => $this->deleteAttribute($database, $collection, $document, $project, $dbForConsole, $dbForProject), - DATABASE_TYPE_CREATE_INDEX => $this->createIndex($database, $collection, $document, $project, $dbForConsole, $dbForProject), - DATABASE_TYPE_DELETE_INDEX => $this->deleteIndex($database, $collection, $document, $project, $dbForConsole, $dbForProject), + DATABASE_TYPE_CREATE_ATTRIBUTE => $this->createAttribute($database, $collection, $document, $project, $dbForConsole, $dbForProject, $realtimeConnection), + DATABASE_TYPE_DELETE_ATTRIBUTE => $this->deleteAttribute($database, $collection, $document, $project, $dbForConsole, $dbForProject, $realtimeConnection), + DATABASE_TYPE_CREATE_INDEX => $this->createIndex($database, $collection, $document, $project, $dbForConsole, $dbForProject, $realtimeConnection), + DATABASE_TYPE_DELETE_INDEX => $this->deleteIndex($database, $collection, $document, $project, $dbForConsole, $dbForProject, $realtimeConnection), default => throw new \Exception('No database operation for type: ' . \strval($type)), }; } @@ -89,12 +102,15 @@ class Databases extends Action * @param Document $project * @param Database $dbForConsole * @param Database $dbForProject + * @param callable $realtimeConnection * @return void * @throws Authorization * @throws Conflict - * @throws \Exception + * @throws DatabaseException + * @throws Structure + * @throws Exception */ - private function createAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject): void + private function createAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject, callable $realtimeConnection): void { if ($collection->isEmpty()) { throw new Exception('Missing collection'); @@ -195,7 +211,7 @@ class Databases extends Action ); } } finally { - $this->trigger($database, $collection, $attribute, $project, $projectId, $events); + $this->trigger($database, $collection, $attribute, $project, $projectId, $events, $realtimeConnection); } if ($type === Database::VAR_RELATIONSHIP && $options['twoWay']) { @@ -212,12 +228,16 @@ class Databases extends Action * @param Document $project * @param Database $dbForConsole * @param Database $dbForProject + * @param callable $realtimeConnection * @return void * @throws Authorization * @throws Conflict - * @throws \Exception - **/ - private function deleteAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject): void + * @throws DatabaseException + * @throws Restricted + * @throws Structure + * @throws Exception + */ + private function deleteAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject, callable $realtimeConnection): void { if ($collection->isEmpty()) { throw new Exception('Missing collection'); @@ -296,7 +316,7 @@ class Databases extends Action ); } } finally { - $this->trigger($database, $collection, $attribute, $project, $projectId, $events); + $this->trigger($database, $collection, $attribute, $project, $projectId, $events, $realtimeConnection); } // The underlying database removes/rebuilds indexes when attribute is removed @@ -366,13 +386,15 @@ class Databases extends Action * @param Document $project * @param Database $dbForConsole * @param Database $dbForProject + * @param callable $realtimeConnection * @return void * @throws Authorization * @throws Conflict - * @throws Structure * @throws DatabaseException + * @throws Structure + * @throws Exception */ - private function createIndex(Document $database, Document $collection, Document $index, Document $project, Database $dbForConsole, Database $dbForProject): void + private function createIndex(Document $database, Document $collection, Document $index, Document $project, Database $dbForConsole, Database $dbForProject, callable $realtimeConnection): void { if ($collection->isEmpty()) { throw new Exception('Missing collection'); @@ -414,7 +436,7 @@ class Databases extends Action $index->setAttribute('status', 'failed') ); } finally { - $this->trigger($database, $collection, $index, $project, $projectId, $events); + $this->trigger($database, $collection, $index, $project, $projectId, $events, $realtimeConnection); } $dbForProject->purgeCachedDocument('database_' . $database->getInternalId(), $collectionId); @@ -427,13 +449,15 @@ class Databases extends Action * @param Document $project * @param Database $dbForConsole * @param Database $dbForProject + * @param callable $realtimeConnection * @return void * @throws Authorization * @throws Conflict - * @throws Structure * @throws DatabaseException + * @throws Structure + * @throws Exception */ - private function deleteIndex(Document $database, Document $collection, Document $index, Document $project, Database $dbForConsole, Database $dbForProject): void + private function deleteIndex(Document $database, Document $collection, Document $index, Document $project, Database $dbForConsole, Database $dbForProject, callable $realtimeConnection): void { if ($collection->isEmpty()) { throw new Exception('Missing collection'); @@ -472,7 +496,7 @@ class Databases extends Action $index->setAttribute('status', 'stuck') ); } finally { - $this->trigger($database, $collection, $index, $project, $projectId, $events); + $this->trigger($database, $collection, $index, $project, $projectId, $events, $realtimeConnection); } $dbForProject->purgeCachedDocument('database_' . $database->getInternalId(), $collection->getId()); @@ -612,13 +636,18 @@ class Databases extends Action Console::info("Deleted {$count} document by group in " . ($executionEnd - $executionStart) . " seconds"); } + /** + * @throws \RedisException + * @throws Exception + */ protected function trigger( Document $database, Document $collection, Document $attribute, Document $project, string $projectId, - array $events + array $events, + callable $realtimeConnection ): void { $target = Realtime::fromPayload( // Pass first, most verbose event pattern @@ -627,6 +656,7 @@ class Databases extends Action project: $project, ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: 'console', payload: $attribute->getArrayCopy(), events: $events, diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index c70d9ca11b..da6e85fdba 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -497,19 +497,20 @@ class Deletes extends Action ]; $limit = \count($projectCollectionIds) + 25; + $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); while (true) { $collections = $dbForProject->listCollections($limit); foreach ($collections as $collection) { - if ($dsn->getHost() !== System::getEnv('_APP_DATABASE_SHARED_TABLES', '') || !\in_array($collection->getId(), $projectCollectionIds)) { - $dbForProject->deleteCollection($collection->getId()); + if (! in_array($dsn->getHost(), $sharedTablesKeys) || !\in_array($collection->getId(), $projectCollectionIds)) { + $dbForProject->deleteCollection($collection->getId()); $dbForProject->deleteCollection($collection->getId()); } else { $this->deleteByGroup($collection->getId(), [], database: $dbForProject); } } - if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { + if (in_array($dsn->getHost(), $sharedTablesKeys)) { $collectionsIds = \array_map(fn ($collection) => $collection->getId(), $collections); if (empty(\array_diff($collectionsIds, $projectCollectionIds))) { @@ -558,7 +559,8 @@ class Deletes extends Action ], $dbForConsole); // Delete metadata table - if ($dsn->getHost() !== System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) { + System::getEnv('_APP_DATABASE_SHARED_TABLES', ''); + if (! in_array($dsn, $sharedTablesKeys)) { $dbForProject->deleteCollection('_metadata'); } else { $this->deleteByGroup('_metadata', [], $dbForProject); diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index e0f66c30f9..481486e63f 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -28,6 +28,11 @@ use Utopia\System\System; class Functions extends Action { + /** + * @var mixed|string + */ + protected string $sourceRegion; + public static function getName(): string { return 'functions'; @@ -47,7 +52,8 @@ class Functions extends Action ->inject('queueForEvents') ->inject('queueForUsage') ->inject('log') - ->callback(fn (Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log) => $this->action($message, $dbForProject, $queueForFunctions, $queueForEvents, $queueForUsage, $log)); + ->inject('realtimeConnection') + ->callback(fn (Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log, Callable $realtimeConnection) => $this->action($message, $dbForProject, $queueForFunctions, $queueForEvents, $queueForUsage, $log, $realtimeConnection)); } /** @@ -63,7 +69,7 @@ class Functions extends Action * @throws \Utopia\Database\Exception * @throws Conflict */ - public function action(Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log): void + public function action(Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log, Callable $realtimeConnection): void { $payload = $message->getPayload() ?? []; @@ -93,6 +99,9 @@ class Functions extends Action return; } + $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; + + if ($function->isEmpty() && !empty($functionId)) { $function = $dbForProject->getDocument('functions', $functionId); } @@ -125,6 +134,7 @@ class Functions extends Action Console::success('Iterating function: ' . $function->getAttribute('name')); $this->execute( + realtimeConnection: $realtimeConnection, log: $log, dbForProject: $dbForProject, queueForFunctions: $queueForFunctions, @@ -161,6 +171,7 @@ class Functions extends Action $execution = new Document($payload['execution'] ?? []); $user = new Document($payload['user'] ?? []); $this->execute( + realtimeConnection: $realtimeConnection, log: $log, dbForProject: $dbForProject, queueForFunctions: $queueForFunctions, @@ -183,6 +194,7 @@ class Functions extends Action case 'schedule': $execution = new Document($payload['execution'] ?? []); $this->execute( + realtimeConnection: $realtimeConnection, log: $log, dbForProject: $dbForProject, queueForFunctions: $queueForFunctions, @@ -292,6 +304,7 @@ class Functions extends Action * @throws Conflict */ private function execute( + Callable $realtimeConnection, Log $log, Database $dbForProject, Func $queueForFunctions, @@ -583,6 +596,7 @@ class Functions extends Action payload: $execution ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: 'console', payload: $execution->getArrayCopy(), events: $allEvents, @@ -590,6 +604,7 @@ class Functions extends Action roles: $target['roles'] ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: $project->getId(), payload: $execution->getArrayCopy(), events: $allEvents, diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 8ab5ebac46..13812a2e12 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -32,6 +32,10 @@ class Migrations extends Action { private ?Database $dbForProject = null; private ?Database $dbForConsole = null; + /** + * @var string + */ + protected string $sourceRegion; public static function getName(): string { @@ -49,7 +53,8 @@ class Migrations extends Action ->inject('dbForProject') ->inject('dbForConsole') ->inject('log') - ->callback(fn (Message $message, Database $dbForProject, Database $dbForConsole, Log $log) => $this->action($message, $dbForProject, $dbForConsole, $log)); + ->inject('realtimeConnection') + ->callback(fn (Message $message, Database $dbForProject, Database $dbForConsole, Log $log, Callable $realtimeConnection) => $this->action($message, $dbForProject, $dbForConsole, $log, $realtimeConnection)); } /** @@ -60,7 +65,7 @@ class Migrations extends Action * @return void * @throws Exception */ - public function action(Message $message, Database $dbForProject, Database $dbForConsole, Log $log): void + public function action(Message $message, Database $dbForProject, Database $dbForConsole, Log $log, Callable $realtimeConnection): void { $payload = $message->getPayload() ?? []; @@ -76,6 +81,7 @@ class Migrations extends Action return; } + $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; $this->dbForProject = $dbForProject; $this->dbForConsole = $dbForConsole; @@ -89,7 +95,7 @@ class Migrations extends Action $log->addTag('migrationId', $migration->getId()); $log->addTag('projectId', $project->getId()); - $this->processMigration($project, $migration, $log); + $this->processMigration($project, $migration, $log, $realtimeConnection); } /** @@ -134,7 +140,7 @@ class Migrations extends Action * @throws \Utopia\Database\Exception * @throws Exception */ - protected function updateMigrationDocument(Document $migration, Document $project): Document + protected function updateMigrationDocument(Document $migration, Document $project, Callable $realtimeConnection): Document { /** Trigger Realtime */ $allEvents = Event::generateEvents('migrations.[migrationId].update', [ @@ -148,6 +154,7 @@ class Migrations extends Action ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: 'console', payload: $migration->getArrayCopy(), events: $allEvents, @@ -156,6 +163,7 @@ class Migrations extends Action ); Realtime::send( + redis: $realtimeConnection($this->sourceRegion), projectId: $project->getId(), payload: $migration->getArrayCopy(), events: $allEvents, @@ -236,6 +244,7 @@ class Migrations extends Action * @param Document $project * @param Document $migration * @param Log $log + * @param callable $realtimeConnection * @return void * @throws Authorization * @throws Conflict @@ -243,7 +252,7 @@ class Migrations extends Action * @throws Structure * @throws \Utopia\Database\Exception */ - protected function processMigration(Document $project, Document $migration, Log $log): void + protected function processMigration(Document $project, Document $migration, Log $log, Callable $realtimeConnection): void { /** * @var Document $migrationDocument @@ -259,7 +268,7 @@ class Migrations extends Action $migrationDocument->setAttribute('stage', 'processing'); $migrationDocument->setAttribute('status', 'processing'); $log->addBreadcrumb(new Breadcrumb("debug", "migration", "Migration hit stage 'processing'", \microtime(true))); - $this->updateMigrationDocument($migrationDocument, $projectDocument); + $this->updateMigrationDocument($migrationDocument, $projectDocument, $realtimeConnection); $log->addTag('type', $migrationDocument->getAttribute('source')); @@ -281,12 +290,12 @@ class Migrations extends Action /** Start Transfer */ $migrationDocument->setAttribute('stage', 'migrating'); $log->addBreadcrumb(new Breadcrumb("debug", "migration", "Migration hit stage 'migrating'", \microtime(true))); - $this->updateMigrationDocument($migrationDocument, $projectDocument); - $transfer->run($migrationDocument->getAttribute('resources'), function () use ($migrationDocument, $transfer, $projectDocument) { + $this->updateMigrationDocument($migrationDocument, $projectDocument, $realtimeConnection); + $transfer->run($migrationDocument->getAttribute('resources'), function () use ($realtimeConnection, $migrationDocument, $transfer, $projectDocument) { $migrationDocument->setAttribute('resourceData', json_encode($transfer->getCache())); $migrationDocument->setAttribute('statusCounters', json_encode($transfer->getStatusCounters())); - $this->updateMigrationDocument($migrationDocument, $projectDocument); + $this->updateMigrationDocument($migrationDocument, $projectDocument, $realtimeConnection); }); $sourceErrors = $source->getErrors(); @@ -309,7 +318,7 @@ class Migrations extends Action $migrationDocument->setAttribute('errors', $errorMessages); $log->addExtra('migrationErrors', json_encode($errorMessages)); - $this->updateMigrationDocument($migrationDocument, $projectDocument); + $this->updateMigrationDocument($migrationDocument, $projectDocument, $realtimeConnection); return; } @@ -352,7 +361,7 @@ class Migrations extends Action $this->removeAPIKey($tempAPIKey); } if ($migrationDocument) { - $this->updateMigrationDocument($migrationDocument, $projectDocument); + $this->updateMigrationDocument($migrationDocument, $projectDocument, $realtimeConnection); if ($migrationDocument->getAttribute('status', '') == 'failed') { throw new Exception("Migration failed"); From 501411f5b5af509d119be02ccce6224fcff75c78 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 15 Sep 2024 17:42:46 +0200 Subject: [PATCH 02/47] 1.6.x --- app/config/bla | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 app/config/bla diff --git a/app/config/bla b/app/config/bla deleted file mode 100644 index 37b151a403..0000000000 --- a/app/config/bla +++ /dev/null @@ -1,36 +0,0 @@ -version: '3.8' - -services: - db_15: - image: mysql:8.0.36-debian - container_name: mysql_db_15 - environment: - MYSQL_ROOT_PASSWORD: lJVYGNZTOTF4VsGSnz5CWoVp - MYSQL_DATABASE: appwrite - MYSQL_USER: appwrite_user - MYSQL_PASSWORD: Fy8i367HpJd8EB1VGos3vsf - volumes: - - /mnt/db-15:/var/lib/mysql - ports: - - "3307:3306" - networks: - - mysql_network - - db_16: - image: mysql:8.0.36-debian - container_name: mysql_db_16 - environment: - MYSQL_ROOT_PASSWORD: d1RNc7NsnsQ8I1OQd0eeLWr0 - MYSQL_DATABASE: appwrite - MYSQL_USER: appwrite_user - MYSQL_PASSWORD: KlQ4NMyl46YPumqSA8CUOLjO - volumes: - - /mnt/db-16:/var/lib/mysql - ports: - - "3308:3306" - networks: - - mysql_network - -networks: - mysql_network: - driver: bridge \ No newline at end of file From fd8b7eb6fc3bf95b6276ca0ff69b1303b44e3af4 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 17 Sep 2024 15:35:48 +0300 Subject: [PATCH 03/47] update --- app/controllers/api/databases.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index a9bb58df4b..8c8dc2ca57 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -205,7 +205,7 @@ function createAttribute(string $databaseId, string $collectionId, Document $att $dbForProject->purgeCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId()); $dbForProject->purgeCachedCollection('database_' . $db->getInternalId() . '_collection_' . $relatedCollection->getInternalId()); } - + var_dump($queueForDatabase); $queueForDatabase ->setType(DATABASE_TYPE_CREATE_ATTRIBUTE) ->setDatabase($db) From eb553fa96ce017d694a54f39fa48938381c6d6a1 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 17 Sep 2024 21:09:16 +0300 Subject: [PATCH 04/47] debug --- src/Appwrite/Event/Database.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index b6eea1e748..9fe991a6dd 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -108,6 +108,10 @@ class Database extends Event */ public function trigger(): string|bool { + + var_dump('database='); + var_dump($this->getProject()->getAttribute('database')); + try { $dsn = new DSN($this->getProject()->getAttribute('database')); } catch (\InvalidArgumentException) { @@ -115,6 +119,9 @@ class Database extends Event $dsn = new DSN('mysql://' . $this->getProject()->getAttribute('database')); } + var_dump('$dsn->getHost()='); + var_dump($dsn->getHost()); + $this->setQueue($dsn->getHost()); $client = new Client($this->queue, $this->connection); From 134f90a46454ae4818c1948f1ab30f5f9a86f0f4 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 17 Sep 2024 23:32:36 +0300 Subject: [PATCH 05/47] fix --- app/controllers/api/databases.php | 2 +- src/Appwrite/Event/Database.php | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 8c8dc2ca57..a9bb58df4b 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -205,7 +205,7 @@ function createAttribute(string $databaseId, string $collectionId, Document $att $dbForProject->purgeCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId()); $dbForProject->purgeCachedCollection('database_' . $db->getInternalId() . '_collection_' . $relatedCollection->getInternalId()); } - var_dump($queueForDatabase); + $queueForDatabase ->setType(DATABASE_TYPE_CREATE_ATTRIBUTE) ->setDatabase($db) diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index 9fe991a6dd..69c38e2ec5 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -109,8 +109,6 @@ class Database extends Event public function trigger(): string|bool { - var_dump('database='); - var_dump($this->getProject()->getAttribute('database')); try { $dsn = new DSN($this->getProject()->getAttribute('database')); @@ -119,9 +117,6 @@ class Database extends Event $dsn = new DSN('mysql://' . $this->getProject()->getAttribute('database')); } - var_dump('$dsn->getHost()='); - var_dump($dsn->getHost()); - $this->setQueue($dsn->getHost()); $client = new Client($this->queue, $this->connection); From b706571a2122ec55a5b9171ea6a11ff8814ffc72 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 18 Sep 2024 10:55:30 +0300 Subject: [PATCH 06/47] _APP_OPTIONS_ROUTER_PROTECTION --- app/controllers/general.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/general.php b/app/controllers/general.php index 0bbfa2b694..c63dfae1ab 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -57,6 +57,10 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo ]) )[0] ?? null; + var_dump(System::getEnv('_APP_DOMAIN_FUNCTIONS', '')); + var_dump($host); + var_dump(APP_HOSTNAME_INTERNAL); + if ($route === null) { if ($host === System::getEnv('_APP_DOMAIN_FUNCTIONS', '')) { throw new AppwriteException(AppwriteException::GENERAL_ACCESS_FORBIDDEN, 'This domain cannot be used for security reasons. Please use any subdomain instead.'); From ec02d881c980c51dc7cc0c4d6320479357d8dd71 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 18 Sep 2024 11:52:59 +0300 Subject: [PATCH 07/47] updates --- app/controllers/general.php | 4 +++- src/Appwrite/Event/Build.php | 2 +- src/Appwrite/Event/Certificate.php | 2 +- src/Appwrite/Event/Database.php | 3 +-- src/Appwrite/Event/Event.php | 2 ++ src/Appwrite/Event/Func.php | 1 + src/Appwrite/Event/Migration.php | 1 - src/Appwrite/Platform/Workers/Builds.php | 2 +- src/Appwrite/Platform/Workers/Certificates.php | 2 +- src/Appwrite/Platform/Workers/Databases.php | 2 +- src/Appwrite/Platform/Workers/Functions.php | 1 - 11 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index c63dfae1ab..1240d659a0 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -60,7 +60,9 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo var_dump(System::getEnv('_APP_DOMAIN_FUNCTIONS', '')); var_dump($host); var_dump(APP_HOSTNAME_INTERNAL); - + var_dump($request->getHeader('host')); + var_dump($request->getHeader('x-forwarded-host')); + if ($route === null) { if ($host === System::getEnv('_APP_DOMAIN_FUNCTIONS', '')) { throw new AppwriteException(AppwriteException::GENERAL_ACCESS_FORBIDDEN, 'This domain cannot be used for security reasons. Please use any subdomain instead.'); diff --git a/src/Appwrite/Event/Build.php b/src/Appwrite/Event/Build.php index af517f2598..237803a7ff 100644 --- a/src/Appwrite/Event/Build.php +++ b/src/Appwrite/Event/Build.php @@ -115,7 +115,7 @@ class Build extends Event $client = new Client($this->queue, $this->connection); return $client->enqueue([ - 'sourceRegion' => System::getEnv('_APP_REGION', 'default'), + 'sourceRegion' => $this->getSourceRegion(), 'project' => $this->project, 'resource' => $this->resource, 'deployment' => $this->deployment, diff --git a/src/Appwrite/Event/Certificate.php b/src/Appwrite/Event/Certificate.php index cacfcc867f..188e6dd27d 100644 --- a/src/Appwrite/Event/Certificate.php +++ b/src/Appwrite/Event/Certificate.php @@ -77,7 +77,7 @@ class Certificate extends Event $client = new Client($this->queue, $this->connection); return $client->enqueue([ - 'sourceRegion' => System::getEnv('_APP_REGION', 'default'), + 'sourceRegion' => $this->getSourceRegion(), 'project' => $this->project, 'domain' => $this->domain, 'skipRenewCheck' => $this->skipRenewCheck diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index 69c38e2ec5..4b53b71d99 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -109,7 +109,6 @@ class Database extends Event public function trigger(): string|bool { - try { $dsn = new DSN($this->getProject()->getAttribute('database')); } catch (\InvalidArgumentException) { @@ -123,7 +122,7 @@ class Database extends Event try { $result = $client->enqueue([ - 'sourceRegion' => System::getEnv('_APP_REGION', 'default'), + 'sourceRegion' => $this->getSourceRegion(), 'project' => $this->project, 'user' => $this->user, 'type' => $this->type, diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 69cf8ddc2a..3595be9216 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -110,6 +110,8 @@ class Event return $this->event; } + + public function getSourceRegion(): string { return System::getEnv('_APP_REGION', 'default'); diff --git a/src/Appwrite/Event/Func.php b/src/Appwrite/Event/Func.php index 451df2b6c1..a099f7152c 100644 --- a/src/Appwrite/Event/Func.php +++ b/src/Appwrite/Event/Func.php @@ -220,6 +220,7 @@ class Func extends Event $events = $this->getEvent() ? Event::generateEvents($this->getEvent(), $this->getParams()) : null; return $client->enqueue([ + 'sourceRegion' => $this->getSourceRegion(), 'project' => $this->project, 'user' => $this->user, 'function' => $this->function, diff --git a/src/Appwrite/Event/Migration.php b/src/Appwrite/Event/Migration.php index 23b959c880..eddce6354e 100644 --- a/src/Appwrite/Event/Migration.php +++ b/src/Appwrite/Event/Migration.php @@ -80,7 +80,6 @@ class Migration extends Event return $client->enqueue([ 'sourceRegion' => $this->getSourceRegion(), - 'project' => $this->project, 'user' => $this->user, 'migration' => $this->migration diff --git a/src/Appwrite/Platform/Workers/Builds.php b/src/Appwrite/Platform/Workers/Builds.php index 18d4f9bd79..36fe1a6d1a 100644 --- a/src/Appwrite/Platform/Workers/Builds.php +++ b/src/Appwrite/Platform/Workers/Builds.php @@ -90,8 +90,8 @@ class Builds extends Action $resource = new Document($payload['resource'] ?? []); $deployment = new Document($payload['deployment'] ?? []); $template = new Document($payload['template'] ?? []); - $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; + $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); diff --git a/src/Appwrite/Platform/Workers/Certificates.php b/src/Appwrite/Platform/Workers/Certificates.php index 1d95fc6a51..7a4ce1ef0b 100644 --- a/src/Appwrite/Platform/Workers/Certificates.php +++ b/src/Appwrite/Platform/Workers/Certificates.php @@ -80,8 +80,8 @@ class Certificates extends Action $document = new Document($payload['domain'] ?? []); $domain = new Domain($document->getAttribute('domain', '')); $skipRenewCheck = $payload['skipRenewCheck'] ?? false; - $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; + $log->addTag('domain', $domain->get()); $this->execute($domain, $dbForConsole, $queueForMails, $queueForEvents, $queueForFunctions, $log,$realtimeConnection, $skipRenewCheck); } diff --git a/src/Appwrite/Platform/Workers/Databases.php b/src/Appwrite/Platform/Workers/Databases.php index 5932deb7d9..7f3132dcb0 100644 --- a/src/Appwrite/Platform/Workers/Databases.php +++ b/src/Appwrite/Platform/Workers/Databases.php @@ -73,8 +73,8 @@ class Databases extends Action $collection = new Document($payload['collection'] ?? []); $document = new Document($payload['document'] ?? []); $database = new Document($payload['database'] ?? []); - $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; + $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index 481486e63f..e03a8ac069 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -101,7 +101,6 @@ class Functions extends Action $this->sourceRegion = $payload['sourceRegion'] ?? 'default'; - if ($function->isEmpty() && !empty($functionId)) { $function = $dbForProject->getDocument('functions', $functionId); } From 812d8a34cf60db4ce2a76648466d10215123d1c1 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 18 Sep 2024 13:45:09 +0300 Subject: [PATCH 08/47] updates --- app/controllers/general.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 1240d659a0..2273e802bb 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -57,12 +57,12 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo ]) )[0] ?? null; - var_dump(System::getEnv('_APP_DOMAIN_FUNCTIONS', '')); - var_dump($host); - var_dump(APP_HOSTNAME_INTERNAL); - var_dump($request->getHeader('host')); - var_dump($request->getHeader('x-forwarded-host')); - +// var_dump(System::getEnv('_APP_DOMAIN_FUNCTIONS', '')); +// var_dump($host); +// var_dump(APP_HOSTNAME_INTERNAL); +// var_dump($request->getHeader('host')); +// var_dump($request->getHeader('x-forwarded-host')); + if ($route === null) { if ($host === System::getEnv('_APP_DOMAIN_FUNCTIONS', '')) { throw new AppwriteException(AppwriteException::GENERAL_ACCESS_FORBIDDEN, 'This domain cannot be used for security reasons. Please use any subdomain instead.'); From d1b062a418b9246d24e20c1e90f4e2b7598fc758 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 19 Sep 2024 20:03:15 +0300 Subject: [PATCH 09/47] Scheduled task fix --- app/cli.php | 4 ++++ src/Appwrite/Platform/Tasks/ScheduleBase.php | 21 ++++++++----------- .../Platform/Tasks/ScheduleExecutions.php | 10 ++++----- .../Platform/Tasks/ScheduleFunctions.php | 12 ++++------- .../Platform/Tasks/ScheduleMessages.php | 12 +++++------ 5 files changed, 26 insertions(+), 33 deletions(-) diff --git a/app/cli.php b/app/cli.php index 0dec8bad04..9b85804349 100644 --- a/app/cli.php +++ b/app/cli.php @@ -156,15 +156,19 @@ CLI::setResource('getProjectDB', function (Group $pools, Database $dbForConsole, CLI::setResource('queue', function (Group $pools) { return $pools->get('queue')->pop()->getResource(); }, ['pools']); + CLI::setResource('queueForFunctions', function (Connection $queue) { return new Func($queue); }, ['queue']); + CLI::setResource('queueForDeletes', function (Connection $queue) { return new Delete($queue); }, ['queue']); + CLI::setResource('queueForCertificates', function (Connection $queue) { return new Certificate($queue); }, ['queue']); + CLI::setResource('logError', function (Registry $register) { return function (Throwable $error, string $namespace, string $action) use ($register) { $logger = $register->get('logger'); diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index e013220aa4..29ca74ee8e 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -11,6 +11,7 @@ use Utopia\Database\Exception; use Utopia\Database\Query; use Utopia\Platform\Action; use Utopia\Pools\Group; +use Utopia\Queue\Connection; use Utopia\System\System; use function Swoole\Coroutine\run; @@ -26,7 +27,7 @@ abstract class ScheduleBase extends Action abstract public static function getSupportedResource(): string; abstract protected function enqueueResources( - Group $pools, + Connection $queue, Database $dbForConsole ); @@ -36,10 +37,10 @@ abstract class ScheduleBase extends Action $this ->desc("Execute {$type}s scheduled in Appwrite") - ->inject('pools') + ->inject('queue') ->inject('dbForConsole') ->inject('getProjectDB') - ->callback(fn (Group $pools, Database $dbForConsole, callable $getProjectDB) => $this->action($pools, $dbForConsole, $getProjectDB)); + ->callback(fn (Connection $queue, Database $dbForConsole, callable $getProjectDB) => $this->action($queue, $dbForConsole, $getProjectDB)); } /** @@ -47,7 +48,7 @@ abstract class ScheduleBase extends Action * 2. Create timer that sync all changes from 'schedules' collection to local copy. Only reading changes thanks to 'resourceUpdatedAt' attribute * 3. Create timer that prepares coroutines for soon-to-execute schedules. When it's ready, coroutine sleeps until exact time before sending request to worker. */ - public function action(Group $pools, Database $dbForConsole, callable $getProjectDB): void + public function action(Connection $queue, Database $dbForConsole, callable $getProjectDB): void { Console::title(\ucfirst(static::getSupportedResource()) . ' scheduler V1'); Console::success(APP_NAME . ' ' . \ucfirst(static::getSupportedResource()) . ' scheduler v1 has started'); @@ -127,17 +128,15 @@ abstract class ScheduleBase extends Action $latestDocument = \end($results); } - $pools->reclaim(); - Console::success("{$total} resources were loaded in " . (\microtime(true) - $loadStart) . " seconds"); Console::success("Starting timers at " . DateTime::now()); - run(function () use ($dbForConsole, &$lastSyncUpdate, $getSchedule, $pools) { + run(function () use ($dbForConsole, &$lastSyncUpdate, $getSchedule, $queue) { /** * The timer synchronize $schedules copy with database collection. */ - Timer::tick(static::UPDATE_TIMER * 1000, function () use ($dbForConsole, &$lastSyncUpdate, $getSchedule, $pools) { + Timer::tick(static::UPDATE_TIMER * 1000, function () use ($dbForConsole, &$lastSyncUpdate, $getSchedule, $queue) { $time = DateTime::now(); $timerStart = \microtime(true); @@ -186,17 +185,15 @@ abstract class ScheduleBase extends Action $lastSyncUpdate = $time; $timerEnd = \microtime(true); - $pools->reclaim(); - Console::log("Sync tick: {$total} schedules were updated in " . ($timerEnd - $timerStart) . " seconds"); }); Timer::tick( static::ENQUEUE_TIMER * 1000, - fn () => $this->enqueueResources($pools, $dbForConsole) + fn () => $this->enqueueResources($queue, $dbForConsole) ); - $this->enqueueResources($pools, $dbForConsole); + $this->enqueueResources($queue, $dbForConsole); }); } } diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index 0e618642d5..2822aea130 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -6,6 +6,7 @@ use Appwrite\Event\Func; use Swoole\Coroutine as Co; use Utopia\Database\Database; use Utopia\Pools\Group; +use Utopia\Queue\Connection; class ScheduleExecutions extends ScheduleBase { @@ -22,11 +23,10 @@ class ScheduleExecutions extends ScheduleBase return 'execution'; } - protected function enqueueResources(Group $pools, Database $dbForConsole): void + protected function enqueueResources(Connection $queue, Database $dbForConsole): void { - $queue = $pools->get('queue')->pop(); - $connection = $queue->getResource(); - $queueForFunctions = new Func($connection); + + $queueForFunctions = new Func($queue); $intervalEnd = (new \DateTime())->modify('+' . self::ENQUEUE_TIMER . ' seconds'); foreach ($this->schedules as $schedule) { @@ -72,7 +72,5 @@ class ScheduleExecutions extends ScheduleBase unset($this->schedules[$schedule['$internalId']]); } - - $queue->reclaim(); } } diff --git a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php index e2c278714f..a598bb5f45 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php @@ -8,6 +8,7 @@ use Utopia\CLI\Console; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Pools\Group; +use Utopia\Queue\Connection; class ScheduleFunctions extends ScheduleBase { @@ -26,7 +27,7 @@ class ScheduleFunctions extends ScheduleBase return 'function'; } - protected function enqueueResources(Group $pools, Database $dbForConsole): void + protected function enqueueResources(Connection $queue, Database $dbForConsole): void { $timerStart = \microtime(true); $time = DateTime::now(); @@ -65,12 +66,9 @@ class ScheduleFunctions extends ScheduleBase } foreach ($delayedExecutions as $delay => $scheduleKeys) { - \go(function () use ($delay, $scheduleKeys, $pools) { + \go(function () use ($delay, $scheduleKeys, $queue) { \sleep($delay); // in seconds - $queue = $pools->get('queue')->pop(); - $connection = $queue->getResource(); - foreach ($scheduleKeys as $scheduleKey) { // Ensure schedule was not deleted if (!\array_key_exists($scheduleKey, $this->schedules)) { @@ -79,7 +77,7 @@ class ScheduleFunctions extends ScheduleBase $schedule = $this->schedules[$scheduleKey]; - $queueForFunctions = new Func($connection); + $queueForFunctions = new Func($queue); $queueForFunctions ->setType('schedule') @@ -89,8 +87,6 @@ class ScheduleFunctions extends ScheduleBase ->setProject($schedule['project']) ->trigger(); } - - $queue->reclaim(); }); } diff --git a/src/Appwrite/Platform/Tasks/ScheduleMessages.php b/src/Appwrite/Platform/Tasks/ScheduleMessages.php index 167f1282ed..cd136cf67d 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleMessages.php +++ b/src/Appwrite/Platform/Tasks/ScheduleMessages.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Event\Messaging; use Utopia\Database\Database; use Utopia\Pools\Group; +use Utopia\Queue\Connection; class ScheduleMessages extends ScheduleBase { @@ -21,7 +22,7 @@ class ScheduleMessages extends ScheduleBase return 'message'; } - protected function enqueueResources(Group $pools, Database $dbForConsole): void + protected function enqueueResources(Connection $queue, Database $dbForConsole): void { foreach ($this->schedules as $schedule) { if (!$schedule['active']) { @@ -35,10 +36,9 @@ class ScheduleMessages extends ScheduleBase continue; } - \go(function () use ($schedule, $pools, $dbForConsole) { - $queue = $pools->get('queue')->pop(); - $connection = $queue->getResource(); - $queueForMessaging = new Messaging($connection); + \go(function () use ($schedule, $queue, $dbForConsole) { + + $queueForMessaging = new Messaging($queue); $queueForMessaging ->setType(MESSAGE_SEND_TYPE_EXTERNAL) @@ -51,8 +51,6 @@ class ScheduleMessages extends ScheduleBase $schedule['$id'], ); - $queue->reclaim(); - unset($this->schedules[$schedule['$internalId']]); }); } From 0de1e649588ff7472d5bbbe9fee13084ce53bf2a Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 24 Sep 2024 12:41:14 +0300 Subject: [PATCH 10/47] debug --- src/Appwrite/Platform/Workers/Certificates.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Workers/Certificates.php b/src/Appwrite/Platform/Workers/Certificates.php index 7a4ce1ef0b..2261912551 100644 --- a/src/Appwrite/Platform/Workers/Certificates.php +++ b/src/Appwrite/Platform/Workers/Certificates.php @@ -174,7 +174,7 @@ class Certificates extends Action $logs = 'Certificate successfully generated.'; $certificate->setAttribute('logs', \mb_strcut($logs, 0, 1000000));// Limit to 1MB - + var_dump($certificate); // Give certificates to Traefik $this->applyCertificateFiles($folder, $domain->get(), $letsEncryptData); From e4d554689f220969fd4b181c05d8d9b4a0cd8b09 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 29 Sep 2024 17:35:27 +0300 Subject: [PATCH 11/47] debug --- app/controllers/general.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 2273e802bb..3e1d77aad5 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -1006,7 +1006,9 @@ App::get('/.well-known/acme-challenge/*') ->action(function (Request $request, Response $response) { $uriChunks = \explode('/', $request->getURI()); $token = $uriChunks[\count($uriChunks) - 1]; - + var_dump('Appwrite acme-challenge'); + var_dump($request->getURI()); + var_dump($token); $validator = new Text(100, allowList: [ ...Text::NUMBERS, ...Text::ALPHABET_LOWER, From 08e5ed6a8a62da5b072b6cb16fc31e68089d4946 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 29 Sep 2024 17:37:56 +0300 Subject: [PATCH 12/47] debug --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 26b8c3a7a3..b55b0102a5 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/cache": "0.10.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "0.53.5-rc1", + "utopia-php/database": "0.53.*", "utopia-php/domains": "0.5.*", "utopia-php/dsn": "0.2.1", "utopia-php/framework": "0.33.*", From 8f6151a575d57a2f86e400fb32e3543b126031a9 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 6 Oct 2024 18:56:39 +0300 Subject: [PATCH 13/47] updates --- app/init.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/init.php b/app/init.php index 9ca874aafc..a10bdd6896 100644 --- a/app/init.php +++ b/app/init.php @@ -90,10 +90,6 @@ use Utopia\Validator\Range; use Utopia\Validator\URL; use Utopia\Validator\WhiteList; use Utopia\VCS\Adapter\Git\GitHub as VcsGitHub; -use Appwrite\Messaging\Adapter\Realtime; -use Appwrite\Utopia\Request; - - const APP_NAME = 'Appwrite'; const APP_DOMAIN = 'appwrite.io'; From 6b429c9da9c43baf8f22ee00b34f11082d045c8b Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 6 Oct 2024 18:59:22 +0300 Subject: [PATCH 14/47] updates --- app/controllers/general.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 7cb971a228..8261356d89 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -1010,9 +1010,6 @@ App::get('/.well-known/acme-challenge/*') ->action(function (Request $request, Response $response) { $uriChunks = \explode('/', $request->getURI()); $token = $uriChunks[\count($uriChunks) - 1]; - var_dump('Appwrite acme-challenge'); - var_dump($request->getURI()); - var_dump($token); $validator = new Text(100, allowList: [ ...Text::NUMBERS, ...Text::ALPHABET_LOWER, From 0cce55565a611feccbc1101671ceaae0b4b4d7be Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 6 Oct 2024 20:16:00 +0300 Subject: [PATCH 15/47] updates --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 3c48977a7c..275fd9dbb9 100644 --- a/composer.lock +++ b/composer.lock @@ -1624,16 +1624,16 @@ }, { "name": "utopia-php/cli", - "version": "0.15.0", + "version": "0.15.1", "source": { "type": "git", "url": "https://github.com/utopia-php/cli.git", - "reference": "ccb7c8125ffe0254fef8f25744bfa376eb7bd0ea" + "reference": "d69bbe51a6a94dc4e5bcdd542b5938038b985a65" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/cli/zipball/ccb7c8125ffe0254fef8f25744bfa376eb7bd0ea", - "reference": "ccb7c8125ffe0254fef8f25744bfa376eb7bd0ea", + "url": "https://api.github.com/repos/utopia-php/cli/zipball/d69bbe51a6a94dc4e5bcdd542b5938038b985a65", + "reference": "d69bbe51a6a94dc4e5bcdd542b5938038b985a65", "shasum": "" }, "require": { @@ -1667,9 +1667,9 @@ ], "support": { "issues": "https://github.com/utopia-php/cli/issues", - "source": "https://github.com/utopia-php/cli/tree/0.15.0" + "source": "https://github.com/utopia-php/cli/tree/0.15.1" }, - "time": "2023-03-01T05:55:14+00:00" + "time": "2024-10-04T13:55:36+00:00" }, { "name": "utopia-php/config", From 5850caff3590fab61fb34bb7f9b0fe4b0475b115 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 7 Oct 2024 11:25:52 +0300 Subject: [PATCH 16/47] debug --- app/controllers/api/projects.php | 14 ++++++++++++++ app/controllers/general.php | 7 +++++++ app/controllers/shared/api.php | 20 ++++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 8073af85b0..a0785131fc 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -88,7 +88,11 @@ App::post('/v1/projects') ->inject('hooks') ->action(function (string $projectId, string $name, string $teamId, string $region, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Request $request, Response $response, Database $dbForConsole, Cache $cache, Group $pools, Hooks $hooks) { + $ctime = time(); + var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before getDocument('teams')"); $team = $dbForConsole->getDocument('teams', $teamId); + $diff = time() - $ctime; + var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After getDocument('teams') : " . $diff . " sec"); if ($team->isEmpty()) { throw new Exception(Exception::TEAM_NOT_FOUND); @@ -154,6 +158,9 @@ App::post('/v1/projects') } try { + + $ctime = time(); + var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before createDocument('projects')"); $project = $dbForConsole->createDocument('projects', new Document([ '$id' => $projectId, '$permissions' => [ @@ -187,6 +194,9 @@ App::post('/v1/projects') 'search' => implode(' ', [$projectId, $name]), 'database' => $dsn, ])); + + $diff = time() - $ctime; + var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After createDocument('projects') : " . $diff . " sec"); } catch (Duplicate) { throw new Exception(Exception::PROJECT_ALREADY_EXISTS); } @@ -238,7 +248,11 @@ App::post('/v1/projects') }, $collection['indexes']); try { + $ctime = time(); + var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before createCollection('".$key."')"); $dbForProject->createCollection($key, $attributes, $indexes); + $diff = time() - $ctime; + var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After createCollection('".$key."') : " . $diff . " sec"); } catch (Duplicate) { // Collection already exists } diff --git a/app/controllers/general.php b/app/controllers/general.php index 8261356d89..4e84a88c34 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -460,6 +460,10 @@ App::init() /* * Appwrite Router */ + + + $ctime = time(); + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before general first init hook"); $host = $request->getHostname() ?? ''; $mainDomain = System::getEnv('_APP_DOMAIN', ''); // Only run Router when external domain @@ -661,6 +665,9 @@ App::init() ) { throw new AppwriteException(AppwriteException::GENERAL_UNKNOWN_ORIGIN, $originValidator->getDescription()); } + + $diff = time() - $ctime; + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After general first init hook : " . $diff . " sec"); }); App::options() diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 13ffbdbb67..fb06e24e77 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -162,6 +162,9 @@ App::init() ->inject('mode') ->inject('team') ->action(function (App $utopia, Request $request, Database $dbForConsole, Document $project, Document $user, ?Document $session, array $servers, string $mode, Document $team) { + $ctime = time(); + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first init hook"); + $route = $utopia->getRoute(); if ($project->isEmpty()) { @@ -344,6 +347,8 @@ App::init() throw new Exception(Exception::USER_MORE_FACTORS_REQUIRED); } } + $diff = time() - $ctime; + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first init hook : " . $diff . " sec"); }); App::init() @@ -363,7 +368,8 @@ App::init() ->inject('dbForProject') ->inject('mode') ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Messaging $queueForMessaging, Audit $queueForAudits, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Usage $queueForUsage, Database $dbForProject, string $mode) use ($databaseListener) { - + $ctime = time(); + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second init hook"); $route = $utopia->getRoute(); if ( @@ -521,6 +527,8 @@ App::init() ; } } + $diff = time() - $ctime; + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second init hook : " . $diff . " sec"); }); App::init() @@ -551,6 +559,8 @@ App::shutdown() ->inject('project') ->inject('dbForProject') ->action(function (App $utopia, Request $request, Response $response, Document $project, Database $dbForProject) { + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first shutdown hook"); + $route = $utopia->getRoute(); $sessionLimit = $project->getAttribute('auths', [])['maxSessions'] ?? APP_LIMIT_USER_SESSIONS_DEFAULT; $session = $response->getPayload(); $userId = $session['userId'] ?? ''; @@ -575,6 +585,8 @@ App::shutdown() } $dbForProject->purgeCachedDocument('users', $userId); + $diff = time() - $ctime; + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first shutdown hook : " . $diff . " sec"); }); App::shutdown() @@ -597,7 +609,9 @@ App::shutdown() ->inject('dbForConsole') ->inject('realtimeConnection') ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Audit $queueForAudits, Usage $queueForUsage, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Messaging $queueForMessaging, Database $dbForProject, Func $queueForFunctions, string $mode, Database $dbForConsole, callable $realtimeConnection) use ($parseLabel) { - + $ctime = time(); + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second shutdown hook"); + $route = $utopia->getRoute(); $responsePayload = $response->getPayload(); if (!empty($queueForEvents->getEvent())) { @@ -800,6 +814,8 @@ App::shutdown() } } } + $diff = time() - $ctime; + var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second shutdown hook : " . $diff . " sec"); }); App::init() From 1cd3e691611eb134fc8ad804c2598efefb868605 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 7 Oct 2024 11:42:14 +0300 Subject: [PATCH 17/47] debug --- app/controllers/api/projects.php | 12 ++++++------ app/controllers/general.php | 4 ++-- app/controllers/shared/api.php | 15 ++++++++------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index a0785131fc..2ddac1394d 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -88,10 +88,10 @@ App::post('/v1/projects') ->inject('hooks') ->action(function (string $projectId, string $name, string $teamId, string $region, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Request $request, Response $response, Database $dbForConsole, Cache $cache, Group $pools, Hooks $hooks) { - $ctime = time(); + $ctime = \microtime(true); var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before getDocument('teams')"); $team = $dbForConsole->getDocument('teams', $teamId); - $diff = time() - $ctime; + $diff = \microtime(true) - $ctime; var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After getDocument('teams') : " . $diff . " sec"); if ($team->isEmpty()) { @@ -159,7 +159,7 @@ App::post('/v1/projects') try { - $ctime = time(); + $ctime = \microtime(true); var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before createDocument('projects')"); $project = $dbForConsole->createDocument('projects', new Document([ '$id' => $projectId, @@ -195,7 +195,7 @@ App::post('/v1/projects') 'database' => $dsn, ])); - $diff = time() - $ctime; + $diff = \microtime(true) - $ctime; var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After createDocument('projects') : " . $diff . " sec"); } catch (Duplicate) { throw new Exception(Exception::PROJECT_ALREADY_EXISTS); @@ -248,10 +248,10 @@ App::post('/v1/projects') }, $collection['indexes']); try { - $ctime = time(); + $ctime = \microtime(true); var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before createCollection('".$key."')"); $dbForProject->createCollection($key, $attributes, $indexes); - $diff = time() - $ctime; + $diff = \microtime(true) - $ctime; var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After createCollection('".$key."') : " . $diff . " sec"); } catch (Duplicate) { // Collection already exists diff --git a/app/controllers/general.php b/app/controllers/general.php index 4e84a88c34..b2d4ad69a1 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -462,7 +462,7 @@ App::init() */ - $ctime = time(); + $ctime = \microtime(true); var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before general first init hook"); $host = $request->getHostname() ?? ''; $mainDomain = System::getEnv('_APP_DOMAIN', ''); @@ -666,7 +666,7 @@ App::init() throw new AppwriteException(AppwriteException::GENERAL_UNKNOWN_ORIGIN, $originValidator->getDescription()); } - $diff = time() - $ctime; + $diff = \microtime(true) - $ctime; var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After general first init hook : " . $diff . " sec"); }); diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index fb06e24e77..f4b38f1eb4 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -162,7 +162,7 @@ App::init() ->inject('mode') ->inject('team') ->action(function (App $utopia, Request $request, Database $dbForConsole, Document $project, Document $user, ?Document $session, array $servers, string $mode, Document $team) { - $ctime = time(); + $ctime = \microtime(true); var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first init hook"); $route = $utopia->getRoute(); @@ -347,7 +347,7 @@ App::init() throw new Exception(Exception::USER_MORE_FACTORS_REQUIRED); } } - $diff = time() - $ctime; + $diff = \microtime(true) - $ctime; var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first init hook : " . $diff . " sec"); }); @@ -368,7 +368,7 @@ App::init() ->inject('dbForProject') ->inject('mode') ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Messaging $queueForMessaging, Audit $queueForAudits, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Usage $queueForUsage, Database $dbForProject, string $mode) use ($databaseListener) { - $ctime = time(); + $ctime = \microtime(true); var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second init hook"); $route = $utopia->getRoute(); @@ -527,7 +527,7 @@ App::init() ; } } - $diff = time() - $ctime; + $diff = \microtime(true) - $ctime; var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second init hook : " . $diff . " sec"); }); @@ -559,6 +559,7 @@ App::shutdown() ->inject('project') ->inject('dbForProject') ->action(function (App $utopia, Request $request, Response $response, Document $project, Database $dbForProject) { + $ctime = \microtime(true); var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first shutdown hook"); $route = $utopia->getRoute(); $sessionLimit = $project->getAttribute('auths', [])['maxSessions'] ?? APP_LIMIT_USER_SESSIONS_DEFAULT; @@ -585,7 +586,7 @@ App::shutdown() } $dbForProject->purgeCachedDocument('users', $userId); - $diff = time() - $ctime; + $diff = \microtime(true) - $ctime; var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first shutdown hook : " . $diff . " sec"); }); @@ -609,7 +610,7 @@ App::shutdown() ->inject('dbForConsole') ->inject('realtimeConnection') ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Audit $queueForAudits, Usage $queueForUsage, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Messaging $queueForMessaging, Database $dbForProject, Func $queueForFunctions, string $mode, Database $dbForConsole, callable $realtimeConnection) use ($parseLabel) { - $ctime = time(); + $ctime = \microtime(true); var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second shutdown hook"); $route = $utopia->getRoute(); $responsePayload = $response->getPayload(); @@ -814,7 +815,7 @@ App::shutdown() } } } - $diff = time() - $ctime; + $diff = \microtime(true) - $ctime; var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second shutdown hook : " . $diff . " sec"); }); From d2c66894fd04f550de68a7b74d72cd34c75e689c Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 7 Oct 2024 17:56:08 +0300 Subject: [PATCH 18/47] debug --- app/controllers/api/projects.php | 12 ++++++------ app/controllers/general.php | 4 ++-- app/controllers/shared/api.php | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 2ddac1394d..31b713da9a 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -89,10 +89,10 @@ App::post('/v1/projects') ->action(function (string $projectId, string $name, string $teamId, string $region, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Request $request, Response $response, Database $dbForConsole, Cache $cache, Group $pools, Hooks $hooks) { $ctime = \microtime(true); - var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before getDocument('teams')"); + var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - Before getDocument('teams')"); $team = $dbForConsole->getDocument('teams', $teamId); $diff = \microtime(true) - $ctime; - var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After getDocument('teams') : " . $diff . " sec"); + var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - After getDocument('teams') : " . $diff . " sec"); if ($team->isEmpty()) { throw new Exception(Exception::TEAM_NOT_FOUND); @@ -160,7 +160,7 @@ App::post('/v1/projects') try { $ctime = \microtime(true); - var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before createDocument('projects')"); + var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - Before createDocument('projects')"); $project = $dbForConsole->createDocument('projects', new Document([ '$id' => $projectId, '$permissions' => [ @@ -196,7 +196,7 @@ App::post('/v1/projects') ])); $diff = \microtime(true) - $ctime; - var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After createDocument('projects') : " . $diff . " sec"); + var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - After createDocument('projects') : " . $diff . " sec"); } catch (Duplicate) { throw new Exception(Exception::PROJECT_ALREADY_EXISTS); } @@ -249,10 +249,10 @@ App::post('/v1/projects') try { $ctime = \microtime(true); - var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - Before createCollection('".$key."')"); + var_dump($request->getURI() ." [".$region."|".System::getEnv('_APP_REGION')."] - Before createCollection('".$key."')"); $dbForProject->createCollection($key, $attributes, $indexes); $diff = \microtime(true) - $ctime; - var_dump("[".$region."|".System::getEnv('_APP_REGION')."] - After createCollection('".$key."') : " . $diff . " sec"); + var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - After createCollection('".$key."') : " . $diff . " sec"); } catch (Duplicate) { // Collection already exists } diff --git a/app/controllers/general.php b/app/controllers/general.php index b2d4ad69a1..1ed164c1e8 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -463,7 +463,7 @@ App::init() $ctime = \microtime(true); - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before general first init hook"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before general first init hook"); $host = $request->getHostname() ?? ''; $mainDomain = System::getEnv('_APP_DOMAIN', ''); // Only run Router when external domain @@ -667,7 +667,7 @@ App::init() } $diff = \microtime(true) - $ctime; - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After general first init hook : " . $diff . " sec"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After general first init hook : " . $diff . " sec"); }); App::options() diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index f4b38f1eb4..0290f7786f 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -163,7 +163,7 @@ App::init() ->inject('team') ->action(function (App $utopia, Request $request, Database $dbForConsole, Document $project, Document $user, ?Document $session, array $servers, string $mode, Document $team) { $ctime = \microtime(true); - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first init hook"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first init hook"); $route = $utopia->getRoute(); @@ -348,7 +348,7 @@ App::init() } } $diff = \microtime(true) - $ctime; - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first init hook : " . $diff . " sec"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first init hook : " . $diff . " sec"); }); App::init() @@ -369,7 +369,7 @@ App::init() ->inject('mode') ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Messaging $queueForMessaging, Audit $queueForAudits, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Usage $queueForUsage, Database $dbForProject, string $mode) use ($databaseListener) { $ctime = \microtime(true); - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second init hook"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second init hook"); $route = $utopia->getRoute(); if ( @@ -528,7 +528,7 @@ App::init() } } $diff = \microtime(true) - $ctime; - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second init hook : " . $diff . " sec"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second init hook : " . $diff . " sec"); }); App::init() @@ -560,7 +560,7 @@ App::shutdown() ->inject('dbForProject') ->action(function (App $utopia, Request $request, Response $response, Document $project, Database $dbForProject) { $ctime = \microtime(true); - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first shutdown hook"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first shutdown hook"); $route = $utopia->getRoute(); $sessionLimit = $project->getAttribute('auths', [])['maxSessions'] ?? APP_LIMIT_USER_SESSIONS_DEFAULT; $session = $response->getPayload(); @@ -587,7 +587,7 @@ App::shutdown() $dbForProject->purgeCachedDocument('users', $userId); $diff = \microtime(true) - $ctime; - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first shutdown hook : " . $diff . " sec"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first shutdown hook : " . $diff . " sec"); }); App::shutdown() @@ -611,7 +611,7 @@ App::shutdown() ->inject('realtimeConnection') ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Audit $queueForAudits, Usage $queueForUsage, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Messaging $queueForMessaging, Database $dbForProject, Func $queueForFunctions, string $mode, Database $dbForConsole, callable $realtimeConnection) use ($parseLabel) { $ctime = \microtime(true); - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second shutdown hook"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second shutdown hook"); $route = $utopia->getRoute(); $responsePayload = $response->getPayload(); @@ -816,7 +816,7 @@ App::shutdown() } } $diff = \microtime(true) - $ctime; - var_dump("[".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second shutdown hook : " . $diff . " sec"); + var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second shutdown hook : " . $diff . " sec"); }); App::init() From 7d7b2c7544f376aa20ce7e4ffceaf7e7c7340bde Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 8 Oct 2024 21:51:43 +0300 Subject: [PATCH 19/47] debug --- app/controllers/api/projects.php | 13 ------------- app/controllers/general.php | 7 +------ app/controllers/shared/api.php | 16 ---------------- 3 files changed, 1 insertion(+), 35 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 31b713da9a..b950ca81fe 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -88,11 +88,7 @@ App::post('/v1/projects') ->inject('hooks') ->action(function (string $projectId, string $name, string $teamId, string $region, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Request $request, Response $response, Database $dbForConsole, Cache $cache, Group $pools, Hooks $hooks) { - $ctime = \microtime(true); - var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - Before getDocument('teams')"); $team = $dbForConsole->getDocument('teams', $teamId); - $diff = \microtime(true) - $ctime; - var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - After getDocument('teams') : " . $diff . " sec"); if ($team->isEmpty()) { throw new Exception(Exception::TEAM_NOT_FOUND); @@ -158,9 +154,6 @@ App::post('/v1/projects') } try { - - $ctime = \microtime(true); - var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - Before createDocument('projects')"); $project = $dbForConsole->createDocument('projects', new Document([ '$id' => $projectId, '$permissions' => [ @@ -195,8 +188,6 @@ App::post('/v1/projects') 'database' => $dsn, ])); - $diff = \microtime(true) - $ctime; - var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - After createDocument('projects') : " . $diff . " sec"); } catch (Duplicate) { throw new Exception(Exception::PROJECT_ALREADY_EXISTS); } @@ -248,11 +239,7 @@ App::post('/v1/projects') }, $collection['indexes']); try { - $ctime = \microtime(true); - var_dump($request->getURI() ." [".$region."|".System::getEnv('_APP_REGION')."] - Before createCollection('".$key."')"); $dbForProject->createCollection($key, $attributes, $indexes); - $diff = \microtime(true) - $ctime; - var_dump($request->getURI() . " [".$region."|".System::getEnv('_APP_REGION')."] - After createCollection('".$key."') : " . $diff . " sec"); } catch (Duplicate) { // Collection already exists } diff --git a/app/controllers/general.php b/app/controllers/general.php index 1ed164c1e8..971881c60d 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -461,10 +461,7 @@ App::init() * Appwrite Router */ - - $ctime = \microtime(true); - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before general first init hook"); - $host = $request->getHostname() ?? ''; + $host = $request->getHostname() ?? ''; $mainDomain = System::getEnv('_APP_DOMAIN', ''); // Only run Router when external domain if ($host !== $mainDomain) { @@ -666,8 +663,6 @@ App::init() throw new AppwriteException(AppwriteException::GENERAL_UNKNOWN_ORIGIN, $originValidator->getDescription()); } - $diff = \microtime(true) - $ctime; - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After general first init hook : " . $diff . " sec"); }); App::options() diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 0290f7786f..c5e4f516d6 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -162,8 +162,6 @@ App::init() ->inject('mode') ->inject('team') ->action(function (App $utopia, Request $request, Database $dbForConsole, Document $project, Document $user, ?Document $session, array $servers, string $mode, Document $team) { - $ctime = \microtime(true); - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first init hook"); $route = $utopia->getRoute(); @@ -347,8 +345,6 @@ App::init() throw new Exception(Exception::USER_MORE_FACTORS_REQUIRED); } } - $diff = \microtime(true) - $ctime; - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first init hook : " . $diff . " sec"); }); App::init() @@ -368,8 +364,6 @@ App::init() ->inject('dbForProject') ->inject('mode') ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Messaging $queueForMessaging, Audit $queueForAudits, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Usage $queueForUsage, Database $dbForProject, string $mode) use ($databaseListener) { - $ctime = \microtime(true); - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second init hook"); $route = $utopia->getRoute(); if ( @@ -527,8 +521,6 @@ App::init() ; } } - $diff = \microtime(true) - $ctime; - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second init hook : " . $diff . " sec"); }); App::init() @@ -559,8 +551,6 @@ App::shutdown() ->inject('project') ->inject('dbForProject') ->action(function (App $utopia, Request $request, Response $response, Document $project, Database $dbForProject) { - $ctime = \microtime(true); - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api first shutdown hook"); $route = $utopia->getRoute(); $sessionLimit = $project->getAttribute('auths', [])['maxSessions'] ?? APP_LIMIT_USER_SESSIONS_DEFAULT; $session = $response->getPayload(); @@ -586,8 +576,6 @@ App::shutdown() } $dbForProject->purgeCachedDocument('users', $userId); - $diff = \microtime(true) - $ctime; - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api first shutdown hook : " . $diff . " sec"); }); App::shutdown() @@ -610,8 +598,6 @@ App::shutdown() ->inject('dbForConsole') ->inject('realtimeConnection') ->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $queueForEvents, Audit $queueForAudits, Usage $queueForUsage, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Messaging $queueForMessaging, Database $dbForProject, Func $queueForFunctions, string $mode, Database $dbForConsole, callable $realtimeConnection) use ($parseLabel) { - $ctime = \microtime(true); - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - Before shared api second shutdown hook"); $route = $utopia->getRoute(); $responsePayload = $response->getPayload(); @@ -815,8 +801,6 @@ App::shutdown() } } } - $diff = \microtime(true) - $ctime; - var_dump($request->getURI() . " [".$project->getAttribute('region')."|".System::getEnv('_APP_REGION')."] - After shared api second shutdown hook : " . $diff . " sec"); }); App::init() From 16fe23244b4b1d726751daa425cda6a22078ec84 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 9 Oct 2024 20:13:28 +0300 Subject: [PATCH 20/47] builds metric reduce --- app/controllers/shared/api.php | 8 +++++- composer.lock | 48 +++++++++++++++++----------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index c5e4f516d6..9631d9b09d 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -469,7 +469,6 @@ App::init() ); $timestamp = 60 * 60 * 24 * 30; $data = $cache->load($key, $timestamp); - if (!empty($data) && !$cacheLog->isEmpty()) { $parts = explode('/', $cacheLog->getAttribute('resourceType')); $type = $parts[0] ?? null; @@ -712,9 +711,13 @@ App::shutdown() * Cache label */ $useCache = $route->getLabel('cache', false); + var_dump('$useCache='); + var_dump($useCache); if ($useCache) { $resource = $resourceType = null; $data = $response->getPayload(); + var_dump('!empty($data[payload]='); + var_dump(!empty($data['payload'])); if (!empty($data['payload'])) { $pattern = $route->getLabel('cache.resource', null); if (!empty($pattern)) { @@ -731,7 +734,10 @@ App::shutdown() $cacheLog = Authorization::skip(fn () => $dbForProject->getDocument('cache', $key)); $accessedAt = $cacheLog->getAttribute('accessedAt', ''); $now = DateTime::now(); + var_dump('$cacheLog='); + var_dump($cacheLog); if ($cacheLog->isEmpty()) { + var_dump('inserting.....'); Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([ '$id' => $key, 'resource' => $resource, diff --git a/composer.lock b/composer.lock index 275fd9dbb9..c7ef692624 100644 --- a/composer.lock +++ b/composer.lock @@ -2124,16 +2124,16 @@ }, { "name": "utopia-php/messaging", - "version": "0.12.0", + "version": "0.12.1", "source": { "type": "git", "url": "https://github.com/utopia-php/messaging.git", - "reference": "6e466d3511981291843c6ebf9ce3f44fc75e37b0" + "reference": "b9dfafb5efc1d12cbee01d03dc98853ef026e35b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/messaging/zipball/6e466d3511981291843c6ebf9ce3f44fc75e37b0", - "reference": "6e466d3511981291843c6ebf9ce3f44fc75e37b0", + "url": "https://api.github.com/repos/utopia-php/messaging/zipball/b9dfafb5efc1d12cbee01d03dc98853ef026e35b", + "reference": "b9dfafb5efc1d12cbee01d03dc98853ef026e35b", "shasum": "" }, "require": { @@ -2169,22 +2169,22 @@ ], "support": { "issues": "https://github.com/utopia-php/messaging/issues", - "source": "https://github.com/utopia-php/messaging/tree/0.12.0" + "source": "https://github.com/utopia-php/messaging/tree/0.12.1" }, - "time": "2024-05-30T14:58:25+00:00" + "time": "2024-10-09T08:17:07+00:00" }, { "name": "utopia-php/migration", - "version": "0.6.4", + "version": "0.6.7", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "e43ef283f1386084e11d1ffe093fb6c6d7a6ce6c" + "reference": "9f2d1afc2acf2b6cdc5088ad4becbab131df0f2a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/e43ef283f1386084e11d1ffe093fb6c6d7a6ce6c", - "reference": "e43ef283f1386084e11d1ffe093fb6c6d7a6ce6c", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/9f2d1afc2acf2b6cdc5088ad4becbab131df0f2a", + "reference": "9f2d1afc2acf2b6cdc5088ad4becbab131df0f2a", "shasum": "" }, "require": { @@ -2225,9 +2225,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/0.6.4" + "source": "https://github.com/utopia-php/migration/tree/0.6.7" }, - "time": "2024-10-02T15:16:36+00:00" + "time": "2024-10-09T10:21:25+00:00" }, { "name": "utopia-php/mongo", @@ -3002,16 +3002,16 @@ "packages-dev": [ { "name": "appwrite/sdk-generator", - "version": "0.39.22", + "version": "0.39.23", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator.git", - "reference": "bdbb1607527550e67283ff0533522d1410c2c0df" + "reference": "0acceabb7593c9c07c5db85a84a5ebac60896763" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/bdbb1607527550e67283ff0533522d1410c2c0df", - "reference": "bdbb1607527550e67283ff0533522d1410c2c0df", + "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/0acceabb7593c9c07c5db85a84a5ebac60896763", + "reference": "0acceabb7593c9c07c5db85a84a5ebac60896763", "shasum": "" }, "require": { @@ -3047,9 +3047,9 @@ "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", "support": { "issues": "https://github.com/appwrite/sdk-generator/issues", - "source": "https://github.com/appwrite/sdk-generator/tree/0.39.22" + "source": "https://github.com/appwrite/sdk-generator/tree/0.39.23" }, - "time": "2024-10-01T16:16:26+00:00" + "time": "2024-10-08T00:38:57+00:00" }, { "name": "doctrine/annotations", @@ -3573,16 +3573,16 @@ }, { "name": "nikic/php-parser", - "version": "v5.3.0", + "version": "v5.3.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "3abf7425cd284141dc5d8d14a9ee444de3345d1a" + "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/3abf7425cd284141dc5d8d14a9ee444de3345d1a", - "reference": "3abf7425cd284141dc5d8d14a9ee444de3345d1a", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/8eea230464783aa9671db8eea6f8c6ac5285794b", + "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b", "shasum": "" }, "require": { @@ -3625,9 +3625,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.1" }, - "time": "2024-09-29T13:56:26+00:00" + "time": "2024-10-08T18:51:32+00:00" }, { "name": "phar-io/manifest", From cffbc77d053a1041e414ef0a1abe97a0f91c04d7 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 10 Oct 2024 11:05:54 +0300 Subject: [PATCH 21/47] update file preview cache save flow --- app/controllers/shared/api.php | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 9631d9b09d..229d2e3588 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -461,6 +461,7 @@ App::init() ->on(Database::EVENT_DOCUMENT_DELETE, 'calculate-usage', fn ($event, $document) => $databaseListener($event, $document, $project, $queueForUsage, $dbForProject)); $useCache = $route->getLabel('cache', false); + if ($useCache) { $key = md5($request->getURI() . '*' . implode('*', $request->getParams()) . '*' . APP_CACHE_BUSTER); $cacheLog = Authorization::skip(fn () => $dbForProject->getDocument('cache', $key)); @@ -711,13 +712,11 @@ App::shutdown() * Cache label */ $useCache = $route->getLabel('cache', false); - var_dump('$useCache='); - var_dump($useCache); + if ($useCache) { $resource = $resourceType = null; $data = $response->getPayload(); - var_dump('!empty($data[payload]='); - var_dump(!empty($data['payload'])); + if (!empty($data['payload'])) { $pattern = $route->getLabel('cache.resource', null); if (!empty($pattern)) { @@ -734,10 +733,8 @@ App::shutdown() $cacheLog = Authorization::skip(fn () => $dbForProject->getDocument('cache', $key)); $accessedAt = $cacheLog->getAttribute('accessedAt', ''); $now = DateTime::now(); - var_dump('$cacheLog='); - var_dump($cacheLog); + if ($cacheLog->isEmpty()) { - var_dump('inserting.....'); Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([ '$id' => $key, 'resource' => $resource, @@ -751,17 +748,17 @@ App::shutdown() Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog)); } - if ($signature !== $cacheLog->getAttribute('signature')) { - $cache = new Cache( - new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) - ); + $cache = new Cache( + new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) + ); + $timestamp = 60 * 60 * 24 * 30; + $data = $cache->load($key, $timestamp); + if ($signature !== $cacheLog->getAttribute('signature') || empty($data)) { $cache->save($key, $data['payload']); } } } - - if ($project->getId() !== 'console') { if (!Auth::isPrivilegedUser(Authorization::getRoles())) { $fileSize = 0; From d477de256a2b99e9494efcf39472655a3aef3f77 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 10 Oct 2024 11:40:13 +0300 Subject: [PATCH 22/47] update file preview cache save flow --- app/controllers/shared/api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 229d2e3588..1aee73d021 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -751,6 +751,7 @@ App::shutdown() $cache = new Cache( new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) ); + $timestamp = 60 * 60 * 24 * 30; $data = $cache->load($key, $timestamp); if ($signature !== $cacheLog->getAttribute('signature') || empty($data)) { From 74ecc20751ab58e38787fb6a44dcfbdb0d79a091 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 10 Oct 2024 12:07:53 +0300 Subject: [PATCH 23/47] debug --- app/controllers/shared/api.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 1aee73d021..43a86b837d 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -754,6 +754,8 @@ App::shutdown() $timestamp = 60 * 60 * 24 * 30; $data = $cache->load($key, $timestamp); + var_dump($cacheLog->getAttribute('signature')); + var_dump(empty($data)); if ($signature !== $cacheLog->getAttribute('signature') || empty($data)) { $cache->save($key, $data['payload']); } From 7921bd62d9313dccaac0206bdb2dcad6921033b7 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 10 Oct 2024 12:11:35 +0300 Subject: [PATCH 24/47] debug --- app/controllers/shared/api.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 43a86b837d..8ddb9c61f7 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -753,10 +753,8 @@ App::shutdown() ); $timestamp = 60 * 60 * 24 * 30; - $data = $cache->load($key, $timestamp); - var_dump($cacheLog->getAttribute('signature')); - var_dump(empty($data)); - if ($signature !== $cacheLog->getAttribute('signature') || empty($data)) { + $cacheFile = $cache->load($key, $timestamp); + if ($signature !== $cacheLog->getAttribute('signature') || empty($cacheFile)) { $cache->save($key, $data['payload']); } } From 36e4c8c399c3c61a5ea03a14ad9854b466f68e3e Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 10 Oct 2024 12:53:45 +0300 Subject: [PATCH 25/47] debug --- app/controllers/shared/api.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 8ddb9c61f7..952ab8b49e 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -471,6 +471,7 @@ App::init() $timestamp = 60 * 60 * 24 * 30; $data = $cache->load($key, $timestamp); if (!empty($data) && !$cacheLog->isEmpty()) { + $timerStart = \microtime(true); $parts = explode('/', $cacheLog->getAttribute('resourceType')); $type = $parts[0] ?? null; @@ -506,7 +507,8 @@ App::init() throw new Exception(Exception::STORAGE_FILE_NOT_FOUND); } } - + $timerEnd= \microtime(true); + var_dump('Timer: ' . $timerEnd - $timerStart); $response ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT') ->addHeader('X-Appwrite-Cache', 'hit') From 051d6e580cb16b0f27b7a16b442815425d687cb3 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 13 Oct 2024 18:15:11 +0300 Subject: [PATCH 26/47] debug --- app/worker.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/worker.php b/app/worker.php index df8a5b8c7a..ee3349f1c8 100644 --- a/app/worker.php +++ b/app/worker.php @@ -268,9 +268,9 @@ Server::setResource('deviceForBuilds', function (Document $project, $connectionS return getDevice(APP_STORAGE_BUILDS.'/app-'.$project->getId(), $connectionString); }, ['project', 'connectionString']); -Server::setResource('deviceForCache', function (Document $project, $connectionString) { - return getDevice(APP_STORAGE_CACHE.'/app-'.$project->getId(), $connectionString); -}, ['project', 'connectionString']); +Server::setResource('deviceForCache', function (Document $project) { + return getDevice(APP_STORAGE_CACHE.'/app-'.$project->getId()); +}, ['project']); Server::setResource('realtimeConnection',function ($pools) { return function () use ($pools) { From d0481482ae5b45a7fe8dd7de26189e5d86b72a7a Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 14 Oct 2024 10:36:52 +0300 Subject: [PATCH 27/47] debug --- app/init.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/init.php b/app/init.php index a10bdd6896..041dfad75b 100644 --- a/app/init.php +++ b/app/init.php @@ -945,7 +945,9 @@ $register->set('pools', function () { }, default => throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Invalid scheme'), }; - + var_dump($poolSize); + $poolSize = $poolSize/8; + var_dump($poolSize); $pool = new Pool($name, $poolSize, function () use ($type, $resource, $dsn) { // Get Adapter switch ($type) { From 1373d62c5215e369fca4f2f36ba16bd2e41af1ef Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 14 Oct 2024 16:18:01 +0300 Subject: [PATCH 28/47] debug --- app/init.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/init.php b/app/init.php index 041dfad75b..a10bdd6896 100644 --- a/app/init.php +++ b/app/init.php @@ -945,9 +945,7 @@ $register->set('pools', function () { }, default => throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Invalid scheme'), }; - var_dump($poolSize); - $poolSize = $poolSize/8; - var_dump($poolSize); + $pool = new Pool($name, $poolSize, function () use ($type, $resource, $dsn) { // Get Adapter switch ($type) { From a80b9219276c5cc86e09e73855b13fa6aa4f50b1 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 14 Oct 2024 16:21:23 +0300 Subject: [PATCH 29/47] debug --- app/controllers/shared/api.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 952ab8b49e..27504d33ab 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -507,8 +507,7 @@ App::init() throw new Exception(Exception::STORAGE_FILE_NOT_FOUND); } } - $timerEnd= \microtime(true); - var_dump('Timer: ' . $timerEnd - $timerStart); + $response ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT') ->addHeader('X-Appwrite-Cache', 'hit') From 5451a9cd23c2394e26f574f6a68645fd6d3b04ee Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 10:56:44 +0300 Subject: [PATCH 30/47] sync against feat-migration --- app/controllers/api/databases.php | 8 +-- app/controllers/api/projects.php | 62 ++++++++--------- app/http.php | 5 +- app/init.php | 12 ++-- app/realtime.php | 6 +- app/worker.php | 18 +++-- composer.json | 2 +- composer.lock | 76 ++++++++++++--------- src/Appwrite/Platform/Workers/Databases.php | 45 ++---------- src/Appwrite/Platform/Workers/Deletes.php | 32 ++------- src/Appwrite/Utopia/Response/Model/Func.php | 2 +- 11 files changed, 117 insertions(+), 151 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 9c110647af..e1ad74acc6 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -3,7 +3,6 @@ use Appwrite\Auth\Auth; use Appwrite\Detector\Detector; use Appwrite\Event\Database as EventDatabase; -use Appwrite\Event\Delete; use Appwrite\Event\Event; use Appwrite\Event\Usage; use Appwrite\Extend\Exception; @@ -3656,11 +3655,10 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu ->inject('requestTimestamp') ->inject('response') ->inject('dbForProject') - ->inject('queueForDeletes') ->inject('queueForEvents') ->inject('queueForUsage') ->inject('mode') - ->action(function (string $databaseId, string $collectionId, string $documentId, ?\DateTime $requestTimestamp, Response $response, Database $dbForProject, Delete $queueForDeletes, Event $queueForEvents, Usage $queueForUsage, string $mode) { + ->action(function (string $databaseId, string $collectionId, string $documentId, ?\DateTime $requestTimestamp, Response $response, Database $dbForProject, Event $queueForEvents, Usage $queueForUsage, string $mode) { $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); $isAPIKey = Auth::isAppUser(Authorization::getRoles()); @@ -3733,10 +3731,6 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu ) ); - $queueForDeletes - ->setType(DELETE_TYPE_AUDIT) - ->setDocument($document); - $queueForEvents ->setParam('databaseId', $databaseId) ->setParam('collectionId', $collection->getId()) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index b950ca81fe..77eed678d9 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -118,6 +118,10 @@ App::post('/v1/projects') $projectId = ($projectId == 'unique()') ? ID::unique() : $projectId; + if ($projectId === 'console') { + throw new Exception(Exception::PROJECT_RESERVED_PROJECT, "'console' is a reserved project."); + } + $databases = Config::getParam('pools-database', []); $databaseOverride = System::getEnv('_APP_DATABASE_OVERRIDE'); @@ -135,14 +139,10 @@ App::post('/v1/projects') $dsn = $databases[array_rand($databases)]; } - if ($projectId === 'console') { - throw new Exception(Exception::PROJECT_RESERVED_PROJECT, "'console' is a reserved project."); - } - // TODO: Temporary until all projects are using shared tables. - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (in_array($dsn, $sharedTablesKeys)) { + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + if (\in_array($dsn, $sharedTables)) { $schema = 'appwrite'; $database = 'appwrite'; $namespace = System::getEnv('_APP_DATABASE_SHARED_NAMESPACE', ''); @@ -187,7 +187,6 @@ App::post('/v1/projects') 'search' => implode(' ', [$projectId, $name]), 'database' => $dsn, ])); - } catch (Duplicate) { throw new Exception(Exception::PROJECT_ALREADY_EXISTS); } @@ -201,11 +200,14 @@ App::post('/v1/projects') $adapter = $pools->get($dsn->getHost())->pop()->getResource(); $dbForProject = new Database($adapter, $cache); + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); + $globalCollections = !\in_array($dsn->getHost(), $sharedTablesV1); - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (in_array($dsn->getHost(), $sharedTablesKeys)) { $dbForProject + if (\in_array($dsn->getHost(), $sharedTables)) { + $dbForProject ->setSharedTables(true) - ->setTenant($project->getInternalId()) + ->setTenant($globalCollections ? null : $project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); } else { $dbForProject @@ -214,34 +216,32 @@ App::post('/v1/projects') ->setNamespace('_' . $project->getInternalId()); } - $dbForProject->create(); + $create = true; - $audit = new Audit($dbForProject); - $audit->setup(); + try { + $dbForProject->create(); + } catch (Duplicate) { + $create = false; + } - $abuse = new TimeLimit('', 0, 1, $dbForProject); - $abuse->setup(); + if ($create || !$globalCollections) { + $audit = new Audit($dbForProject); + $audit->setup(); - /** @var array $collections */ - $collections = Config::getParam('collections', [])['projects'] ?? []; + $abuse = new TimeLimit('', 0, 1, $dbForProject); + $abuse->setup(); - foreach ($collections as $key => $collection) { - if (($collection['$collection'] ?? '') !== Database::METADATA) { - continue; - } + /** @var array $collections */ + $collections = Config::getParam('collections', [])['projects'] ?? []; - $attributes = \array_map(function (array $attribute) { - return new Document($attribute); - }, $collection['attributes']); + foreach ($collections as $key => $collection) { + if (($collection['$collection'] ?? '') !== Database::METADATA) { + continue; + } - $indexes = \array_map(function (array $index) { - return new Document($index); - }, $collection['indexes']); - - try { + $attributes = \array_map(fn ($attribute) => new Document($attribute), $collection['attributes']); + $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']); $dbForProject->createCollection($key, $attributes, $indexes); - } catch (Duplicate) { - // Collection already exists } } diff --git a/app/http.php b/app/http.php index bec772c770..9754263046 100644 --- a/app/http.php +++ b/app/http.php @@ -16,6 +16,7 @@ use Utopia\CLI\Console; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Exception\Duplicate; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -92,7 +93,7 @@ $http->on(Constant::EVENT_START, function (Server $http) use ($payloadSize, $reg try { Console::success('[Setup] - Creating database: appwrite...'); $dbForConsole->create(); - } catch (\Throwable $e) { + } catch (Duplicate) { Console::success('[Setup] - Skip: metadata table already exists'); } @@ -224,7 +225,7 @@ $http->on(Constant::EVENT_START, function (Server $http) use ($payloadSize, $reg }); }); -$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) { +$http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) { App::setResource('swooleRequest', fn () => $swooleRequest); App::setResource('swooleResponse', fn () => $swooleResponse); diff --git a/app/init.php b/app/init.php index a10bdd6896..e18b5102b4 100644 --- a/app/init.php +++ b/app/init.php @@ -1407,8 +1407,10 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForConsole, $dsn = new DSN('mysql://' . $project->getAttribute('database')); } - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + + if (\in_array($dsn->getHost(), $sharedTables)) { + $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -1460,8 +1462,10 @@ App::setResource('getProjectDB', function (Group $pools, Database $dbForConsole, ->setMetadata('project', $project->getId()) ->setTimeout(APP_DATABASE_TIMEOUT_MILLISECONDS); - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + + if (\in_array($dsn->getHost(), $sharedTables)) { + $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); diff --git a/app/realtime.php b/app/realtime.php index 00633b7f9c..6ca4639f85 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -92,8 +92,10 @@ if (!function_exists('getProjectDB')) { $database = new Database($adapter, getCache()); - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + + if (\in_array($dsn->getHost(), $sharedTables)) { + $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); diff --git a/app/worker.php b/app/worker.php index ee3349f1c8..0e76338e64 100644 --- a/app/worker.php +++ b/app/worker.php @@ -93,8 +93,10 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register, $dsn = new DSN('mysql://' . $project->getAttribute('database')); } - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + + if (\in_array($dsn->getHost(), $sharedTables)) { + $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -126,8 +128,10 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso if (isset($databases[$dsn->getHost()])) { $database = $databases[$dsn->getHost()]; - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + + if (\in_array($dsn->getHost(), $sharedTables)) { + $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); @@ -150,8 +154,10 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso $databases[$dsn->getHost()] = $database; - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - if (in_array($dsn->getHost(), $sharedTablesKeys)) { $database + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + + if (\in_array($dsn->getHost(), $sharedTables)) { + $database ->setSharedTables(true) ->setTenant($project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); diff --git a/composer.json b/composer.json index c176d383f3..10f2a12492 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/cache": "0.10.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "0.53.5", + "utopia-php/database": "dev-feat-migrations as 0.53.6", "utopia-php/domains": "0.5.*", "utopia-php/dsn": "0.2.1", "utopia-php/framework": "0.33.*", diff --git a/composer.lock b/composer.lock index c7ef692624..bc2b3fb5ca 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1884e3a2966762c4a955842426b64f6c", + "content-hash": "5cb1524d4677594554ab26d05b3c38e6", "packages": [ { "name": "adhocore/jwt", @@ -1724,16 +1724,16 @@ }, { "name": "utopia-php/database", - "version": "0.53.5", + "version": "dev-feat-migrations", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "689ba22063bf46def385da8695ba7a921e81e38d" + "reference": "efd0f4e8fb5d158486aec0e6fea9561facf0daad" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/689ba22063bf46def385da8695ba7a921e81e38d", - "reference": "689ba22063bf46def385da8695ba7a921e81e38d", + "url": "https://api.github.com/repos/utopia-php/database/zipball/efd0f4e8fb5d158486aec0e6fea9561facf0daad", + "reference": "efd0f4e8fb5d158486aec0e6fea9561facf0daad", "shasum": "" }, "require": { @@ -1774,9 +1774,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.53.5" + "source": "https://github.com/utopia-php/database/tree/feat-migrations" }, - "time": "2024-09-24T08:43:10+00:00" + "time": "2024-10-15T05:50:09+00:00" }, { "name": "utopia-php/domains", @@ -2070,16 +2070,16 @@ }, { "name": "utopia-php/logger", - "version": "0.6.1", + "version": "0.6.2", "source": { "type": "git", "url": "https://github.com/utopia-php/logger.git", - "reference": "7e8ff512c6f04577aba1df67c7b9628971946f9c" + "reference": "25b5bd2ad8bb51292f76332faa7034644fd0941d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/logger/zipball/7e8ff512c6f04577aba1df67c7b9628971946f9c", - "reference": "7e8ff512c6f04577aba1df67c7b9628971946f9c", + "url": "https://api.github.com/repos/utopia-php/logger/zipball/25b5bd2ad8bb51292f76332faa7034644fd0941d", + "reference": "25b5bd2ad8bb51292f76332faa7034644fd0941d", "shasum": "" }, "require": { @@ -2118,9 +2118,9 @@ ], "support": { "issues": "https://github.com/utopia-php/logger/issues", - "source": "https://github.com/utopia-php/logger/tree/0.6.1" + "source": "https://github.com/utopia-php/logger/tree/0.6.2" }, - "time": "2024-09-20T14:02:12+00:00" + "time": "2024-10-14T16:02:49+00:00" }, { "name": "utopia-php/messaging", @@ -2175,16 +2175,16 @@ }, { "name": "utopia-php/migration", - "version": "0.6.7", + "version": "0.6.9", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "9f2d1afc2acf2b6cdc5088ad4becbab131df0f2a" + "reference": "ce97cdf2ca82e7cec78e2ed484ef2c71ebe8744b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/9f2d1afc2acf2b6cdc5088ad4becbab131df0f2a", - "reference": "9f2d1afc2acf2b6cdc5088ad4becbab131df0f2a", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/ce97cdf2ca82e7cec78e2ed484ef2c71ebe8744b", + "reference": "ce97cdf2ca82e7cec78e2ed484ef2c71ebe8744b", "shasum": "" }, "require": { @@ -2225,9 +2225,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/0.6.7" + "source": "https://github.com/utopia-php/migration/tree/0.6.9" }, - "time": "2024-10-09T10:21:25+00:00" + "time": "2024-10-16T08:33:21+00:00" }, { "name": "utopia-php/mongo", @@ -3002,16 +3002,16 @@ "packages-dev": [ { "name": "appwrite/sdk-generator", - "version": "0.39.23", + "version": "0.39.24", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator.git", - "reference": "0acceabb7593c9c07c5db85a84a5ebac60896763" + "reference": "412451c87f6ef17e24e9a5cf41721043d74c60c8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/0acceabb7593c9c07c5db85a84a5ebac60896763", - "reference": "0acceabb7593c9c07c5db85a84a5ebac60896763", + "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/412451c87f6ef17e24e9a5cf41721043d74c60c8", + "reference": "412451c87f6ef17e24e9a5cf41721043d74c60c8", "shasum": "" }, "require": { @@ -3047,9 +3047,9 @@ "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", "support": { "issues": "https://github.com/appwrite/sdk-generator/issues", - "source": "https://github.com/appwrite/sdk-generator/tree/0.39.23" + "source": "https://github.com/appwrite/sdk-generator/tree/0.39.24" }, - "time": "2024-10-08T00:38:57+00:00" + "time": "2024-10-09T19:13:27+00:00" }, { "name": "doctrine/annotations", @@ -3847,6 +3847,7 @@ "issues": "https://github.com/phpbench/dom/issues", "source": "https://github.com/phpbench/dom/tree/0.3.3" }, + "abandoned": true, "time": "2023-03-06T23:46:57+00:00" }, { @@ -4194,16 +4195,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.32.0", + "version": "1.33.0", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "6ca22b154efdd9e3c68c56f5d94670920a1c19a4" + "reference": "82a311fd3690fb2bf7b64d5c98f912b3dd746140" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/6ca22b154efdd9e3c68c56f5d94670920a1c19a4", - "reference": "6ca22b154efdd9e3c68c56f5d94670920a1c19a4", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/82a311fd3690fb2bf7b64d5c98f912b3dd746140", + "reference": "82a311fd3690fb2bf7b64d5c98f912b3dd746140", "shasum": "" }, "require": { @@ -4235,9 +4236,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.32.0" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.33.0" }, - "time": "2024-09-26T07:23:32+00:00" + "time": "2024-10-13T11:25:22+00:00" }, { "name": "phpunit/php-code-coverage", @@ -7002,9 +7003,18 @@ "time": "2024-03-07T20:33:40+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "utopia-php/database", + "version": "dev-feat-migrations", + "alias": "0.53.6", + "alias_normalized": "0.53.6.0" + } + ], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "utopia-php/database": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/Appwrite/Platform/Workers/Databases.php b/src/Appwrite/Platform/Workers/Databases.php index 7f3132dcb0..c9023318c7 100644 --- a/src/Appwrite/Platform/Workers/Databases.php +++ b/src/Appwrite/Platform/Workers/Databases.php @@ -5,7 +5,6 @@ namespace Appwrite\Platform\Workers; use Appwrite\Event\Event; use Appwrite\Messaging\Adapter\Realtime; use Exception; -use Utopia\Audit\Audit; use Utopia\CLI\Console; use Utopia\Database\Database; use Utopia\Database\Document; @@ -53,12 +52,7 @@ class Databases extends Action * @param Log $log * @param callable $realtimeConnection * @return void - * @throws Authorization - * @throws Conflict - * @throws DatabaseException - * @throws Restricted - * @throws Structure - * @throws Exception + * @throws \Exception */ public function action(Message $message, Database $dbForConsole, Database $dbForProject, Log $log, callable $realtimeConnection): void { @@ -106,9 +100,7 @@ class Databases extends Action * @return void * @throws Authorization * @throws Conflict - * @throws DatabaseException - * @throws Structure - * @throws Exception + * @throws \Exception */ private function createAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject, callable $realtimeConnection): void { @@ -151,7 +143,6 @@ class Databases extends Action $options = $attribute->getAttribute('options', []); $project = $dbForConsole->getDocument('projects', $projectId); - try { switch ($type) { case Database::VAR_RELATIONSHIP: @@ -232,12 +223,9 @@ class Databases extends Action * @return void * @throws Authorization * @throws Conflict - * @throws DatabaseException - * @throws Restricted - * @throws Structure - * @throws Exception - */ - private function deleteAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject, callable $realtimeConnection): void + * @throws \Exception + **/ + private function deleteAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject): void { if ($collection->isEmpty()) { throw new Exception('Missing collection'); @@ -390,9 +378,8 @@ class Databases extends Action * @return void * @throws Authorization * @throws Conflict - * @throws DatabaseException * @throws Structure - * @throws Exception + * @throws DatabaseException */ private function createIndex(Document $database, Document $collection, Document $index, Document $project, Database $dbForConsole, Database $dbForProject, callable $realtimeConnection): void { @@ -453,9 +440,8 @@ class Databases extends Action * @return void * @throws Authorization * @throws Conflict - * @throws DatabaseException * @throws Structure - * @throws Exception + * @throws DatabaseException */ private function deleteIndex(Document $database, Document $collection, Document $index, Document $project, Database $dbForConsole, Database $dbForProject, callable $realtimeConnection): void { @@ -516,8 +502,6 @@ class Databases extends Action }); $dbForProject->deleteCollection('database_' . $database->getInternalId()); - - $this->deleteAuditLogsByResource('database/' . $database->getId(), $project, $dbForProject); } /** @@ -573,23 +557,8 @@ class Databases extends Action Query::equal('databaseInternalId', [$databaseInternalId]), Query::equal('collectionInternalId', [$collectionInternalId]) ], $dbForProject); - - $this->deleteAuditLogsByResource('database/' . $databaseId . '/collection/' . $collectionId, $project, $dbForProject); } - /** - * @param string $resource - * @param Document $project - * @param Database $dbForProject - * @return void - * @throws Exception - */ - protected function deleteAuditLogsByResource(string $resource, Document $project, Database $dbForProject): void - { - $this->deleteByGroup(Audit::COLLECTION, [ - Query::equal('resource', [$resource]) - ], $dbForProject); - } /** * @param string $collection collectionID diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 9ea8133cf5..700a480065 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -119,10 +119,6 @@ class Deletes extends Action if (!$project->isEmpty()) { $this->deleteAuditLogs($project, $getProjectDB, $auditRetention); } - - if (!$document->isEmpty()) { - $this->deleteAuditLogsByResource($getProjectDB, 'document/' . $document->getId(), $project); - } break; case DELETE_TYPE_ABUSE: $this->deleteAbuseLogs($project, $getProjectDB, $abuseRetention); @@ -498,13 +494,13 @@ class Deletes extends Action ]; $limit = \count($projectCollectionIds) + 25; - $sharedTablesKeys = explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); + $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); while (true) { $collections = $dbForProject->listCollections($limit); foreach ($collections as $collection) { - if (! in_array($dsn->getHost(), $sharedTablesKeys) || !\in_array($collection->getId(), $projectCollectionIds)) { + if (\in_array($dsn->getHost(), $sharedTables) || !\in_array($collection->getId(), $projectCollectionIds)) { try { $dbForProject->deleteCollection($collection->getId()); } catch (Throwable $e) { @@ -516,12 +512,13 @@ class Deletes extends Action if (!preg_match('/^_\d+_\d+$/', $collection->getId())) { throw $e; } - } } else { + } + } else { $this->deleteByGroup($collection->getId(), [], database: $dbForProject); } } - if (in_array($dsn->getHost(), $sharedTablesKeys)) { + if (\in_array($dsn->getHost(), $sharedTables)) { $collectionsIds = \array_map(fn ($collection) => $collection->getId(), $collections); if (empty(\array_diff($collectionsIds, $projectCollectionIds))) { @@ -575,8 +572,7 @@ class Deletes extends Action ], $dbForConsole); // Delete metadata table - System::getEnv('_APP_DATABASE_SHARED_TABLES', ''); - if (! in_array($dsn, $sharedTablesKeys)) { + if (\in_array($dsn->getHost(), $sharedTables)) { $dbForProject->deleteCollection('_metadata'); } else { $this->deleteByGroup('_metadata', [], $dbForProject); @@ -734,22 +730,6 @@ class Deletes extends Action } } - /** - * @param callable $getProjectDB - * @param string $resource - * @param Document $project - * @return void - * @throws Exception - */ - private function deleteAuditLogsByResource(callable $getProjectDB, string $resource, Document $project): void - { - $dbForProject = $getProjectDB($project); - - $this->deleteByGroup(Audit::COLLECTION, [ - Query::equal('resource', [$resource]) - ], $dbForProject); - } - /** * @param callable $getProjectDB * @param Device $deviceForFunctions diff --git a/src/Appwrite/Utopia/Response/Model/Func.php b/src/Appwrite/Utopia/Response/Model/Func.php index f4ff214d0b..ab2d7ba051 100644 --- a/src/Appwrite/Utopia/Response/Model/Func.php +++ b/src/Appwrite/Utopia/Response/Model/Func.php @@ -94,7 +94,7 @@ class Func extends Model ]) ->addRule('schedule', [ 'type' => self::TYPE_STRING, - 'description' => 'Function execution schedult in CRON format.', + 'description' => 'Function execution schedule in CRON format.', 'default' => '', 'example' => '5 4 * * *', ]) From 9bfab69062dc3e3eb01506aa4bbe535999c02fb9 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 15:33:10 +0300 Subject: [PATCH 31/47] debug --- app/controllers/api/projects.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 77eed678d9..a21aa37be6 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -203,7 +203,6 @@ App::post('/v1/projects') $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); $globalCollections = !\in_array($dsn->getHost(), $sharedTablesV1); - if (\in_array($dsn->getHost(), $sharedTables)) { $dbForProject ->setSharedTables(true) @@ -224,6 +223,11 @@ App::post('/v1/projects') $create = false; } + var_dump($dsn->getHost()); + var_dump($sharedTablesV1); + var_dump($globalCollections); + var_dump($create); + if ($create || !$globalCollections) { $audit = new Audit($dbForProject); $audit->setup(); From bcb593e6df8c80501ae182b60daf345c4e61d77b Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 15:56:43 +0300 Subject: [PATCH 32/47] debug --- app/controllers/api/projects.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index a21aa37be6..e3a04f1a50 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -224,6 +224,7 @@ App::post('/v1/projects') } var_dump($dsn->getHost()); + var_dump(in_array($dsn->getHost(), $sharedTablesV1)); var_dump($sharedTablesV1); var_dump($globalCollections); var_dump($create); @@ -242,7 +243,7 @@ App::post('/v1/projects') if (($collection['$collection'] ?? '') !== Database::METADATA) { continue; } - + var_dump('trying to create collection ' . $collection['$collection']); $attributes = \array_map(fn ($attribute) => new Document($attribute), $collection['attributes']); $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']); $dbForProject->createCollection($key, $attributes, $indexes); From db244937ffface2dbb4f737a08fb0d5bd71ffe96 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 16:10:14 +0300 Subject: [PATCH 33/47] debug --- app/controllers/api/projects.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index e3a04f1a50..ece3989878 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -223,13 +223,16 @@ App::post('/v1/projects') $create = false; } - var_dump($dsn->getHost()); - var_dump(in_array($dsn->getHost(), $sharedTablesV1)); - var_dump($sharedTablesV1); + //var_dump($dsn->getHost()); + //var_dump(in_array($dsn->getHost(), $sharedTablesV1)); + //var_dump($sharedTablesV1); var_dump($globalCollections); var_dump($create); if ($create || !$globalCollections) { + var_dump('#######'); + var_dump($globalCollections); + var_dump($create); $audit = new Audit($dbForProject); $audit->setup(); From 7ab55e0694c908d37d85b7330f1f0fb852d6c85e Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 16:21:39 +0300 Subject: [PATCH 34/47] debug --- app/controllers/api/projects.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index ece3989878..3427e6d5c1 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -223,9 +223,9 @@ App::post('/v1/projects') $create = false; } - //var_dump($dsn->getHost()); - //var_dump(in_array($dsn->getHost(), $sharedTablesV1)); - //var_dump($sharedTablesV1); + var_dump($dsn->getHost()); + var_dump(in_array($dsn->getHost(), $sharedTablesV1)); + var_dump($sharedTablesV1); var_dump($globalCollections); var_dump($create); From d980711c30bec77e9f9df12e02cedc72f2478c6f Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 16:32:19 +0300 Subject: [PATCH 35/47] debug --- app/controllers/api/projects.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 3427e6d5c1..cf58abb238 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -223,12 +223,6 @@ App::post('/v1/projects') $create = false; } - var_dump($dsn->getHost()); - var_dump(in_array($dsn->getHost(), $sharedTablesV1)); - var_dump($sharedTablesV1); - var_dump($globalCollections); - var_dump($create); - if ($create || !$globalCollections) { var_dump('#######'); var_dump($globalCollections); From c853b6593aebe211358fdedeba6bb66dd83ce680 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 17:22:37 +0300 Subject: [PATCH 36/47] debug --- app/controllers/api/projects.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index cf58abb238..492f27a248 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -223,10 +223,9 @@ App::post('/v1/projects') $create = false; } + var_dump($globalCollections); + var_dump($create); if ($create || !$globalCollections) { - var_dump('#######'); - var_dump($globalCollections); - var_dump($create); $audit = new Audit($dbForProject); $audit->setup(); From b2521cf8a33e19634acc5e46fe0126d651149f69 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 17:36:08 +0300 Subject: [PATCH 37/47] debug --- app/controllers/api/projects.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 492f27a248..9fad64a138 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -206,7 +206,8 @@ App::post('/v1/projects') if (\in_array($dsn->getHost(), $sharedTables)) { $dbForProject ->setSharedTables(true) - ->setTenant($globalCollections ? null : $project->getInternalId()) + ->setTenant($project->getInternalId()) + //->setTenant($globalCollections ? null : $project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); } else { $dbForProject @@ -217,15 +218,14 @@ App::post('/v1/projects') $create = true; - try { - $dbForProject->create(); - } catch (Duplicate) { - $create = false; - } + //try { + $dbForProject->create(); + //} catch (Duplicate) { + // $create = false; + //} - var_dump($globalCollections); - var_dump($create); - if ($create || !$globalCollections) { + + //if ($create || !$globalCollections) { $audit = new Audit($dbForProject); $audit->setup(); @@ -239,12 +239,12 @@ App::post('/v1/projects') if (($collection['$collection'] ?? '') !== Database::METADATA) { continue; } - var_dump('trying to create collection ' . $collection['$collection']); + $attributes = \array_map(fn ($attribute) => new Document($attribute), $collection['attributes']); $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']); $dbForProject->createCollection($key, $attributes, $indexes); } - } + //} // Hook allowing instant project mirroring during migration // Outside of migration, hook is not registered and has no effect From a37aa5551a45b475ab8ef4b3f738bf427891b71b Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Oct 2024 17:59:39 +0300 Subject: [PATCH 38/47] debug --- composer.json | 2 +- composer.lock | 27 +++++++++------------------ 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 10f2a12492..50881bec87 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/cache": "0.10.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "dev-feat-migrations as 0.53.6", + "utopia-php/database": "0.53.6", "utopia-php/domains": "0.5.*", "utopia-php/dsn": "0.2.1", "utopia-php/framework": "0.33.*", diff --git a/composer.lock b/composer.lock index bc2b3fb5ca..308fc85ebf 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5cb1524d4677594554ab26d05b3c38e6", + "content-hash": "a81b2ddbd465059b986947b63435e2bc", "packages": [ { "name": "adhocore/jwt", @@ -1724,16 +1724,16 @@ }, { "name": "utopia-php/database", - "version": "dev-feat-migrations", + "version": "0.53.6", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "efd0f4e8fb5d158486aec0e6fea9561facf0daad" + "reference": "feddc8e808eaea9a11c65cca3f01683def422f52" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/efd0f4e8fb5d158486aec0e6fea9561facf0daad", - "reference": "efd0f4e8fb5d158486aec0e6fea9561facf0daad", + "url": "https://api.github.com/repos/utopia-php/database/zipball/feddc8e808eaea9a11c65cca3f01683def422f52", + "reference": "feddc8e808eaea9a11c65cca3f01683def422f52", "shasum": "" }, "require": { @@ -1774,9 +1774,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/feat-migrations" + "source": "https://github.com/utopia-php/database/tree/0.53.6" }, - "time": "2024-10-15T05:50:09+00:00" + "time": "2024-10-08T02:18:46+00:00" }, { "name": "utopia-php/domains", @@ -7003,18 +7003,9 @@ "time": "2024-03-07T20:33:40+00:00" } ], - "aliases": [ - { - "package": "utopia-php/database", - "version": "dev-feat-migrations", - "alias": "0.53.6", - "alias_normalized": "0.53.6.0" - } - ], + "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "utopia-php/database": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { From 8743330ad86078e787b4b4a1640bcd400fc2a52b Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 21 Oct 2024 09:59:05 +0300 Subject: [PATCH 39/47] debug --- app/controllers/api/projects.php | 18 +++++++++--------- composer.json | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 9fad64a138..062756bb21 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -203,11 +203,12 @@ App::post('/v1/projects') $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); $globalCollections = !\in_array($dsn->getHost(), $sharedTablesV1); + if (\in_array($dsn->getHost(), $sharedTables)) { $dbForProject ->setSharedTables(true) ->setTenant($project->getInternalId()) - //->setTenant($globalCollections ? null : $project->getInternalId()) + ->setTenant($globalCollections ? null : $project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); } else { $dbForProject @@ -218,14 +219,13 @@ App::post('/v1/projects') $create = true; - //try { - $dbForProject->create(); - //} catch (Duplicate) { - // $create = false; - //} + try { + $dbForProject->create(); + } catch (Duplicate) { + $create = false; + } - - //if ($create || !$globalCollections) { + if ($create || !$globalCollections) { $audit = new Audit($dbForProject); $audit->setup(); @@ -244,7 +244,7 @@ App::post('/v1/projects') $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']); $dbForProject->createCollection($key, $attributes, $indexes); } - //} + } // Hook allowing instant project mirroring during migration // Outside of migration, hook is not registered and has no effect diff --git a/composer.json b/composer.json index 50881bec87..10f2a12492 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/cache": "0.10.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "0.53.6", + "utopia-php/database": "dev-feat-migrations as 0.53.6", "utopia-php/domains": "0.5.*", "utopia-php/dsn": "0.2.1", "utopia-php/framework": "0.33.*", From 6435fa84af5a52bc4fbabf718cce907d1768c9f7 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 21 Oct 2024 10:00:35 +0300 Subject: [PATCH 40/47] debug --- app/controllers/api/projects.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 062756bb21..7e04ee07b7 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -225,6 +225,11 @@ App::post('/v1/projects') $create = false; } + var_dump('$globalCollections = '); + var_dump($globalCollections); + var_dump('$create = '); + var_dump($create); + if ($create || !$globalCollections) { $audit = new Audit($dbForProject); $audit->setup(); From ba0a495d3cf99464f7247ab05e5f49891f0ab625 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 21 Oct 2024 10:01:32 +0300 Subject: [PATCH 41/47] debug --- composer.lock | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/composer.lock b/composer.lock index 308fc85ebf..bc2b3fb5ca 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a81b2ddbd465059b986947b63435e2bc", + "content-hash": "5cb1524d4677594554ab26d05b3c38e6", "packages": [ { "name": "adhocore/jwt", @@ -1724,16 +1724,16 @@ }, { "name": "utopia-php/database", - "version": "0.53.6", + "version": "dev-feat-migrations", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "feddc8e808eaea9a11c65cca3f01683def422f52" + "reference": "efd0f4e8fb5d158486aec0e6fea9561facf0daad" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/feddc8e808eaea9a11c65cca3f01683def422f52", - "reference": "feddc8e808eaea9a11c65cca3f01683def422f52", + "url": "https://api.github.com/repos/utopia-php/database/zipball/efd0f4e8fb5d158486aec0e6fea9561facf0daad", + "reference": "efd0f4e8fb5d158486aec0e6fea9561facf0daad", "shasum": "" }, "require": { @@ -1774,9 +1774,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.53.6" + "source": "https://github.com/utopia-php/database/tree/feat-migrations" }, - "time": "2024-10-08T02:18:46+00:00" + "time": "2024-10-15T05:50:09+00:00" }, { "name": "utopia-php/domains", @@ -7003,9 +7003,18 @@ "time": "2024-03-07T20:33:40+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "utopia-php/database", + "version": "dev-feat-migrations", + "alias": "0.53.6", + "alias_normalized": "0.53.6.0" + } + ], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "utopia-php/database": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { From a41b76740047bc0a82e945ef2d4f353287a74b5a Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 21 Oct 2024 10:32:53 +0300 Subject: [PATCH 42/47] debug --- app/controllers/api/projects.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 7e04ee07b7..924057b426 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -225,10 +225,7 @@ App::post('/v1/projects') $create = false; } - var_dump('$globalCollections = '); - var_dump($globalCollections); - var_dump('$create = '); - var_dump($create); + if ($create || !$globalCollections) { $audit = new Audit($dbForProject); @@ -247,7 +244,11 @@ App::post('/v1/projects') $attributes = \array_map(fn ($attribute) => new Document($attribute), $collection['attributes']); $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']); - $dbForProject->createCollection($key, $attributes, $indexes); + try { + $dbForProject->createCollection($key, $attributes, $indexes); + } catch (Duplicate) { + // Collection already exists + } } } From 3ff5472543850771bac77e0056bd948e90d1f194 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 21 Oct 2024 18:57:11 +0300 Subject: [PATCH 43/47] sync with feat-migration --- app/controllers/api/projects.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 924057b426..b46302be07 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -207,7 +207,6 @@ App::post('/v1/projects') if (\in_array($dsn->getHost(), $sharedTables)) { $dbForProject ->setSharedTables(true) - ->setTenant($project->getInternalId()) ->setTenant($globalCollections ? null : $project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); } else { @@ -225,8 +224,6 @@ App::post('/v1/projects') $create = false; } - - if ($create || !$globalCollections) { $audit = new Audit($dbForProject); $audit->setup(); @@ -247,7 +244,16 @@ App::post('/v1/projects') try { $dbForProject->createCollection($key, $attributes, $indexes); } catch (Duplicate) { - // Collection already exists + if (!$globalCollections) { + $dbForProject->createDocument(Database::METADATA, new Document([ + '$id' => ID::custom($key), + '$permissions' => [Permission::create(Role::any())], + 'name' => $key, + 'attributes' => $attributes, + 'indexes' => $indexes, + 'documentSecurity' => true + ])); + } } } } From e687c64ca02de2e7a13aeffd2a3eec8cbdcfbc03 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 21 Oct 2024 18:58:11 +0300 Subject: [PATCH 44/47] sync with feat-migration --- app/controllers/api/projects.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index b46302be07..b04657e07f 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -241,6 +241,7 @@ App::post('/v1/projects') $attributes = \array_map(fn ($attribute) => new Document($attribute), $collection['attributes']); $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']); + try { $dbForProject->createCollection($key, $attributes, $indexes); } catch (Duplicate) { From f8b2878943c68ccecc9e6e77b45520b7a5ea0c69 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Oct 2024 11:09:27 +0300 Subject: [PATCH 45/47] sync with feat-migration --- app/controllers/api/functions.php | 4 ++-- app/controllers/api/messaging.php | 10 +++++----- composer.lock | 20 ++++++++++---------- src/Appwrite/Migration/Version/V19.php | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 5942077ae4..42a8e7f8fa 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -250,7 +250,7 @@ App::post('/v1/functions') $schedule = Authorization::skip( fn () => $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), // Todo replace with projects region + 'region' => $project->getAttribute('region'), 'resourceType' => 'function', 'resourceId' => $function->getId(), 'resourceInternalId' => $function->getInternalId(), @@ -1928,7 +1928,7 @@ App::post('/v1/functions/:functionId/executions') ]; $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => ScheduleExecutions::getSupportedResource(), 'resourceId' => $execution->getId(), 'resourceInternalId' => $execution->getInternalId(), diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 7da0348a8f..315782775a 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -2691,7 +2691,7 @@ App::post('/v1/messaging/messages/email') break; case MessageStatus::SCHEDULED: $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), @@ -2807,7 +2807,7 @@ App::post('/v1/messaging/messages/sms') break; case MessageStatus::SCHEDULED: $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), @@ -2983,7 +2983,7 @@ App::post('/v1/messaging/messages/push') break; case MessageStatus::SCHEDULED: $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), @@ -3522,7 +3522,7 @@ App::patch('/v1/messaging/messages/sms/:messageId') if (\is_null($currentScheduledAt) && !\is_null($scheduledAt)) { $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), @@ -3686,7 +3686,7 @@ App::patch('/v1/messaging/messages/push/:messageId') if (\is_null($currentScheduledAt) && !\is_null($scheduledAt)) { $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), diff --git a/composer.lock b/composer.lock index bc2b3fb5ca..061f669841 100644 --- a/composer.lock +++ b/composer.lock @@ -1728,12 +1728,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "efd0f4e8fb5d158486aec0e6fea9561facf0daad" + "reference": "5b9462f8abc12c2c7ab2dac99aa6f68aa2e27365" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/efd0f4e8fb5d158486aec0e6fea9561facf0daad", - "reference": "efd0f4e8fb5d158486aec0e6fea9561facf0daad", + "url": "https://api.github.com/repos/utopia-php/database/zipball/5b9462f8abc12c2c7ab2dac99aa6f68aa2e27365", + "reference": "5b9462f8abc12c2c7ab2dac99aa6f68aa2e27365", "shasum": "" }, "require": { @@ -1776,7 +1776,7 @@ "issues": "https://github.com/utopia-php/database/issues", "source": "https://github.com/utopia-php/database/tree/feat-migrations" }, - "time": "2024-10-15T05:50:09+00:00" + "time": "2024-10-22T08:05:24+00:00" }, { "name": "utopia-php/domains", @@ -2124,16 +2124,16 @@ }, { "name": "utopia-php/messaging", - "version": "0.12.1", + "version": "0.12.2", "source": { "type": "git", "url": "https://github.com/utopia-php/messaging.git", - "reference": "b9dfafb5efc1d12cbee01d03dc98853ef026e35b" + "reference": "f6790fba1fcee12163d51c65d2c226a7856295d9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/messaging/zipball/b9dfafb5efc1d12cbee01d03dc98853ef026e35b", - "reference": "b9dfafb5efc1d12cbee01d03dc98853ef026e35b", + "url": "https://api.github.com/repos/utopia-php/messaging/zipball/f6790fba1fcee12163d51c65d2c226a7856295d9", + "reference": "f6790fba1fcee12163d51c65d2c226a7856295d9", "shasum": "" }, "require": { @@ -2169,9 +2169,9 @@ ], "support": { "issues": "https://github.com/utopia-php/messaging/issues", - "source": "https://github.com/utopia-php/messaging/tree/0.12.1" + "source": "https://github.com/utopia-php/messaging/tree/0.12.2" }, - "time": "2024-10-09T08:17:07+00:00" + "time": "2024-10-22T01:02:20+00:00" }, { "name": "utopia-php/migration", diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index 18234ebdc4..039169f3a6 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -731,7 +731,7 @@ class V19 extends Migration if (empty($document->getAttribute('scheduleId', null))) { $schedule = $this->consoleDB->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), // Todo replace with projects region + 'region' => $project->getAttribute('region'), 'resourceType' => 'function', 'resourceId' => $document->getId(), 'resourceInternalId' => $document->getInternalId(), From 54555cb3cd702399a52b1e8172232611d29aedcb Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Oct 2024 19:39:57 +0300 Subject: [PATCH 46/47] updates --- app/controllers/api/projects.php | 63 ++++++++++--------- composer.json | 2 +- composer.lock | 39 +++++------- src/Appwrite/Platform/Tasks/ScheduleBase.php | 21 ++++--- .../Platform/Tasks/ScheduleExecutions.php | 10 +-- .../Platform/Tasks/ScheduleFunctions.php | 12 ++-- .../Platform/Tasks/ScheduleMessages.php | 12 ++-- 7 files changed, 83 insertions(+), 76 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index b04657e07f..78f2c56cd8 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -207,7 +207,8 @@ App::post('/v1/projects') if (\in_array($dsn->getHost(), $sharedTables)) { $dbForProject ->setSharedTables(true) - ->setTenant($globalCollections ? null : $project->getInternalId()) + ->setTenant($project->getInternalId()) + //->setTenant($globalCollections ? null : $project->getInternalId()) ->setNamespace($dsn->getParam('namespace')); } else { $dbForProject @@ -218,46 +219,50 @@ App::post('/v1/projects') $create = true; - try { + // try { $dbForProject->create(); - } catch (Duplicate) { - $create = false; - } + // } catch (Duplicate) { + // $create = false; + //} - if ($create || !$globalCollections) { - $audit = new Audit($dbForProject); - $audit->setup(); + $audit = new Audit($dbForProject); + $audit->setup(); - $abuse = new TimeLimit('', 0, 1, $dbForProject); - $abuse->setup(); + $abuse = new TimeLimit('', 0, 1, $dbForProject); + $abuse->setup(); - /** @var array $collections */ - $collections = Config::getParam('collections', [])['projects'] ?? []; + /** @var array $collections */ + $collections = Config::getParam('collections', [])['projects'] ?? []; - foreach ($collections as $key => $collection) { - if (($collection['$collection'] ?? '') !== Database::METADATA) { - continue; - } + foreach ($collections as $key => $collection) { + if (($collection['$collection'] ?? '') !== Database::METADATA) { + continue; + } - $attributes = \array_map(fn ($attribute) => new Document($attribute), $collection['attributes']); - $indexes = \array_map(fn (array $index) => new Document($index), $collection['indexes']); + $attributes = \array_map(function (array $attribute) { + return new Document($attribute); + }, $collection['attributes']); + + $indexes = \array_map(function (array $index) { + return new Document($index); + }, $collection['indexes']); try { $dbForProject->createCollection($key, $attributes, $indexes); } catch (Duplicate) { - if (!$globalCollections) { - $dbForProject->createDocument(Database::METADATA, new Document([ - '$id' => ID::custom($key), - '$permissions' => [Permission::create(Role::any())], - 'name' => $key, - 'attributes' => $attributes, - 'indexes' => $indexes, - 'documentSecurity' => true - ])); - } +// if (!$globalCollections) { +// $dbForProject->createDocument(Database::METADATA, new Document([ +// '$id' => ID::custom($key), +// '$permissions' => [Permission::create(Role::any())], +// 'name' => $key, +// 'attributes' => $attributes, +// 'indexes' => $indexes, +// 'documentSecurity' => true +// ])); +// } } } - } + // } // Hook allowing instant project mirroring during migration // Outside of migration, hook is not registered and has no effect diff --git a/composer.json b/composer.json index 10f2a12492..50881bec87 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/cache": "0.10.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "dev-feat-migrations as 0.53.6", + "utopia-php/database": "0.53.6", "utopia-php/domains": "0.5.*", "utopia-php/dsn": "0.2.1", "utopia-php/framework": "0.33.*", diff --git a/composer.lock b/composer.lock index 061f669841..70b3d833c2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5cb1524d4677594554ab26d05b3c38e6", + "content-hash": "a81b2ddbd465059b986947b63435e2bc", "packages": [ { "name": "adhocore/jwt", @@ -1724,16 +1724,16 @@ }, { "name": "utopia-php/database", - "version": "dev-feat-migrations", + "version": "0.53.6", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "5b9462f8abc12c2c7ab2dac99aa6f68aa2e27365" + "reference": "feddc8e808eaea9a11c65cca3f01683def422f52" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/5b9462f8abc12c2c7ab2dac99aa6f68aa2e27365", - "reference": "5b9462f8abc12c2c7ab2dac99aa6f68aa2e27365", + "url": "https://api.github.com/repos/utopia-php/database/zipball/feddc8e808eaea9a11c65cca3f01683def422f52", + "reference": "feddc8e808eaea9a11c65cca3f01683def422f52", "shasum": "" }, "require": { @@ -1774,9 +1774,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/feat-migrations" + "source": "https://github.com/utopia-php/database/tree/0.53.6" }, - "time": "2024-10-22T08:05:24+00:00" + "time": "2024-10-08T02:18:46+00:00" }, { "name": "utopia-php/domains", @@ -2341,16 +2341,16 @@ }, { "name": "utopia-php/platform", - "version": "0.7.0", + "version": "0.7.1", "source": { "type": "git", "url": "https://github.com/utopia-php/platform.git", - "reference": "beeea0f2c9bce14a6869fc5c87a1047cdecb5c52" + "reference": "3433a0f1a54988f2a59c735f507745cb2c24638a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/platform/zipball/beeea0f2c9bce14a6869fc5c87a1047cdecb5c52", - "reference": "beeea0f2c9bce14a6869fc5c87a1047cdecb5c52", + "url": "https://api.github.com/repos/utopia-php/platform/zipball/3433a0f1a54988f2a59c735f507745cb2c24638a", + "reference": "3433a0f1a54988f2a59c735f507745cb2c24638a", "shasum": "" }, "require": { @@ -2385,9 +2385,9 @@ ], "support": { "issues": "https://github.com/utopia-php/platform/issues", - "source": "https://github.com/utopia-php/platform/tree/0.7.0" + "source": "https://github.com/utopia-php/platform/tree/0.7.1" }, - "time": "2024-05-08T17:00:55+00:00" + "time": "2024-10-22T10:27:49+00:00" }, { "name": "utopia-php/pools", @@ -7003,18 +7003,9 @@ "time": "2024-03-07T20:33:40+00:00" } ], - "aliases": [ - { - "package": "utopia-php/database", - "version": "dev-feat-migrations", - "alias": "0.53.6", - "alias_normalized": "0.53.6.0" - } - ], + "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "utopia-php/database": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index 8f893b3b2f..a1b85c341f 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -11,7 +11,6 @@ use Utopia\Database\Exception; use Utopia\Database\Query; use Utopia\Platform\Action; use Utopia\Pools\Group; -use Utopia\Queue\Connection; use Utopia\System\System; use function Swoole\Coroutine\run; @@ -26,7 +25,7 @@ abstract class ScheduleBase extends Action abstract public static function getName(): string; abstract public static function getSupportedResource(): string; abstract public static function getCollectionId(): string; - abstract protected function enqueueResources(Connection $queue, Database $dbForConsole); + abstract protected function enqueueResources(Group $pools, Database $dbForConsole, callable $getProjectDB): void; public function __construct() { @@ -34,10 +33,10 @@ abstract class ScheduleBase extends Action $this ->desc("Execute {$type}s scheduled in Appwrite") - ->inject('queue') + ->inject('pools') ->inject('dbForConsole') ->inject('getProjectDB') - ->callback(fn (Connection $queue, Database $dbForConsole, callable $getProjectDB) => $this->action($queue, $dbForConsole, $getProjectDB)); + ->callback(fn (Group $pools, Database $dbForConsole, callable $getProjectDB) => $this->action($pools, $dbForConsole, $getProjectDB)); } /** @@ -45,7 +44,7 @@ abstract class ScheduleBase extends Action * 2. Create timer that sync all changes from 'schedules' collection to local copy. Only reading changes thanks to 'resourceUpdatedAt' attribute * 3. Create timer that prepares coroutines for soon-to-execute schedules. When it's ready, coroutine sleeps until exact time before sending request to worker. */ - public function action(Connection $queue, Database $dbForConsole, callable $getProjectDB): void + public function action(Group $pools, Database $dbForConsole, callable $getProjectDB): void { Console::title(\ucfirst(static::getSupportedResource()) . ' scheduler V1'); Console::success(APP_NAME . ' ' . \ucfirst(static::getSupportedResource()) . ' scheduler v1 has started'); @@ -114,15 +113,17 @@ abstract class ScheduleBase extends Action $latestDocument = \end($results); } + $pools->reclaim(); + Console::success("{$total} resources were loaded in " . (\microtime(true) - $loadStart) . " seconds"); Console::success("Starting timers at " . DateTime::now()); - run(function () use ($dbForConsole, &$lastSyncUpdate, $getSchedule, $queue) { + run(function () use ($dbForConsole, &$lastSyncUpdate, $getSchedule, $pools, $getProjectDB) { /** * The timer synchronize $schedules copy with database collection. */ - Timer::tick(static::UPDATE_TIMER * 1000, function () use ($dbForConsole, &$lastSyncUpdate, $getSchedule, $queue) { + Timer::tick(static::UPDATE_TIMER * 1000, function () use ($dbForConsole, &$lastSyncUpdate, $getSchedule, $pools) { $time = DateTime::now(); $timerStart = \microtime(true); @@ -171,15 +172,17 @@ abstract class ScheduleBase extends Action $lastSyncUpdate = $time; $timerEnd = \microtime(true); + $pools->reclaim(); + Console::log("Sync tick: {$total} schedules were updated in " . ($timerEnd - $timerStart) . " seconds"); }); Timer::tick( static::ENQUEUE_TIMER * 1000, - fn () => $this->enqueueResources($queue, $dbForConsole) + fn () => $this->enqueueResources($pools, $dbForConsole, $getProjectDB) ); - $this->enqueueResources($queue, $dbForConsole); + $this->enqueueResources($pools, $dbForConsole, $getProjectDB); }); } } diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index 6bbc4ab65a..73a2814397 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -6,7 +6,6 @@ use Appwrite\Event\Func; use Swoole\Coroutine as Co; use Utopia\Database\Database; use Utopia\Pools\Group; -use Utopia\Queue\Connection; class ScheduleExecutions extends ScheduleBase { @@ -28,10 +27,11 @@ class ScheduleExecutions extends ScheduleBase return 'executions'; } - protected function enqueueResources(Connection $queue, Database $dbForConsole): void + protected function enqueueResources(Group $pools, Database $dbForConsole, callable $getProjectDB): void { - - $queueForFunctions = new Func($queue); + $queue = $pools->get('queue')->pop(); + $connection = $queue->getResource(); + $queueForFunctions = new Func($connection); $intervalEnd = (new \DateTime())->modify('+' . self::ENQUEUE_TIMER . ' seconds'); foreach ($this->schedules as $schedule) { @@ -81,5 +81,7 @@ class ScheduleExecutions extends ScheduleBase unset($this->schedules[$schedule['$internalId']]); } + + $queue->reclaim(); } } diff --git a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php index d1ee5a6e93..4d57902330 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php @@ -8,7 +8,6 @@ use Utopia\CLI\Console; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Pools\Group; -use Utopia\Queue\Connection; class ScheduleFunctions extends ScheduleBase { @@ -32,7 +31,7 @@ class ScheduleFunctions extends ScheduleBase return 'functions'; } - protected function enqueueResources(Connection $queue, Database $dbForConsole): void + protected function enqueueResources(Group $pools, Database $dbForConsole, callable $getProjectDB): void { $timerStart = \microtime(true); $time = DateTime::now(); @@ -71,9 +70,12 @@ class ScheduleFunctions extends ScheduleBase } foreach ($delayedExecutions as $delay => $scheduleKeys) { - \go(function () use ($delay, $scheduleKeys, $queue) { + \go(function () use ($delay, $scheduleKeys, $pools) { \sleep($delay); // in seconds + $queue = $pools->get('queue')->pop(); + $connection = $queue->getResource(); + foreach ($scheduleKeys as $scheduleKey) { // Ensure schedule was not deleted if (!\array_key_exists($scheduleKey, $this->schedules)) { @@ -82,7 +84,7 @@ class ScheduleFunctions extends ScheduleBase $schedule = $this->schedules[$scheduleKey]; - $queueForFunctions = new Func($queue); + $queueForFunctions = new Func($connection); $queueForFunctions ->setType('schedule') @@ -92,6 +94,8 @@ class ScheduleFunctions extends ScheduleBase ->setProject($schedule['project']) ->trigger(); } + + $queue->reclaim(); }); } diff --git a/src/Appwrite/Platform/Tasks/ScheduleMessages.php b/src/Appwrite/Platform/Tasks/ScheduleMessages.php index 4f58a58177..b9d8e2a282 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleMessages.php +++ b/src/Appwrite/Platform/Tasks/ScheduleMessages.php @@ -5,7 +5,6 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Event\Messaging; use Utopia\Database\Database; use Utopia\Pools\Group; -use Utopia\Queue\Connection; class ScheduleMessages extends ScheduleBase { @@ -27,7 +26,7 @@ class ScheduleMessages extends ScheduleBase return 'messages'; } - protected function enqueueResources(Connection $queue, Database $dbForConsole): void + protected function enqueueResources(Group $pools, Database $dbForConsole, callable $getProjectDB): void { foreach ($this->schedules as $schedule) { if (!$schedule['active']) { @@ -41,9 +40,10 @@ class ScheduleMessages extends ScheduleBase continue; } - \go(function () use ($schedule, $queue, $dbForConsole) { - - $queueForMessaging = new Messaging($queue); + \go(function () use ($schedule, $pools, $dbForConsole) { + $queue = $pools->get('queue')->pop(); + $connection = $queue->getResource(); + $queueForMessaging = new Messaging($connection); $queueForMessaging ->setType(MESSAGE_SEND_TYPE_EXTERNAL) @@ -56,6 +56,8 @@ class ScheduleMessages extends ScheduleBase $schedule['$id'], ); + $queue->reclaim(); + unset($this->schedules[$schedule['$internalId']]); }); } From 7be7b3389c2d1815cf09f45b1f8850512803c918 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Oct 2024 21:31:32 +0300 Subject: [PATCH 47/47] debug --- app/controllers/api/projects.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 78f2c56cd8..c8c3928ebc 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -123,7 +123,7 @@ App::post('/v1/projects') } $databases = Config::getParam('pools-database', []); - + var_dump($databases); $databaseOverride = System::getEnv('_APP_DATABASE_OVERRIDE'); $index = \array_search($databaseOverride, $databases); if ($index !== false) { @@ -198,6 +198,9 @@ App::post('/v1/projects') $dsn = new DSN('mysql://' . $dsn); } + + var_dump($dsn->getHost()); + $adapter = $pools->get($dsn->getHost())->pop()->getResource(); $dbForProject = new Database($adapter, $cache); $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', ''));