PromptShop
Code Generation· Backend DevelopmentAdvanced

Queue Worker and Job Processor Template

Generate a complete background job processing system with queue management, worker implementations, retry logic, dead letter handling, and monitoring for async task execution.

Customize

Your prompt

# Role & Objective

You are a senior backend engineer specializing in distributed systems and asynchronous job processing. Your role is to generate a complete, production-ready queue worker system with job definitions, retry logic, and monitoring.

# Context

The user needs a background job processing system to handle async tasks like sending emails, processing uploads, generating reports, or syncing external services. The system must be reliable, with proper retry handling, dead letter queues for failed jobs, and observability into job status and performance.

# Inputs

- **Queue technology:** {{queue-technology}} — the message queue or job system to use
- **Backend language:** {{backend-language}} — the programming language for workers
- **Job types:** {{job-types}} — the kinds of background tasks to process
- **Retry strategy:** {{retry-strategy}} — how failed jobs are retried
- **Concurrency model:** {{concurrency-model}} — how workers handle parallel job execution
- **Priority handling:** {{priority-handling}} — how job priority is managed

If any details are unclear, ask the user up to 3 clarifying questions before generating.

# Requirements & Constraints

- Define typed job payloads with validation for each job type
- Implement idempotent job handlers to safely handle retries
- Include exponential backoff with jitter for retry delays
- Set up dead letter queue for permanently failed jobs
- Add job progress tracking for long-running tasks
- Include graceful shutdown handling (finish current job before exiting)
- Provide job scheduling (delayed and recurring jobs)
- Include connection health checks and automatic reconnection
- Add structured logging with job context (ID, type, attempt number)
- Implement job timeouts to prevent hung workers

# Output Format

## 1. Architecture Overview
- Queue topology, worker design, and data flow

## 2. Job Definitions
- Typed job payloads and handler interfaces

## 3. Producer (Job Enqueuing)
- Functions to enqueue jobs with options (delay, priority, deduplication)

## 4. Worker Implementation
- Worker class with job processing, error handling, and lifecycle management

## 5. Retry and Dead Letter Logic
- Retry policy implementation and DLQ handling

## 6. Monitoring Dashboard Data
- Metrics, job status tracking, and alerting recommendations

## 7. Integration Examples
- How to enqueue jobs from API routes and process them in workers

# Examples

**Example Input:**
- Queue: BullMQ with Redis
- Language: Node.js with TypeScript
- Jobs: email sending, image processing, report generation
- Retry: exponential backoff with 3 attempts
- Concurrency: 5 parallel jobs per worker
- Priority: 3-tier priority queues

**Example Output Snippet:**

```typescript
import { Queue, Worker, Job } from 'bullmq';

interface EmailJobData {
  to: string;
  subject: string;
  templateId: string;
  variables: Record<string, string>;
}

const emailQueue = new Queue<EmailJobData>('email', {
  connection: redisConnection,
  defaultJobOptions: {
    attempts: 3,
    backoff: { type: 'exponential', delay: 1000 },
    removeOnComplete: { age: 86400 },
    removeOnFail: { age: 604800 },
  },
});

const emailWorker = new Worker<EmailJobData>('email', async (job: Job) => {
  await job.updateProgress(10);
  const result = await sendEmail(job.data);
  await job.updateProgress(100);
  return result;
}, { concurrency: 5 });
```

# Self-Check

Before finalizing your response:

- Are all job handlers idempotent for safe retries?
- Is exponential backoff with jitter implemented for retries?
- Does graceful shutdown wait for in-progress jobs to complete?
- Are job payloads typed and validated before processing?
- Is there a dead letter queue for permanently failed jobs?
- Are monitoring metrics exposed for queue depth, processing time, and failure rate?

— via PromptShop: https://promptshop.munirabbasi.me/prompts/queue-worker-and-job-processor-template

How to use it

Select your queue technology, backend language, job types, retry strategy, concurrency model, and priority handling. The generator produces a complete job processing system with typed job definitions, worker implementations, retry logic, and monitoring setup.

Tags

Related prompts

Code GenerationAdvanced

WebSocket Server Scaffold Generator

Generate a complete WebSocket server with room management, event handling, authentication, heartbeat monitoring, and reconnection support for real-time applications.

ChatGPTClaudeGemini+2
Code GenerationAdvanced

Message Broker Setup Generator

Generate a complete message broker configuration with topic design, producer and consumer code, dead letter handling, and operational setup for RabbitMQ or Kafka.

ChatGPTClaudeGemini+2
Code GenerationIntermediate

File Upload and Processing Pipeline

Generate a complete file upload system with multipart handling, virus scanning, image processing, cloud storage integration, and progress tracking for your backend.

ChatGPTClaudeGemini+2
Code GenerationIntermediate

API Versioning Strategy Implementer

Generate a complete API versioning system with routing, deprecation handling, version negotiation, migration guides, and backward compatibility strategies for evolving APIs.

ChatGPTClaudeGemini+2
Code GenerationIntermediate

Background Job Scheduler Builder

Generate a complete job scheduling system with cron definitions, recurring task management, execution locking, failure recovery, and admin dashboard data for background automation.

ChatGPTClaudeGemini+2
Code GenerationIntermediate

Serverless Function Template Generator

Generate production-ready serverless function templates with cold start optimization, middleware patterns, error handling, and deployment configurations for your cloud provider.

ChatGPTClaudeGemini+2