agentleFS
saizulsaizul/nodejs-backend-skills/skills/background-jobs/SKILL.md

background-jobs

Skill0 starsChanged 5 days agoMITView on GitHub

Background jobs for Node.js backends with BullMQ (and pointers to pg-boss/Graphile Worker) - what to move out of the request, queue and worker setup, retries with exponential backoff and UnrecoverableError, idempotency (deterministic jobId, provider idempotency keys, processed table, upserts), transactional outbox, dead-letter inspection and replay, rate limits and concurrency, job schedulers instead of setInterval/cron in web processes, graceful worker shutdown and Redis settings. Includes a checker for missing connections, Worker ioredis without maxRetriesPerRequest null, missing error/failed listeners, workers never closed, jobs without attempts/backoff/retention, whole request bodies in job data, missing jobIds and obsolete QueueScheduler/repeat usage, plus a jobs.ts template. Use when the user adds a queue, emails/webhooks/exports run in requests, jobs run twice or get lost, Redis memory grows, or asks about BullMQ, retries or idempotency.

10 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use query-optimization, containerization and 8 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • ffsshhttiikkSkill

    query-optimization

    Database query optimization

    Also in 4 projects that use this

  • mmondoraSkill

    containerization

    Docker best practices for cloud-native applications. Multi-stage builds, distroless images, security scanning, non-root users, Cloud Run orchestration. Use when writing Dockerfiles, configuring container builds, or deploying to Cloud Run/GKE.

    Also in 3 projects that use this

  • mmondoraSkill

    infrastructure-as-code

    Infrastructure as Code with Terraform and Pulumi. State management, modularity, drift detection, secrets handling. Use when provisioning cloud resources, writing Terraform/Pulumi modules, or managing infrastructure configuration.

    Also in 4 projects that use this

  • wpankSkill

    caching

    Caching strategies, invalidation, eviction policies, HTTP caching, distributed caching, and anti-patterns. Use when designing cache layers, choosing eviction policies, debugging stale data, or optimizing read-heavy workloads.

    Also in 4 projects that use this

  • harrissohail6Skill

    caching-strategies

    Caching patterns and strategies. Auto-activates when implementing caching, Redis, or performance optimization.

    Also in 3 projects that use this

  • SkeneTechnologiesSkill

    product-analytics

    When the user wants to set up product analytics -- including event taxonomy, tracking plans, funnel analysis, or tool selection (Mixpanel, Amplitude, PostHog). Also use when the user says "event tracking," "analytics setup," "tracking plan," "analytics implementation," or "user identification." For PLG metrics, see plg-metrics. For experimentation, see growth-experimentation.

    Also in 4 projects that use this

The file

Files
SKILL.md

Background jobs

At-least-once delivery is the default, so jobs must be safe to run twice; they must not be lost between the database write and the enqueue; and deploys must not cut them off.

Paths below are relative to this skill's folder (in Claude Code: ${CLAUDE_SKILL_DIR}).

When to use

  • "Move email / webhooks / PDF generation / exports to a queue"
  • "Jobs run twice", "jobs get lost on deploy", "stalled jobs", "Redis memory keeps growing"
  • Setting up BullMQ (Express, Fastify, NestJS @nestjs/bullmq), retries, schedules
  • Reviewing queue/worker code before production

When not to use

  • Event streaming at high volume (Kafka, NATS): different guarantees
  • Simple fire-and-forget that may be lost (analytics pings): a plain async call is fine
  • Error responses for HTTP requests: error-handling-patterns
  • PM2/systemd/Docker setup for the worker process itself: node-deploy

Workflow

  1. Decide what becomes a job (slow, flaky, CPU-heavy or must-retry work; see the first section of references/patterns.md). The request keeps validation and the defining write and returns 202 with an id.
  2. Check existing code (read-only).
    python3 scripts/check_bullmq.py src
    python3 scripts/check_bullmq.py src --json --min-severity low
    Exit 1 = findings at or above --fail-on (default medium).
  3. Set up or fix the basics from assets/jobs.ts (show the diff first):
    • explicit connection from REDIS_URL; Workers use ioredis with maxRetriesPerRequest: null
    • defaultJobOptions: attempts, exponential backoff, removeOnComplete, removeOnFail
    • job data holds ids, not request bodies or files
    • worker.on('error'), worker.on('failed') with final-failure alerting
    • SIGTERM: await worker.close() then queues and Redis
  4. Make every processor idempotent. For each side effect decide: deterministic jobId, provider idempotency key, processed-table check, or upsert. Write it down in the code next to the effect.
  5. Enqueue reliably. For jobs that must not be lost (payments, fulfilment), use the transactional outbox from the patterns reference; for best-effort jobs, enqueue after commit.
  6. Schedules: replace setInterval/node-cron in web processes and add({ repeat }) with upsertJobScheduler (idempotent across deploys and replicas).
  7. Operations: Redis maxmemory-policy noeviction, dashboards behind auth, alerts on failed count and queue depth, replay procedure for failed jobs. Run the checker again.

Output

Jobs review: api + worker (BullMQ 5.30) - 3 medium, 4 low
MED   src/queue.ts:4    Worker ioredis without maxRetriesPerRequest: null
MED   src/worker.ts:17  no worker.close() on SIGTERM -> active jobs stall on every deploy
MED   src/worker.ts:17  no 'error' listener
LOW   src/signup.ts:9   req.body stored in job data -> { userId } only
Idempotency: sendReceipt now keyed by jobId receipt-<orderId> + provider idempotency key
Outbox: orders + outbox row in one transaction; relay every 1s (diff below)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.