From a6782899a671e81eb422d7db252f7e5c5319fd82 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 25 Aug 2026 17:13:30 +0530 Subject: [PATCH] fix(scheduler): stop dropping live function crons on a project-DB race (#13334) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(scheduler): isolate project DB lookups for scheduled functions The combined scheduler runs functions, executions, and messages as sibling coroutines. CLI getProjectDB reused one Database per host and mutated its namespace, so a live function could be read from the wrong project and treated as missing. That miss deleted the schedule, and a failed make() is not retried until the next full snapshot — longer than the FunctionsSchedule e2e wait. Give each lookup its own Database (same pattern as HTTP/worker), stop orphan-deleting on a single miss, and keep a function cron registered when the worker will re-fetch by id at execution time. Co-authored-by: Chirag Aggarwal * style: apply Pint to scheduler flake-fix files Co-authored-by: Chirag Aggarwal * style: reformat scheduler files with repo Pint PSR-12 The previous format pass used the container default Laravel preset because pint.json is not in the image. CI runs vendor/bin/pint --config pint.json (PSR-12). Restore new() parentheses and constructor braces. Co-authored-by: Chirag Aggarwal * fix(scheduler): keep only logic diffs vs main Restore surrounding style from main so the review is the getProjectDB isolation, no orphan-delete on a single miss, and the function stub. Co-authored-by: Chirag Aggarwal * fix(scheduler): drop the getProjectDB comment in cli.php Co-authored-by: Chirag Aggarwal * fix(scheduler): keep only CLI getProjectDB isolation Restore deleteOrphan and the skipFilters function lookup. With a per-call Database, a miss is a real miss again. Co-authored-by: Chirag Aggarwal --------- Co-authored-by: Cursor Agent --- app/cli.php | 81 +++++++---------------------------------------------- 1 file changed, 10 insertions(+), 71 deletions(-) diff --git a/app/cli.php b/app/cli.php index 4d92e515cf..9e471618d3 100644 --- a/app/cli.php +++ b/app/cli.php @@ -2,6 +2,7 @@ require_once __DIR__ . '/init.php'; +use Appwrite\Database\Factory as DatabaseFactory; use Appwrite\Platform\Appwrite; use Appwrite\Runtimes\Runtimes; use Appwrite\Usage\Context as UsageContext; @@ -19,7 +20,6 @@ use Utopia\Database\Adapter\Pool as DatabasePool; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\DSN\DSN; use Utopia\Logger\Log; use Utopia\Platform\Service; use Utopia\Pools\Group; @@ -125,81 +125,20 @@ $container->set( [] ); -$container->set('getProjectDB', function (Group $pools, Database $dbForPlatform, $cache, $authorization) { - $databases = []; // TODO: @Meldiron This should probably be responsibility of utopia-php/pools - - return function (Document $project) use ($pools, $dbForPlatform, $cache, $authorization, &$databases) { +$container->set('getProjectDB', function (DatabaseFactory $databaseFactory, Database $dbForPlatform) { + return function (Document $project) use ($databaseFactory, $dbForPlatform): Database { if ($project->isEmpty() || $project->getId() === 'console') { return $dbForPlatform; } - try { - $dsn = new DSN($project->getAttribute('database')); - } catch (\InvalidArgumentException) { - // TODO: Temporary until all projects are using shared tables - $dsn = new DSN('mysql://' . $project->getAttribute('database')); - } - - if (isset($databases[$dsn->getHost()])) { - /** @var array $collections */ - $collections = Config::getParam('collections', []); - $projectCollections = $collections['projects'] ?? []; - $projectsGlobalCollections = array_keys($projectCollections); - $projectsGlobalCollections[] = 'audit'; - - $database = $databases[$dsn->getHost()]; - $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - - if (\in_array($dsn->getHost(), $sharedTables)) { - $database - ->setSharedTables(true) - ->setGlobalCollections($projectsGlobalCollections) - ->setTenant($project->getSequence()) - ->setNamespace($dsn->getParam('namespace')); - } else { - $database - ->setSharedTables(false) - ->setTenant(null) - ->setNamespace('_' . $project->getSequence()); - } - - return $database; - } - - $adapter = new DatabasePool($pools->get($dsn->getHost())); - $database = new Database($adapter, $cache); - - $databases[$dsn->getHost()] = $database; - $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); - - if (\in_array($dsn->getHost(), $sharedTables)) { - /** @var array $collections */ - $collections = Config::getParam('collections', []); - $projectCollections = $collections['projects'] ?? []; - $projectsGlobalCollections = array_keys($projectCollections); - $projectsGlobalCollections[] = 'audit'; - - $database - ->setSharedTables(true) - ->setTenant($project->getSequence()) - ->setGlobalCollections($projectsGlobalCollections) - ->setNamespace($dsn->getParam('namespace')); - } else { - $database - ->setSharedTables(false) - ->setTenant(null) - ->setNamespace('_' . $project->getSequence()); - } - - $database - ->setDatabase(APP_DATABASE) - ->setAuthorization($authorization) - ->setMetadata('host', \gethostname()) - ->setMetadata('project', $project->getId()); - - return $database; + return $databaseFactory->project( + $project, + APP_DATABASE_TIMEOUT_MILLISECONDS_TASK, + APP_DATABASE_QUERY_MAX_VALUES, + ['host' => \gethostname(), 'project' => $project->getId()] + ); }; -}, ['pools', 'dbForPlatform', 'cache', 'authorization']); +}, ['databaseFactory', 'dbForPlatform']); $container->set('getLogsDB', function (Group $pools, Cache $cache, Authorization $authorization) { $database = null;