|
| 1 | +--- |
| 2 | +title: Queue Configuration |
| 3 | +description: Configure bun-queue with various options for Redis, rate limiting, metrics, and more. |
| 4 | +--- |
| 5 | + |
| 6 | +# Queue Configuration |
| 7 | + |
| 8 | +bun-queue provides extensive configuration options to customize queue behavior. |
| 9 | + |
| 10 | +## Basic Configuration |
| 11 | + |
| 12 | +```typescript |
| 13 | +import { Queue } from 'bun-queue' |
| 14 | + |
| 15 | +const queue = new Queue('tasks', { |
| 16 | + redis: { |
| 17 | + url: 'redis://username:password@localhost:6379' |
| 18 | + }, |
| 19 | + prefix: 'myapp', // Prefix for Redis keys (default: 'queue') |
| 20 | + verbose: true, // Enable verbose logging |
| 21 | + logLevel: 'info', // Log level: 'debug' | 'info' | 'warn' | 'error' | 'silent' |
| 22 | +}) |
| 23 | +``` |
| 24 | + |
| 25 | +## Redis Configuration |
| 26 | + |
| 27 | +### Connection String |
| 28 | + |
| 29 | +```typescript |
| 30 | +const queue = new Queue('tasks', { |
| 31 | + redis: { |
| 32 | + url: 'redis://localhost:6379' |
| 33 | + } |
| 34 | +}) |
| 35 | +``` |
| 36 | + |
| 37 | +### Custom Redis Client |
| 38 | + |
| 39 | +```typescript |
| 40 | +const queue = new Queue('tasks', { |
| 41 | + redis: { |
| 42 | + client: myRedisClient // Provide your own client |
| 43 | + } |
| 44 | +}) |
| 45 | +``` |
| 46 | + |
| 47 | +### Environment Variables |
| 48 | + |
| 49 | +bun-queue reads these environment variables: |
| 50 | + |
| 51 | +- `REDIS_URL`: Redis connection string (default: `redis://localhost:6379`) |
| 52 | + |
| 53 | +```bash |
| 54 | +# .env |
| 55 | +REDIS_URL=redis://localhost:6379 |
| 56 | +``` |
| 57 | + |
| 58 | +## Default Job Options |
| 59 | + |
| 60 | +Set default options for all jobs in a queue: |
| 61 | + |
| 62 | +```typescript |
| 63 | +const queue = new Queue('tasks', { |
| 64 | + defaultJobOptions: { |
| 65 | + attempts: 5, |
| 66 | + backoff: { |
| 67 | + type: 'exponential', |
| 68 | + delay: 5000 |
| 69 | + }, |
| 70 | + removeOnComplete: true, |
| 71 | + removeOnFail: false, |
| 72 | + timeout: 60000 |
| 73 | + } |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +## Rate Limiting |
| 78 | + |
| 79 | +Configure rate limiting to control job processing speed: |
| 80 | + |
| 81 | +```typescript |
| 82 | +const queue = new Queue('api-calls', { |
| 83 | + limiter: { |
| 84 | + max: 100, // Maximum jobs per duration |
| 85 | + duration: 60000, // Duration in milliseconds (1 minute) |
| 86 | + keyPrefix: (data) => data.userId // Optional: rate limit per key |
| 87 | + } |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +### Key-Based Rate Limiting |
| 92 | + |
| 93 | +Rate limit based on job data: |
| 94 | + |
| 95 | +```typescript |
| 96 | +const queue = new Queue('api-calls', { |
| 97 | + limiter: { |
| 98 | + max: 10, |
| 99 | + duration: 1000, |
| 100 | + keyPrefix: (data) => `user:${data.userId}` // Limit per user |
| 101 | + } |
| 102 | +}) |
| 103 | +``` |
| 104 | + |
| 105 | +## Metrics Configuration |
| 106 | + |
| 107 | +Enable built-in metrics collection: |
| 108 | + |
| 109 | +```typescript |
| 110 | +const queue = new Queue('tasks', { |
| 111 | + metrics: { |
| 112 | + enabled: true, |
| 113 | + collectInterval: 5000 // Collect metrics every 5 seconds |
| 114 | + } |
| 115 | +}) |
| 116 | + |
| 117 | +// Get metrics |
| 118 | +const metrics = await queue.getMetrics() |
| 119 | +console.log(metrics) |
| 120 | +``` |
| 121 | + |
| 122 | +## Stalled Job Detection |
| 123 | + |
| 124 | +Configure detection and handling of stalled jobs: |
| 125 | + |
| 126 | +```typescript |
| 127 | +const queue = new Queue('tasks', { |
| 128 | + stalledJobCheckInterval: 30000, // Check every 30 seconds |
| 129 | + maxStalledJobRetries: 3 // Retry stalled jobs up to 3 times |
| 130 | +}) |
| 131 | +``` |
| 132 | + |
| 133 | +## Distributed Lock |
| 134 | + |
| 135 | +Enable distributed locking for job processing: |
| 136 | + |
| 137 | +```typescript |
| 138 | +const queue = new Queue('tasks', { |
| 139 | + distributedLock: true // Enable distributed locks (default) |
| 140 | +}) |
| 141 | +``` |
| 142 | + |
| 143 | +## Dead Letter Queue |
| 144 | + |
| 145 | +Configure automatic dead letter queue handling: |
| 146 | + |
| 147 | +```typescript |
| 148 | +const queue = new Queue('tasks', { |
| 149 | + defaultDeadLetterOptions: { |
| 150 | + enabled: true, |
| 151 | + queueSuffix: '-dead-letter', // Suffix for dead letter queue name |
| 152 | + maxRetries: 3, // Move to DLQ after 3 failures |
| 153 | + removeFromOriginalQueue: true |
| 154 | + } |
| 155 | +}) |
| 156 | +``` |
| 157 | + |
| 158 | +## Horizontal Scaling |
| 159 | + |
| 160 | +Configure horizontal scaling for distributed processing: |
| 161 | + |
| 162 | +```typescript |
| 163 | +const queue = new Queue('tasks', { |
| 164 | + horizontalScaling: { |
| 165 | + enabled: true, |
| 166 | + instanceId: 'worker-1', // Unique ID for this instance |
| 167 | + maxWorkersPerInstance: 10, |
| 168 | + jobsPerWorker: 10, |
| 169 | + leaderElection: { |
| 170 | + heartbeatInterval: 5000, |
| 171 | + leaderTimeout: 30000 |
| 172 | + }, |
| 173 | + workCoordination: { |
| 174 | + pollInterval: 1000, |
| 175 | + keyPrefix: 'coordinator' |
| 176 | + } |
| 177 | + } |
| 178 | +}) |
| 179 | +``` |
| 180 | + |
| 181 | +### Cluster Information |
| 182 | + |
| 183 | +Get information about the cluster: |
| 184 | + |
| 185 | +```typescript |
| 186 | +// Check if this instance is the leader |
| 187 | +const isLeader = queue.isLeader() |
| 188 | + |
| 189 | +// Get current leader ID |
| 190 | +const leaderId = await queue.getLeaderId() |
| 191 | + |
| 192 | +// Get cluster information |
| 193 | +const clusterInfo = await queue.getClusterInfo() |
| 194 | +``` |
| 195 | + |
| 196 | +## Full Configuration Example |
| 197 | + |
| 198 | +```typescript |
| 199 | +import { Queue } from 'bun-queue' |
| 200 | + |
| 201 | +const queue = new Queue('production-tasks', { |
| 202 | + // Redis connection |
| 203 | + redis: { |
| 204 | + url: process.env.REDIS_URL || 'redis://localhost:6379' |
| 205 | + }, |
| 206 | + |
| 207 | + // Key prefix |
| 208 | + prefix: 'myapp', |
| 209 | + |
| 210 | + // Logging |
| 211 | + verbose: true, |
| 212 | + logLevel: 'info', |
| 213 | + |
| 214 | + // Default job options |
| 215 | + defaultJobOptions: { |
| 216 | + attempts: 3, |
| 217 | + backoff: { |
| 218 | + type: 'exponential', |
| 219 | + delay: 2000 |
| 220 | + }, |
| 221 | + removeOnComplete: 100, // Keep last 100 completed jobs |
| 222 | + removeOnFail: false, |
| 223 | + timeout: 30000 |
| 224 | + }, |
| 225 | + |
| 226 | + // Rate limiting |
| 227 | + limiter: { |
| 228 | + max: 1000, |
| 229 | + duration: 60000 |
| 230 | + }, |
| 231 | + |
| 232 | + // Metrics |
| 233 | + metrics: { |
| 234 | + enabled: true, |
| 235 | + collectInterval: 10000 |
| 236 | + }, |
| 237 | + |
| 238 | + // Stalled jobs |
| 239 | + stalledJobCheckInterval: 30000, |
| 240 | + maxStalledJobRetries: 3, |
| 241 | + |
| 242 | + // Distributed lock |
| 243 | + distributedLock: true, |
| 244 | + |
| 245 | + // Dead letter queue |
| 246 | + defaultDeadLetterOptions: { |
| 247 | + enabled: true, |
| 248 | + maxRetries: 5 |
| 249 | + }, |
| 250 | + |
| 251 | + // Horizontal scaling |
| 252 | + horizontalScaling: { |
| 253 | + enabled: true, |
| 254 | + instanceId: process.env.INSTANCE_ID || 'default', |
| 255 | + maxWorkersPerInstance: 10, |
| 256 | + jobsPerWorker: 5 |
| 257 | + } |
| 258 | +}) |
| 259 | +``` |
| 260 | + |
| 261 | +## Queue Management |
| 262 | + |
| 263 | +### Pause and Resume |
| 264 | + |
| 265 | +```typescript |
| 266 | +// Pause the queue |
| 267 | +await queue.pause() |
| 268 | + |
| 269 | +// Resume the queue |
| 270 | +await queue.resume() |
| 271 | +``` |
| 272 | + |
| 273 | +### Health Check |
| 274 | + |
| 275 | +```typescript |
| 276 | +// Check Redis connection |
| 277 | +const isHealthy = await queue.ping() |
| 278 | +console.log('Queue healthy:', isHealthy) |
| 279 | +``` |
| 280 | + |
| 281 | +### Close Queue |
| 282 | + |
| 283 | +```typescript |
| 284 | +// Gracefully close the queue |
| 285 | +await queue.close() |
| 286 | +``` |
0 commit comments