refactored index to redis with event listeners

master
j 2024-03-22 01:27:06 -04:00
parent 6c34cc77b8
commit 7e2a125570
1 changed files with 48 additions and 30 deletions

View File

@ -1,48 +1,66 @@
import dotenv from 'dotenv';
dotenv.config();
import WorkflowOrchestrator from './workflows/WorkflowOrchestrator';
import rDramaSession from './rdrama/session/SessionManager';
import redditSession from './reddit/session/SessionManager';
import { DatabaseInitializer } from './db/initializeDatabase';
import { DatabaseService } from './db/services/Database';
import { DatabaseMaintenanceService } from './db/services/DatabaseMaintenance';
// Import other necessary services or configurations
import RedisSessionManager from './redis/session/SessionManager';
import { CommentProcessor } from './rdrama/services/CommentProcessor';
const workflowInterval = 60000; // Run every 60,000 milliseconds (1 minute)
async function startApplication() {
console.log('Database Start')
const databaseInitializer = DatabaseInitializer.getInstance();
const db = await databaseInitializer.getDbInstance()
if (!db) {
throw new Error('Failed to initialize the database.');
}
const canSend = await DatabaseService.canSendNotification();
const coolDownHours = process.env.NOTIFICATION_COOLDOWN_HOURS
if (!canSend) {
console.log(`Last Message Sent less than ${coolDownHours ? coolDownHours : 4} hours ago. Set NOTIFICATION_COOLDOWN_HOURS to change this`)
return;
}
console.log('Redis Start');
const redisManager = RedisSessionManager.getInstance();
console.log('RDrama Session Start')
// Initialize SessionManager or other global configurations
console.log('RDrama Session Start');
const rDramaSessionManager = rDramaSession.getInstance();
if (!process.env.RDRAMA_API_KEY) {
throw new Error('RDRAMA_API_KEY is undefined. Please set this environment variable.');
}
rDramaSessionManager.setAuthorizationToken(process.env.RDRAMA_API_KEY);
console.log('Database Maintenance Start')
const databaseMaintenance = new DatabaseMaintenanceService()
await databaseMaintenance.runMaintenanceTasks()
console.log('Reddit Session Start')
await redditSession.getInstance()
// Initialize and start your workflow
const workflowOrchestrator = new WorkflowOrchestrator();
await workflowOrchestrator.executeWorkflow();
setInterval(async () => {
try {
await CommentProcessor.processComments();
console.log('Workflow executed successfully.');
} catch (error) {
console.error('An error occurred during workflow execution:', error);
}
}, workflowInterval);
await rDramaSessionManager.shutdown()
// Setup shutdown and error handling
setupProcessListeners(redisManager, rDramaSessionManager);
}
function setupProcessListeners(redisManager: RedisSessionManager, rDramaSessionManager: rDramaSession) {
process.on('SIGINT', async () => {
console.log('SIGINT received. Shutting down gracefully...');
await shutdownServices(redisManager, rDramaSessionManager);
});
process.on('SIGTERM', async () => {
console.log('SIGTERM received. Shutting down gracefully...');
await shutdownServices(redisManager, rDramaSessionManager);
});
process.on('uncaughtException', async (error) => {
console.error('Uncaught Exception:', error);
await shutdownServices(redisManager, rDramaSessionManager);
process.exit(1); // Exit with error
});
process.on('unhandledRejection', async (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
await shutdownServices(redisManager, rDramaSessionManager);
process.exit(1); // Exit with error
});
}
async function shutdownServices(redisManager: RedisSessionManager, rDramaSessionManager: rDramaSession) {
await redisManager.shutdown();
await rDramaSessionManager.shutdown();
console.log('Services shut down successfully.');
process.exit(0); // Exit cleanly
}
startApplication()