fix(scheduler): stop dropping live function crons on a project-DB race (#13334)

* 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 <chiragaggarwal5k@gmail.com>

* style: apply Pint to scheduler flake-fix files

Co-authored-by: Chirag Aggarwal <chiragaggarwal5k@gmail.com>

* 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 <chiragaggarwal5k@gmail.com>

* 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 <chiragaggarwal5k@gmail.com>

* fix(scheduler): drop the getProjectDB comment in cli.php

Co-authored-by: Chirag Aggarwal <chiragaggarwal5k@gmail.com>

* 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 <chiragaggarwal5k@gmail.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
Chirag AggarwalandCursor Agent authored and GitHub committed 2026-08-25 17:13:30 +05:30
1 parent 4a5a087b19
commit a6782899a6
1 file changed
+10 -71
+10 -71
View File
@@ -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;