-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathworker.js
More file actions
74 lines (63 loc) · 2.26 KB
/
worker.js
File metadata and controls
74 lines (63 loc) · 2.26 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
const { loadConfig } = require('./src/config');
const { BackgroundWorkerService } = require('./src/services/backgroundWorkerService');
/**
* Standalone background worker process
* This can be run as a separate service from the main API
*/
async function startWorker() {
console.log('Starting SubStream Background Worker...');
const config = loadConfig();
// Check if RabbitMQ is configured
if (!config.rabbitmq || (!config.rabbitmq.url && !config.rabbitmq.host)) {
console.error('RabbitMQ configuration is missing. Please set RABBITMQ_URL or RABBITMQ_HOST environment variables.');
process.exit(1);
}
const worker = new BackgroundWorkerService(config.rabbitmq);
// Handle graceful shutdown
const shutdown = async (signal) => {
console.log(`\nReceived ${signal}. Shutting down gracefully...`);
try {
await worker.stop();
console.log('Background worker stopped successfully');
process.exit(0);
} catch (error) {
console.error('Error during shutdown:', error);
process.exit(1);
}
};
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
// Start the worker
try {
await worker.start();
console.log('Background worker started successfully');
console.log('Processing events from queues:');
console.log(` - Events: ${config.rabbitmq.eventQueue}`);
console.log(` - Notifications: ${config.rabbitmq.notificationQueue}`);
console.log(` - Emails: ${config.rabbitmq.emailQueue}`);
console.log(` - Leaderboard: ${config.rabbitmq.leaderboardQueue}`);
// Keep the process alive
process.stdin.resume();
} catch (error) {
console.error('Failed to start background worker:', error);
process.exit(1);
}
}
// Health check endpoint for monitoring
if (process.argv.includes('--health')) {
const config = loadConfig();
const worker = new BackgroundWorkerService(config.rabbitmq);
worker.start()
.then(() => {
const status = worker.getStatus();
console.log(JSON.stringify(status, null, 2));
process.exit(status.isRunning && status.connected ? 0 : 1);
})
.catch((error) => {
console.error('Health check failed:', error);
process.exit(1);
});
} else {
startWorker();
}