Repository navigation
fix(daemon): run each account's sync jobs on their own - #77
Merged
Merged
Conversation
The daemon job loop took turns between accounts and awaited each job, so while one account's job slept through a FLOOD_WAIT (up to 5 minutes) or waited on a slow page, every other account's sync stopped too, and with three or more busy accounts each one synced more slowly than alone. Each account now has its own job in flight: the main loop starts the next job of every account that has none running and doesn't wait for it. The 3 s delay still counts from the end of the account's own last job, and the lane cycle, the retry rules and the per-account rate limits are unchanged. A stop ends the tick sleep at once, the shutdown waits for the running jobs (whose requests the stop already ended) before it drops the schedulers and closes the clients, and the health check skips an account while its job runs, so a reconnect never closes a client under a job. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t it The health check skipped any account with a sync job in flight. A job starts on the same tick, before the check, and a job every 3 to 6 ticks divides the 60-tick interval, so an account busy with a long backfill could miss every check until its queue drained. A dead connection was then never flagged and its jobs kept using up their attempts. getMe now runs beside the job on the same client. Only the reconnect, which closes the client, waits for the job to end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…test The run order alone was the same under the old turn-taking loop, so the test couldn't tell per-account job loops from it. With the tick, both accounts must run at 0 s and again at 3 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… clients The main loop no longer awaits jobs, so only the shutdown's wait keeps a client or cache from closing under a running job. The fake FLOOD_WAIT ended the job within microtasks, before the shutdown ran, so the test passed without that wait. Its rejection now comes a moment after the abort, and the test checks that no job is in flight at disconnect. Co-Authored-By: Claude Opus 5.5 <[email protected]>
rate-limiting.md and database-schema.md still said a FLOOD_WAIT slept through in one account's job holds every account, linking #60, and testing.md said the accounts take turns. daemon.md now says the health check's getMe still pauses every account's new jobs during its own FLOOD_WAIT, and that the 30 s shutdown limit covers waiting for the jobs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The daemon job loop took turns between accounts and awaited each job. While one account's job slept through a FLOOD_WAIT (mtcute sleeps up to 5 minutes inside the request) or waited on a slow page, every other account's sync stopped too. With three or more busy accounts, each one synced more slowly than it would alone.
Change
DaemonRuntime.jobsInFlight(one promise per account) replaceslastJobAccountId.startDueJobsstarts the next job of every account that has none running and is due: connected, no recorded FLOOD_WAIT, its own 3 s delay passed.mainLoopcalls it without awaiting.processJobsstill awaits them all, for tests and one-shot use.performCleanupcallsrequestShutdownfirst (also after amainLoopthat threw).cleanupSchedulerwaits for the running jobs before it drops the schedulers, and only then are the clients closed, so a cache or client is never closed under a job. The tg daemon stop waits out a FLOOD_WAIT in the dialog pass or a sync job, past its 10 s timeout #64 behavior stays: the stop ends each job's FLOOD_WAIT at once and the job stays pending with no attempt used.getMeruns beside a running job on the same client. Only the reconnect waits until the account's job ends, so it never closes a client a job is using. (Skipping the whole check could skip a busy account on every check tick: a job every 3 to 6 ticks divides the 60-tick interval.)docs/daemon.mdsays so.docs/daemon.md,docs/sync-strategy.md,docs/rate-limiting.md,docs/database-schema.mdanddocs/testing.mddescribe one job loop per account.Tests
mainLoop, account 2's jobs run on ticks 1, 4 and 7 while account 1's job waits, and the stop ends both (injected clock).cleanupSchedulerwaits for jobs in flight.daemon-stop.test.ts: the aborted FLOOD_WAIT rejects a moment later, and no job is in flight when the clients close (fails without the drain).bun run check, the full unit suite (1766 pass) andsrc/__e2e__/daemon.e2e.test.ts.Risks
getMestill holds back new job starts for every account. That needs a follow-up issue, together with the sequential startupbootstrapDialogs.refreshAllQueuesandcleanupOldJobscan now run while an account's job is running. This is safe: they only dedupe, or delete completed and failed rows, and bun:sqlite is synchronous.mainLoop. They should use the same in-flight pattern and the drain.Fixes #60
🤖 Generated with Claude Code