feat(directory): run sync as a retryable background job#66
Merged
Conversation
syncDirectoryConnection was awaited inside the POST handler: timeout risk on large directories, no retry or recovery (#54). - New SyncJob table (status, attempts, lastError, runAfter) + migration. - enqueueSyncJob dedups against a pending/running job; the route now scopes the connection to the caller's company, enqueues, and returns 202 with the job id instead of blocking. - processSyncJobs claims due jobs with FOR UPDATE SKIP LOCKED (safe across instances), runs the sync, and on failure retries with exponential backoff (30s/60s/120s) up to maxAttempts before FAILED. - The route kicks the queue fire-and-forget; a scheduler can also drain it. Concurrency is now handled by the job claim, superseding the in-request advisory lock from #55. - Unit tests for backoff, enqueue dedup, and the success/retry/failure paths, plus the updated route behavior. Closes #54
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.
What
Moves directory sync out of the request cycle into a retryable job queue. Closes #54.
Why
syncDirectoryConnectionwas awaited insidePOST /api/directory/[id]/sync: large directories risk an HTTP timeout, and a failure left no retry or recovery.How
SyncJobtable (status, attempts, maxAttempts, lastError, runAfter, timestamps) + migration.enqueueSyncJobdedups against a pending/running job, and the handler returns 202 with the job id instead of blocking.processSyncJobsclaims due jobs withFOR UPDATE SKIP LOCKED(safe across instances), runs the sync, and on failure retries with exponential backoff (30s → 60s → 120s) up tomaxAttempts, then marksFAILED.Tests
backoffMs, enqueue dedup vs create, andprocessSyncJobssuccess / re-queue-with-backoff / exhausted-FAILED paths; updated route tests (auth, 404 scoping, 202 + job id).Notes
prisma validatepasses and the client generates cleanly. Runprisma migrate deployon deploy.Closes #54
🤖 Generated with Claude Code