fix(workers): atomically claim BackgroundJob batches with FOR UPDATE SKIP LOCKED in DbJobPoller
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 55/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- postgresql, typescript
- Área
- backend, databases, distributed-systems
Línea de trabajo
Start with apps/workers/src/services/db-job-poller.ts, then read its tests in apps/workers/src/services/db-job-poller.spec.ts and the BackgroundJob model in packages/prisma/schema.prisma. Check how the two queue lanes filter jobs and how retries and soft rescheduling update attempts. Done when replicas claim disjoint batches atomically and the existing status, locking, lane, retry, and rescheduling behavior is covered by updated tests.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Problem Description
In apps/workers/src/services/db-job-poller.ts, pollRealtimeQueue (lines 129-140) and pollMediaQueue (lines 158-169) fetch the top PENDING rows with prisma.backgroundJob.findMany({ take: batchSize }), and then claimAndExecute (lines 183-197) claims each row individually via prisma.backgroundJob.updateMany({ where: { id: candidate.id, status: 'PENDING' } }).
When running multiple worker replicas at 1k-10k+ user scale, every replica fetches the exact same top 20 PENDING rows and races on updateMany, wasting DB queries and delaying jobs #21+ until the first batch is claimed.
Expected Behavior
- Each worker replica atomically claims its own disjoint batch of
PENDINGjobs in a single PostgreSQL query (FOR UPDATE SKIP LOCKED), so replicas never contend on the same rows. - All existing
BackgroundJobcolumns (status = 'PROCESSING',lockedAt,lockedBy,startedAt,attempts), retry/backoff counts, andREALTIMEvsMEDIAlane filtering continue to work identically locally (1 worker) and in production (multiple replicas).
Where to Look
apps/workers/src/services/db-job-poller.ts(pollRealtimeQueue,pollMediaQueue,claimAndExecute, lines 129-314)apps/workers/src/services/db-job-poller.spec.tspackages/prisma/schema.prisma(BackgroundJob, lines 348-370)
Solution / Implementation Guide
- Replace
findMany+ per-rowupdateManywith an atomic PostgreSQL claim query usingprisma.$queryRaw+Prisma.join(REALTIME_JOB_TYPES)(note:BackgroundJobhas noupdatedAtcolumn):UPDATE "BackgroundJob" SET "status" = 'PROCESSING', "lockedAt" = $now, "lockedBy" = $workerId, "startedAt" = $now, "attempts" = "attempts" + 1 WHERE "id" IN ( SELECT "id" FROM "BackgroundJob" WHERE "status" = 'PENDING' AND "runAfter" <= $now AND "jobType" IN (...) -- or NOT IN (...) for MEDIA lane ORDER BY "priority" ASC, "createdAt" ASC LIMIT $batchSize FOR UPDATE SKIP LOCKED ) RETURNING *; - Update
attemptsmath for post-incrementRETURNING *rows indb-job-poller.ts:- Soft reschedule (line 286): revert
attempts: Math.max(0, job.attempts - 1)instead ofcandidate.attempts(sinceRETURNING *already incrementedattempts). - Hard failure retry/backoff (lines 299-300): change
candidate.attempts + 1 >= candidate.maxAttemptstojob.attempts >= job.maxAttemptsandMath.pow(2, candidate.attempts + 1)toMath.pow(2, job.attempts)so jobs do not lose a retry.
- Soft reschedule (line 286): revert
- Update unit tests (
db-job-poller.spec.ts):- Update mocks from
prisma.backgroundJob.findManytoprisma.$queryRawand verify retry exhaustion and soft-rescheduleattemptspreservation.
- Update mocks from
- Lenguaje dominante
- TypeScript
- Estrellas
- 2
- Forks
- 1
- Merge medio
- 6 min
- PR fusionados (30 d)
- 27
Preparar el entorno
- Incluye un Dockerfile o un archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de sumamakhan761/tryinstance
-
backend good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
sumamakhan761/tryinstance#65 ·
Los mantenedores suelen responder en 1 día
-
backend good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
sumamakhan761/tryinstance#64 ·
Los mantenedores suelen responder en 1 día
-
backend good first issue
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
sumamakhan761/tryinstance#63 ·
Los mantenedores suelen responder en 1 día
-
backend good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
sumamakhan761/tryinstance#62 ·
Los mantenedores suelen responder en 1 día
-
backend good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
sumamakhan761/tryinstance#60 ·
Los mantenedores suelen responder en 1 día
Todos los issues de sumamakhan761/tryinstance
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
makecindy/cindy#15342 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
kwitsch/claude-code-plugin-tooling#219 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día
-
no-repo-link tool-submission
Dificultad 1/5 Menos de una hora Aptitud para principiantes 74/100
BraveOPotato/NoSignups#2618 ·
-
enhancement good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100