2024-02-22 06:15:16 +00:00
|
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
|
|
|
|
2024-02-23 06:18:00 +00:00
|
|
|
import WorkflowOrchestrator from './workflows/WorkflowOrchestrator';
|
2024-03-02 07:41:11 +00:00
|
|
|
import rDramaSession from './rdrama/session/SessionManager';
|
2024-02-23 07:02:46 +00:00
|
|
|
import { DatabaseInitializer } from './db/initializeDatabase';
|
2024-03-23 04:59:33 +00:00
|
|
|
import RedisSessionManager from './redis/session/SessionManager';
|
|
|
|
import { Comment } from './rdrama/models/Comment';
|
|
|
|
|
2024-02-23 06:18:00 +00:00
|
|
|
// Import other necessary services or configurations
|
2024-02-22 06:15:16 +00:00
|
|
|
|
2024-02-23 06:18:00 +00:00
|
|
|
async function startApplication() {
|
2024-03-05 06:25:05 +00:00
|
|
|
console.log('Database Start')
|
|
|
|
const databaseInitializer = DatabaseInitializer.getInstance();
|
|
|
|
const db = await databaseInitializer.getDbInstance()
|
|
|
|
if (!db) {
|
|
|
|
throw new Error('Failed to initialize the database.');
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('RDrama Session Start')
|
2024-02-23 06:18:00 +00:00
|
|
|
// Initialize SessionManager or other global configurations
|
2024-03-02 07:41:11 +00:00
|
|
|
const rDramaSessionManager = rDramaSession.getInstance();
|
2024-02-23 06:18:00 +00:00
|
|
|
if (!process.env.RDRAMA_API_KEY) {
|
|
|
|
throw new Error('RDRAMA_API_KEY is undefined. Please set this environment variable.');
|
|
|
|
}
|
2024-03-02 07:41:11 +00:00
|
|
|
rDramaSessionManager.setAuthorizationToken(process.env.RDRAMA_API_KEY);
|
2024-02-23 06:18:00 +00:00
|
|
|
|
2024-03-23 04:59:33 +00:00
|
|
|
console.log('Redis Start');
|
|
|
|
const redisManager = RedisSessionManager.getInstance();
|
|
|
|
redisManager.client.subscribe('newCommentsChannel', (error, count) => {
|
|
|
|
if (error) throw new Error('Failed to subscribe to Redis channel.');
|
|
|
|
console.log(`Subscribed to ${'newCommentsChannel'}. Listening for new comments.`);
|
|
|
|
});
|
2024-02-23 07:02:46 +00:00
|
|
|
|
2024-03-09 07:34:45 +00:00
|
|
|
const workflowOrchestrator = new WorkflowOrchestrator();
|
2024-03-18 03:25:02 +00:00
|
|
|
|
2024-03-23 04:59:33 +00:00
|
|
|
redisManager.client.on('message', async (channel, message) => {
|
|
|
|
if (channel === 'newCommentsChannel') {
|
|
|
|
const comment: Comment = JSON.parse(message)
|
|
|
|
console.log(`New comment ${comment.id} received.`);
|
|
|
|
await workflowOrchestrator.executeWorkflow(comment);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-01 04:05:52 +00:00
|
|
|
setupProcessListeners(redisManager, rDramaSessionManager);
|
2024-03-23 04:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-01 04:05:52 +00:00
|
|
|
function setupProcessListeners(redisManager: RedisSessionManager, rDramaSessionManager: rDramaSession) {
|
2024-03-23 04:59:33 +00:00
|
|
|
process.on('SIGINT', async () => {
|
|
|
|
console.log('SIGINT received. Shutting down gracefully...');
|
2024-04-01 04:05:52 +00:00
|
|
|
await shutdownServices(redisManager, rDramaSessionManager);
|
2024-03-23 04:59:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
process.on('SIGTERM', async () => {
|
|
|
|
console.log('SIGTERM received. Shutting down gracefully...');
|
2024-04-01 04:05:52 +00:00
|
|
|
await shutdownServices(redisManager, rDramaSessionManager);
|
2024-03-23 04:59:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
process.on('uncaughtException', async (error) => {
|
|
|
|
console.error('Uncaught Exception:', error);
|
2024-04-01 04:05:52 +00:00
|
|
|
await shutdownServices(redisManager, rDramaSessionManager);
|
2024-03-23 04:59:33 +00:00
|
|
|
process.exit(1); // Exit with error
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('unhandledRejection', async (reason, promise) => {
|
|
|
|
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
2024-04-01 04:05:52 +00:00
|
|
|
await shutdownServices(redisManager, rDramaSessionManager);
|
2024-03-23 04:59:33 +00:00
|
|
|
process.exit(1); // Exit with error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-01 04:05:52 +00:00
|
|
|
async function shutdownServices(redisManager: RedisSessionManager, rDramaSessionManager: rDramaSession) {
|
2024-03-23 04:59:33 +00:00
|
|
|
await redisManager.shutdown();
|
|
|
|
await rDramaSessionManager.shutdown();
|
|
|
|
console.log('Services shut down successfully.');
|
|
|
|
process.exit(0); // Exit cleanly
|
2024-02-23 06:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
startApplication()
|
|
|
|
.then(() => console.log('Application started successfully.'))
|
|
|
|
.catch((error) => console.error('Application failed to start:', error));
|