-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
50 lines (43 loc) · 1.32 KB
/
index.ts
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
import { rabbitMQService } from './src/services/rabbitmq-service';
import { rabbitmqConfig } from './src/config/rabbitmq-config';
import { logger } from './src/utils/logger';
async function connectWithRetry(attempt = 5): Promise<void> {
try {
await rabbitMQService.connect();
logger.info('Successfully connected to RabbitMQ');
} catch (error) {
logger.error(`Connection attempt ${attempt} failed:`, error);
if (attempt < rabbitmqConfig.retryAttempts) {
logger.info(`Retrying in ${rabbitmqConfig.retryDelay}ms...`);
await new Promise(resolve =>
setTimeout(resolve, rabbitmqConfig.retryDelay),
);
return connectWithRetry(attempt + 1);
}
throw error;
}
}
const start = async () => {
try {
await connectWithRetry();
await rabbitMQService.consume();
logger.info('Auth service is running');
} catch (err) {
logger.error('Failed to start service:', err);
process.exit(1);
}
};
process.on('SIGINT', async () => {
try {
await rabbitMQService.close();
logger.info('Gracefully shutting down');
process.exit(0);
} catch (error) {
logger.error('Error during shutdown:', error);
process.exit(1);
}
});
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
start();