File size: 1,446 Bytes
14960b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// src/index.js - Main Entry Point
require('dotenv').config(); // Ensure env variables are loaded first

const logger = require('./logger');
const { connectDB } = require('./database');
const { connectWhatsApp } = require('./connection');

/**
 * Starts the application by connecting to the database
 * and then establishing the WhatsApp connection.
 */
async function start() {
    logger.info("===================================");
    logger.info("      Starting WhatsApp Bot        ");
    logger.info("===================================");
    try {
        await connectDB();
        await connectWhatsApp();
    } catch (error) {
        logger.fatal({ err: error }, "Critical error during application startup sequence.");
        process.exit(1); // Exit if essential connections fail
    }
}

// Execute startup sequence
start().catch(err => {
    // This catch is unlikely to be hit if errors in start() are handled
    // but provides a final safety net.
    logger.fatal({ err: err }, "FATAL UNHANDLED ERROR during startup!");
    process.exit(1);
});

// Optional: Graceful shutdown handling
process.on('SIGINT', () => {
    logger.warn("Received SIGINT. Shutting down gracefully...");
    // Add cleanup logic here if needed (e.g., close DB connection)
    process.exit(0);
});
process.on('SIGTERM', () => {
    logger.warn("Received SIGTERM. Shutting down gracefully...");
    // Add cleanup logic here
    process.exit(0);
});