Repository navigation
fix(daemon): retry failed sync jobs with backoff - #39
Merged
Merged
Conversation
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
force-pushed
the
fix/sync-retries-9
branch
from
October 21, 2026 17:23
24f1368 to
4eca9eb
Compare
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
Every error marked a sync job
failed, including a FLOOD_WAIT, and the daemon only logged it. The only retry was hidden:refreshQueuescounted only pending and running jobs, so 30 seconds later it queued the chat again, with no backoff and no limit. A permanent error such asCHANNEL_PRIVATEor a missing InputPeer was retried forever, each try left a failed row that nothing cleaned up (cleanupFailedhad no callers), andtg statusalways showedfailed_jobs: 0.Design
sync_jobsgetsattemptsandnext_run_at(ms, 0 = due now). They are added to 0.1.4 databases withensureColumn, in the same column order as a fresh table.getNextPending,getNextPendingOfTypesandclaimNextJobskip jobs that aren't due yet. When every job in a lane is waiting, its slot goes to the next lane.src/daemon/sync-retry.ts, reusingcalculateReconnectDelay). The fifth failure marks itfailed. 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_0too) and uses no attempt. Every lane still pauses.CHANNEL_PRIVATEwith 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.retryLaterandfailOrDropeach run one guardedUPDATEand then aDELETE, which serialize with the IMMEDIATE transaction intg chats sync.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 addsclearedFailedJobs.tg statuscounts failed jobs for real and addsretrying_jobs. It lists the 5 newest failed jobs, and each job gainsattempts,error,next_run_atand its chat'ssync_mode. Pretty output gets Retrying and Failed rows and prints onetg chats sync <chat_id>command per chat with a failed job, with the flag of its mode (--autofor an auto chat), because a baretg chats syncturns an auto chat on. All fields are additions.tg statusfor the retry command. A FLOOD_WAIT deferral is debug only.JobExecutorandscheduler.failJobare unchanged. The README and its "How it compares" table are unchanged too.Tests
sync-retry.test.tscovers the backoff and which errors are permanent.sync-failing-chats.test.tsruns the real scheduler, worker and job loop with a fake clock:failedwith 5 attempts. After that, 100 refresh cycles make no call and add no row.CHANNEL_PRIVATEgets one call, then exactly one more after the 24 h cleanup.retryLater,deleteFailed,countJobsand cleanupprint-schemafor the new columnsfailOrDrop, 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.CHANNEL_PRIVATEfails at once.status.e2e.test.ts(JSON and pretty: Retrying, attempt and "next in" lines, the 5-newest limit,next_run_at: nullfor a due retry, and the printed retry commands, which delete the failed jobs and keep each chat's mode) and extendedchats-sync.e2e.test.ts. Both use made-up IDs and no credentials.failed_jobsis a number.bun run check,bun run test(1469) andbun run test:e2e(166) pass locally. The live integration suite was not run in this change.Fixes #9
🤖 Generated with Claude Code