Skip to content

fix(daemon): retry failed sync jobs with backoff - #39

Merged
neoromantic merged 10 commits into
mainfrom
fix/sync-retries-9
Oct 21, 2026
Merged

neoromantic merged 10 commits into
mainfrom
fix/sync-retries-9

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Problem

Every error marked a sync job failed, including a FLOOD_WAIT, and the daemon only logged it. The only retry was hidden: refreshQueues counted only pending and running jobs, so 30 seconds later it queued the chat again, with no backoff and no limit. A permanent error such as CHANNEL_PRIVATE or a missing InputPeer was retried forever, each try left a failed row that nothing cleaned up (cleanupFailed had no callers), and tg status always showed failed_jobs: 0.

Design

  • Schema: sync_jobs gets attempts and next_run_at (ms, 0 = due now). They are added to 0.1.4 databases with ensureColumn, in the same column order as a fresh table.
  • Selectors: getNextPending, getNextPendingOfTypes and claimNextJob skip jobs that aren't due yet. When every job in a lane is waiting, its slot goes to the next lane.
  • Backoff: a failed job goes back to pending with the same ID after 1, 2, 4 and 8 minutes (src/daemon/sync-retry.ts, reusing calculateReconnectDelay). The fifth failure marks it failed. The retry takes its chat's current lane priority, so a chat turned on while its first page ran gets the retry at P0.
  • FLOOD_WAIT: delays the job by the wait (at least 1 s, so FLOOD_WAIT_0 too) and uses no attempt. Every lane still pauses.
  • Permanent errors fail at once: RPC 400, 403 and 404, CHANNEL_PRIVATE with any code, a missing InputPeer and an unknown job type. 401 and 406 are account errors that the health check owns, so they retry like everything else.
  • Failed rows keep their chat's slot for that job type, so refreshes stop queueing the chat. The daemon deletes them after 24 h (in the ~5 minute cleanup, which now covers failed rows) and all of them on start. A crash still doesn't count as an attempt.
  • Turned-off chats: if a chat is turned off while its job runs and the job then fails or hits a FLOOD_WAIT, the job is deleted, whatever the error or attempt: not retried and not kept as failed. retryLater and failOrDrop each run one guarded UPDATE and then a DELETE, which serialize with the IMMEDIATE transaction in tg chats sync.
  • No stuck jobs: a result with no error is retried, so no path leaves a job running.
  • tg chats sync <chat> deletes the chat's failed jobs in any mode, even when the mode didn't change. Run with the chat's current mode flag, that is the per-chat retry; a running daemon queues the chat at its next refresh. The output adds clearedFailedJobs.
  • tg status counts failed jobs for real and adds retrying_jobs. It lists the 5 newest failed jobs, and each job gains attempts, error, next_run_at and its chat's sync_mode. Pretty output gets Retrying and Failed rows and prints one tg chats sync <chat_id> command per chat with a failed job, with the flag of its mode (--auto for an auto chat), because a bare tg chats sync turns an auto chat on. All fields are additions.
  • Daemon log: it logs by outcome. A retry is a warning with "attempt N of 5" and the error. A job that failed for good is a warning with the reason that points to tg status for the retry command. A FLOOD_WAIT deferral is debug only.

JobExecutor and scheduler.failJob are unchanged. The README and its "How it compares" table are unchanged too.

Tests

  • New sync-retry.test.ts covers the backoff and which errors are permanent.
  • New sync-failing-chats.test.ts runs the real scheduler, worker and job loop with a fake clock:
    • A transient error gets calls only at t0, +1, +2, +4 and +8 minutes, then the job is failed with 5 attempts. After that, 100 refresh cycles make no call and add no row.
    • CHANNEL_PRIVATE gets one call, then exactly one more after the 24 h cleanup.
    • A chat turned off while its page is in flight is dropped, on a transient and on a permanent error.
    • No scenario leaves a duplicate open row.
  • Updated tests:
    • The 0.1.4 migration, including 4 processes migrating at once
    • Selectors and the lane fallback
    • retryLater, deleteFailed, countJobs and cleanup
    • Startup clearing failed rows
    • Refresh not requeueing failed chats
    • The FLOOD_WAIT fair-share case (no failed rows, the same job ID completes)
    • Daemon log lines
    • print-schema for the new columns
  • failOrDrop, and the real job path dropping a turned-off chat's job on a transient error, a FLOOD_WAIT, a permanent error and the last attempt.
  • A retry after a mode change takes the new priority; a handler that throws CHANNEL_PRIVATE fails at once.
  • New e2e status.e2e.test.ts (JSON and pretty: Retrying, attempt and "next in" lines, the 5-newest limit, next_run_at: null for a due retry, and the printed retry commands, which delete the failed jobs and keep each chat's mode) and extended chats-sync.e2e.test.ts. Both use made-up IDs and no credentials.
  • The integration status check now asserts that failed_jobs is a number.

bun run check, bun run test (1469) and bun run test:e2e (166) pass locally. The live integration suite was not run in this change.

Fixes #9

🤖 Generated with Claude Code

neoromantic and others added 10 commits October 21, 2026 21:16
Every error marked a sync job failed, even a FLOOD_WAIT, and the only
retry was hidden: the next refresh, 30 seconds later, queued the chat
again, with no backoff and no limit. A permanent error such as
CHANNEL_PRIVATE or a missing InputPeer was retried forever, and each try
left a failed row that nothing cleaned up.

sync_jobs gets attempts and next_run_at (added to 0.1.4 databases). A
job that fails is pending again with the same ID after 1, 2, 4 and 8
minutes, and the fifth failure marks it failed. A FLOOD_WAIT, including
FLOOD_WAIT_0, only delays the job and uses no attempt. 400, 403 and 404
RPC errors, CHANNEL_PRIVATE, a missing InputPeer and an unknown job type
fail at once; 401 and 406 stay transient because the health check owns
account errors. A failed row now holds its chat's slot for that job
type, so refreshes stop queueing it; the daemon deletes failed rows
after 24 hours and on start. A job whose chat was turned off while it
ran is dropped instead of retried, and no path leaves a job running.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A failed sync job now keeps the daemon from queueing its chat for that
job type until the row is cleaned up a day later or the daemon
restarts. That is the right default for a permanent error, but it left
no way to retry one chat, and turning a chat off and on again stayed
blocked by its failed row.

tg chats sync deletes the chat's failed jobs inside its existing
transaction, in any mode and even when the mode didn't change, so
running it again is the per-chat retry. The output reports how many it
cleared as clearedFailedJobs, and the daemon's log for a job that
failed for good points to it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tg status always reported failed_jobs: 0, because the value was
hard-coded, so a chat whose sync kept failing looked healthy. Now that a
failed job blocks its chat until it is cleaned up, the failures need to
be visible.

The sync counts come from one query: failed_jobs is the real count, and
the new retrying_jobs counts pending jobs that failed at least once
(pending_jobs still includes them). The job list adds the 5 newest
failed jobs, and every job gains attempts, error and next_run_at. The
pretty output gets Retrying and Failed rows, shows each failed job's
error and how it is retried, and prints the queue as empty only when
nothing is pending, running or failed. All fields are additions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Only the retry path checked that the chat was still enabled. A permanent
error or the fifth failure called plain markFailed, so a chat turned off
with tg chats sync --off while its job ran kept a failed row. tg status
then listed it with a retry hint that turned the chat back on.

failOrDrop marks the job failed only while its chat is enabled and deletes
it otherwise, like retryLater, so every way a job ends drops it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
retryLater put a running job back at the priority it was queued with.
tg chats sync cancels only pending jobs, so a chat turned on while its
first page ran got the retry at its old tier, and the pending row kept
the refresh from queueing it at P0. Before retries, the failed job was
queued again at P0 by the next refresh.

The retry now takes the chat's lane priority, with the same expression
as alignPendingPriorities.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The retry hint in tg status, the daemon's failed-for-good warning and the
docs said to run bare tg chats sync <chat>. Without a flag that command
sets the mode to on, and most failed jobs belong to auto chats, so
following the hint pinned the chat on, raised its priority, stopped it
following the member-count policy and cancelled its other pending jobs.
The hint also said "now", but the daemon retries at its next refresh.

Each listed job now has its chat's sync_mode, and the pretty output prints
one tg chats sync command per chat with a failed job, with the flag of
its mode. The daemon warning points to tg status. The docs explain the
flag and the JSON example matches its listing rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
No test checked the Retrying row, the "attempt N of 5, next in" line or a
deferred job's "next in" line, so removing any of them passed, though
docs/testing.md says the pretty output is covered. The limit of 5 failed
jobs and the null next_run_at of a retry whose backoff ended weren't
pinned either.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…error

Every permanent-error test went through the jobs' own catch blocks, and the
only throwing-handler test threw a plain Error. failJobResult could report
every thrown error as transient and the suite still passed, though the
core worker rethrows its errors into that path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The permanent RPC error cases ran only a backward history job, so
turning off the permanent check in forward catch-up went unnoticed.
Forward catch-up is the job that meets CHANNEL_PRIVATE most often: a
synced channel turns private or removes the user, and the job would go
back to five transient retries. The cases now run for forward catch-up,
backward history and initial load, and check that the API call threw.
The transient forward catch-up test now also checks the error is not
permanent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The pretty tests that print retry commands listed only failed jobs, so
nothing noticed when the command list stopped filtering on the failed
status: tg status would then suggest tg chats sync for chats whose jobs
are only running or pending. The failed jobs test now also lists a
running and a pending job for two other chats with a sync mode, and
still expects only the two failed chats' commands.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 7fa776e into main Oct 21, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/sync-retries-9 branch October 21, 2026 17:24
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.

Retry failed sync jobs with backoff

1 participant