Sync
Immediate execution for development and testing.
@veloxts/queue provides background job processing with type-safe job definitions, retries, delays, and priorities.
pnpm add @veloxts/queue
# For BullMQ (production)pnpm add bullmq ioredisimport { queuePlugin } from '@veloxts/queue';
// Sync driver - jobs run immediately in-processapp.register(queuePlugin({ driver: 'sync',}));import { queuePlugin } from '@veloxts/queue';
// BullMQ driver - jobs queued in Redisapp.register(queuePlugin({ driver: 'bullmq', config: { url: process.env.REDIS_URL, prefix: 'myapp:queue:', defaultConcurrency: 5, },}));import { defineJob } from '@veloxts/queue';import { z } from 'zod';
export const SendWelcomeEmail = defineJob({ name: 'send-welcome-email', schema: z.object({ userId: z.string(), email: z.string().email(), }), handler: async ({ data, ctx, progress }) => { await progress(50); await ctx.mail.send(WelcomeEmail, { to: data.email, data: { ... } }); await progress(100); }, options: { attempts: 3, backoff: { type: 'exponential', delay: 1000 }, },});// Dispatch immediatelyawait ctx.queue.dispatch(SendWelcomeEmail, { userId: '123', email: 'user@example.com',});
// With delayawait ctx.queue.dispatch(SendWelcomeEmail, data, { delay: '10m',});
// With priority (lower = higher priority)await ctx.queue.dispatch(SendWelcomeEmail, data, { priority: 1,});
// Batch dispatchawait ctx.queue.dispatchBatch(SendWelcomeEmail, [ { userId: '1', email: 'a@example.com' }, { userId: '2', email: 'b@example.com' },]);defineJob({ name: 'my-job', schema: MySchema, handler: async ({ data }) => { ... }, options: { attempts: 3, // Retry count backoff: { type: 'exponential', // or 'fixed' delay: 1000, // Base delay (ms) }, priority: 1, // Lower = higher priority timeout: 30000, // Job timeout (ms) removeOnComplete: true, // Clean up completed removeOnFail: false, // Keep failed for inspection },});Sync
Immediate execution for development and testing.
BullMQ
Redis-backed queues for production with retries and monitoring.
| Feature | Sync | BullMQ |
|---|---|---|
| Persistent jobs | No | Yes |
| Retries with backoff | No | Yes |
| Delayed jobs | No | Yes |
| Priority queues | No | Yes |
| Job progress | No | Yes |
| Multiple workers | No | Yes |
app.register(queuePlugin({ driver: 'bullmq', config: { url: process.env.REDIS_URL, prefix: 'myapp:queue:', defaultConcurrency: 5, },}));REDIS_URL=redis://user:password@your-redis-host:6379In production, run workers as separate processes:
import { createWorker } from '@veloxts/queue';import { SendWelcomeEmail, ProcessOrder } from './jobs';
const worker = await createWorker({ connection: { url: process.env.REDIS_URL }, jobs: [SendWelcomeEmail, ProcessOrder], concurrency: 5,});
// Graceful shutdownprocess.on('SIGTERM', async () => { await worker.close(); process.exit(0);});| Provider | Best For |
|---|---|
| Upstash | Serverless, pay-per-request |
| Redis Cloud | Managed Redis clusters |
| Railway | Simple Redis add-on |