Spaces:
Running
Running
| const mongoose = require('mongoose'); | |
| const app = require('./app'); | |
| const socketUtils = require('./utils/socket'); | |
| const DB = process.env.DATABASE.replace( | |
| '<PASSWORD>', | |
| process.env.DATABASE_PASSWORD, | |
| ); | |
| // CONNECT TO DATABASE | |
| mongoose | |
| .connect(DB, { | |
| maxPoolSize: 10, | |
| serverSelectionTimeoutMS: 5000, | |
| socketTimeoutMS: 45000, | |
| }) | |
| .then(() => console.log('DB connection successful!')) | |
| .catch((err) => { | |
| console.log('DB connection error:', err); | |
| }); | |
| // START SERVER | |
| const port = process.env.PORT || 8000; | |
| const server = app.listen(port, () => { | |
| console.log(`App running on port ${port}...`); | |
| }); | |
| const { deleteOldNotifications } = require('./utils/notificationService'); | |
| socketUtils.init(server); | |
| // Background cleanup job for notifications (runs every 24 hours) | |
| setInterval( | |
| () => { | |
| deleteOldNotifications().catch((err) => | |
| console.error( | |
| 'Failed to run periodic notification cleanup:', | |
| err.message, | |
| ), | |
| ); | |
| }, | |
| 24 * 60 * 60 * 1000, | |
| ); | |
| // Run cleanup once on startup | |
| deleteOldNotifications().catch((err) => | |
| console.error('Failed to run initial notification cleanup:', err.message), | |
| ); | |
| // Graceful shutdown on unhandled rejections | |
| process.on('unhandledRejection', (err) => { | |
| console.error('UNHANDLED REJECTION 💥 Shutting down...', err); | |
| server.close(() => { | |
| process.exit(1); | |
| }); | |
| }); | |