import gracefulShutdown from "http-graceful-shutdown"; import app from "./app"; import { initIO, getIO } from "./libs/socket"; import { logger } from "./utils/logger"; import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions"; import User from "./models/User"; import Whatsapp from "./models/Whatsapp"; import endPointQuery from "./helpers/EndPointQuery"; const server = app.listen(process.env.PORT, () => { logger.info(`Server started on port: ${process.env.PORT}`); }); // if (global.gc) { // console.log(">> Starting Garbage Collector..."); // global.gc(); // } else { // console.warn('No GC hook! Start your program as `node --expose-gc file.js`.'); // } initIO(server); // StartAllWhatsAppsSessions(); gracefulShutdown(server); (async () => { let whatsapps: any = await Whatsapp.findAll({ attributes: ['id', 'url'] }) // console.log('whatsapps: ', whatsapps) if (whatsapps && whatsapps.length > 0) { for (let i = 0; i < whatsapps.length; i++) { try { const response = await endPointQuery(`${whatsapps[i].dataValues.url}/api/status`, {}) if (!response) { throw new Error('Response null'); } } catch (error: any) { await whatsapps[i].update({ status: 'OPENING' }); console.log(`There was an error on try acess the api sessions ${whatsapps[i].dataValues.url}`) } await new Promise(f => setTimeout(f, 100)); } } })() setTimeout(async () => { const io = getIO(); console.log('Triggered socket!') let users = await User.findAll({ raw: true, attributes: ["id"], }) if (users && users.length > 0) { for (let i = 0; i < users.length; i++) { io.emit("reload_page", { action: "update", userId: users[i].id }); console.log('USER ID: ', users[i].id) await new Promise(f => setTimeout(f, 300)); } } }, 5000)