|
1 | 1 | import type { QueueConfig } from '@stacksjs/types' |
| 2 | +import { env } from '@stacksjs/env' |
2 | 3 |
|
3 | 4 | /** |
4 | | - * **Queue Options** |
| 5 | + * **Queue Configuration** |
5 | 6 | * |
6 | | - * This configuration defines all of your search engine options. Because Stacks is fully-typed, |
7 | | - * you may hover any of the options below and the definitions will be provided. In case |
8 | | - * you have any questions, feel free to reach out via Discord or GitHub Discussions. |
| 7 | + * This configuration defines all of your queue options. Stacks supports multiple |
| 8 | + * queue drivers including sync (immediate execution), database, Redis (via bun-queue), |
| 9 | + * and SQS. |
| 10 | + * |
| 11 | + * The Redis driver (powered by bun-queue) offers advanced features like: |
| 12 | + * - Distributed locks for job processing |
| 13 | + * - Horizontal scaling with leader election |
| 14 | + * - Rate limiting |
| 15 | + * - Dead letter queues |
| 16 | + * - Job priorities |
| 17 | + * - Cron scheduling |
| 18 | + * - Metrics collection |
| 19 | + * |
| 20 | + * Because Stacks is fully-typed, you may hover any of the options below and the |
| 21 | + * definitions will be provided. |
9 | 22 | */ |
10 | 23 | export default { |
11 | | - default: 'sync', |
| 24 | + // Default queue driver: 'sync' | 'database' | 'redis' | 'sqs' | 'memory' |
| 25 | + default: env.QUEUE_DRIVER || 'sync', |
12 | 26 |
|
13 | 27 | connections: { |
| 28 | + // Sync driver - executes jobs immediately (good for development) |
14 | 29 | sync: { |
15 | 30 | driver: 'sync', |
16 | 31 | }, |
17 | 32 |
|
18 | | - // database: { |
19 | | - // driver: 'database', |
20 | | - // table: 'jobs', |
21 | | - // queue: 'default', |
22 | | - // }, |
| 33 | + // Database driver - stores jobs in the database |
| 34 | + database: { |
| 35 | + driver: 'database', |
| 36 | + table: 'jobs', |
| 37 | + queue: 'default', |
| 38 | + retryAfter: 90, |
| 39 | + }, |
23 | 40 |
|
24 | | - // redis: { |
25 | | - // driver: 'redis', |
26 | | - // connection: 'default', |
27 | | - // queue: 'default', |
28 | | - // }, |
| 41 | + // Redis driver (powered by bun-queue) - high-performance Redis-backed queue |
| 42 | + redis: { |
| 43 | + driver: 'redis', |
| 44 | + // Redis connection settings |
| 45 | + redis: { |
| 46 | + url: env.REDIS_URL, |
| 47 | + host: env.REDIS_HOST || 'localhost', |
| 48 | + port: env.REDIS_PORT || 6379, |
| 49 | + password: env.REDIS_PASSWORD, |
| 50 | + db: env.REDIS_DB || 0, |
| 51 | + }, |
| 52 | + // Key prefix for all queue keys |
| 53 | + prefix: env.QUEUE_PREFIX || 'stacks:queue', |
| 54 | + // Worker concurrency (number of jobs processed simultaneously) |
| 55 | + concurrency: env.QUEUE_CONCURRENCY || 5, |
| 56 | + // Log level for queue operations |
| 57 | + logLevel: env.QUEUE_LOG_LEVEL || 'info', |
| 58 | + // Enable distributed locking for job processing |
| 59 | + distributedLock: true, |
| 60 | + // Stalled job check interval (ms) |
| 61 | + stalledJobCheckInterval: 30000, |
| 62 | + // Maximum retries for stalled jobs |
| 63 | + maxStalledJobRetries: 3, |
| 64 | + // Rate limiting configuration |
| 65 | + limiter: env.QUEUE_RATE_LIMIT_ENABLED ? { |
| 66 | + max: env.QUEUE_RATE_LIMIT_MAX || 100, |
| 67 | + duration: env.QUEUE_RATE_LIMIT_DURATION || 1000, |
| 68 | + } : undefined, |
| 69 | + // Metrics collection |
| 70 | + metrics: { |
| 71 | + enabled: env.QUEUE_METRICS_ENABLED || false, |
| 72 | + collectInterval: 10000, |
| 73 | + }, |
| 74 | + // Dead letter queue for failed jobs |
| 75 | + defaultDeadLetterOptions: { |
| 76 | + enabled: env.QUEUE_DLQ_ENABLED || true, |
| 77 | + maxRetries: env.QUEUE_DLQ_MAX_RETRIES || 3, |
| 78 | + queueSuffix: '-dead-letter', |
| 79 | + }, |
| 80 | + // Horizontal scaling (for multi-instance deployments) |
| 81 | + horizontalScaling: env.QUEUE_HORIZONTAL_SCALING_ENABLED ? { |
| 82 | + enabled: true, |
| 83 | + maxWorkersPerInstance: env.QUEUE_MAX_WORKERS || 10, |
| 84 | + jobsPerWorker: env.QUEUE_JOBS_PER_WORKER || 10, |
| 85 | + leaderElection: { |
| 86 | + heartbeatInterval: 5000, |
| 87 | + leaderTimeout: 15000, |
| 88 | + }, |
| 89 | + } : undefined, |
| 90 | + // Default job options |
| 91 | + defaultJobOptions: { |
| 92 | + attempts: 3, |
| 93 | + removeOnComplete: true, |
| 94 | + removeOnFail: false, |
| 95 | + backoff: { |
| 96 | + type: 'exponential', |
| 97 | + delay: 1000, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
29 | 101 |
|
| 102 | + // SQS driver - AWS Simple Queue Service |
30 | 103 | sqs: { |
31 | 104 | driver: 'sqs', |
32 | | - key: '', // set this if you |
33 | | - secret: '', |
34 | | - prefix: '', |
35 | | - suffix: '', |
| 105 | + key: env.AWS_ACCESS_KEY_ID || '', |
| 106 | + secret: env.AWS_SECRET_ACCESS_KEY || '', |
| 107 | + prefix: env.SQS_PREFIX || '', |
| 108 | + suffix: env.SQS_SUFFIX || '', |
36 | 109 | queue: 'default', |
37 | | - region: 'us-east-1', |
| 110 | + region: env.AWS_REGION || 'us-east-1', |
| 111 | + }, |
| 112 | + |
| 113 | + // Memory driver - in-memory queue (for testing) |
| 114 | + memory: { |
| 115 | + driver: 'memory', |
| 116 | + maxSize: 10000, |
38 | 117 | }, |
39 | 118 | }, |
| 119 | + |
| 120 | + // Failed jobs configuration |
| 121 | + failed: { |
| 122 | + driver: env.QUEUE_FAILED_DRIVER || 'database', |
| 123 | + table: 'failed_jobs', |
| 124 | + prefix: 'stacks:failed', |
| 125 | + }, |
| 126 | + |
| 127 | + // Batching configuration |
| 128 | + batching: { |
| 129 | + driver: 'redis', |
| 130 | + prefix: 'stacks:batches', |
| 131 | + }, |
| 132 | + |
| 133 | + // Worker configuration |
| 134 | + worker: { |
| 135 | + concurrency: env.QUEUE_WORKER_CONCURRENCY || 5, |
| 136 | + shutdownTimeout: 30000, |
| 137 | + }, |
40 | 138 | } satisfies QueueConfig |
0 commit comments