Skip to content

fix(daemon): run each account's sync jobs on their own - #77

Merged
neoromantic merged 5 commits into
mainfrom
feat/per-account-job-loops-60
Oct 22, 2026
Merged

neoromantic merged 5 commits into
mainfrom
feat/per-account-job-loops-60

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

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) replaces lastJobAccountId.
  • startDueJobs starts the next job of every account that has none running and is due: connected, no recorded FLOOD_WAIT, its own 3 s delay passed. mainLoop calls it without awaiting. processJobs still awaits them all, for tests and one-shot use.
  • The 3 s delay still counts from the end of the account's last job. The lane cycle, the Backfill sync doesn't run #6/Retry failed sync jobs with backoff #9 retry rules and the per-account rate limits are unchanged.
  • The 1 s tick sleep ends as soon as the daemon is told to stop.
  • Shutdown: performCleanup calls requestShutdown first (also after a mainLoop that threw). cleanupScheduler waits 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.
  • The health check's getMe runs 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.)
  • If waiting for the jobs and closing the clients takes over 30 s, the daemon exits with code 1, as before for closing the clients; docs/daemon.md says so.
  • docs/daemon.md, docs/sync-strategy.md, docs/rate-limiting.md, docs/database-schema.md and docs/testing.md describe one job loop per account.

Tests

  • New: account 2 finishes two jobs while account 1's job sleeps in a FLOOD_WAIT. After the stop, account 1's row is back to how it was queued and the drain takes under 500 ms. This uses a blocked request, with no real wait.
  • New: in 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).
  • New: a stop ends the tick wait at once (under 500 ms; about 1000 ms without the fix).
  • New: the health check runs while a job is in flight, and a due reconnect waits for the job; cleanupScheduler waits 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).
  • The 3 s delay test records the tick of each run: both accounts at 0 s and 3 s, which the old rotation can't produce.
  • I confirmed that the new loop tests fail without the fix.
  • Updated: the fair-share test now runs 20 ticks instead of 40, because both accounts run on every tick. Its first six runs are still [1,2,1,2,1,2].
  • Run locally: bun run check, the full unit suite (1766 pass) and src/__e2e__/daemon.e2e.test.ts.

Risks

  • A FLOOD_WAIT on one account's health-check getMe still holds back new job starts for every account. That needs a follow-up issue, together with the sequential startup bootstrapDialogs.
  • API, CPU and SQLite load now grows with the number of busy accounts (at most 5).
  • refreshAllQueues and cleanupOldJobs can 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.
  • Sibling reliability PRs must not await per-account Telegram calls in mainLoop. They should use the same in-flight pattern and the drain.

Fixes #60

🤖 Generated with Claude Code

neoromantic and others added 5 commits October 22, 2026 19:16
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]>
@neoromantic
neoromantic merged commit 44924bd into main Oct 22, 2026
5 checks passed
@neoromantic
neoromantic deleted the feat/per-account-job-loops-60 branch October 22, 2026 17:44
@neoromantic neoromantic mentioned this pull request Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run each account's sync jobs independently in the daemon

1 participant