-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.ts
More file actions
54 lines (47 loc) · 1.62 KB
/
Copy pathworker.ts
File metadata and controls
54 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Standalone BullMQ Worker Process
*
* This file runs as a separate process/container dedicated to processing background jobs.
* Separating workers from the web server allows independent scaling and better resource isolation.
*
* Usage:
* - Development: bun run workers:dev
* - Production: bun run workers:start
*/
import {
deckGenerationQueue,
deckWorker,
gracefulShutdown,
scheduleAllJobs,
subscriptionQueue,
subscriptionWorker,
} from './lib/queue';
async function main() {
console.log('[WORKER] 🚀 Starting BullMQ worker process...\n');
try {
// Schedule all recurring jobs (idempotent - safe to call multiple times)
await scheduleAllJobs();
console.log('[WORKER] ✅ Worker process initialized and ready');
console.log('[WORKER] 📊 Active workers:');
console.log('[WORKER] - Subscriptions (concurrency: 2)');
console.log('[WORKER] - Deck Generation (concurrency: 2)\n');
// Store references for graceful shutdown
const workers = [subscriptionWorker, deckWorker];
const queues = [subscriptionQueue, deckGenerationQueue];
// Graceful shutdown handlers
process.on('SIGTERM', async () => {
console.log('[WORKER] 📛 Received SIGTERM, shutting down gracefully...');
await gracefulShutdown(workers, queues);
process.exit(0);
});
process.on('SIGINT', async () => {
console.log('[WORKER] 📛 Received SIGINT, shutting down gracefully...');
await gracefulShutdown(workers, queues);
process.exit(0);
});
} catch (error) {
console.error('[WORKER] ❌ Failed to start worker:', error);
process.exit(1);
}
}
main();