2022-01-06 01:26:15 +00:00
|
|
|
import gracefulShutdown from "http-graceful-shutdown";
|
|
|
|
import app from "./app";
|
2023-02-07 15:47:40 +00:00
|
|
|
import { initIO, getIO } from "./libs/socket";
|
2022-01-06 01:26:15 +00:00
|
|
|
import { logger } from "./utils/logger";
|
|
|
|
import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions";
|
2023-02-09 21:09:51 +00:00
|
|
|
import User from "./models/User";
|
|
|
|
import Whatsapp from "./models/Whatsapp";
|
|
|
|
import endPointQuery from "./helpers/EndPointQuery";
|
2023-02-10 14:03:23 +00:00
|
|
|
import { cacheSize, flushCache, loadTicketsCache } from "./helpers/TicketCache";
|
|
|
|
import { loadContactsCache } from "./helpers/ContactsCache";
|
|
|
|
import { loadSchedulesCache } from "./helpers/SchedulingNotifyCache";
|
|
|
|
import { delRestoreControllFile } from "./helpers/RestoreControll";
|
|
|
|
|
|
|
|
import "./helpers/SchedulingNotifySendMessage"
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const server = app.listen(process.env.PORT, () => {
|
|
|
|
logger.info(`Server started on port: ${process.env.PORT}`);
|
|
|
|
});
|
|
|
|
|
2022-12-31 06:12:26 +00:00
|
|
|
|
2023-02-09 21:14:39 +00:00
|
|
|
// 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`.');
|
|
|
|
// }
|
2023-02-09 21:09:51 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
initIO(server);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-01-25 20:44:02 +00:00
|
|
|
// StartAllWhatsAppsSessions();
|
2022-01-06 01:26:15 +00:00
|
|
|
gracefulShutdown(server);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
|
2023-02-09 21:09:51 +00:00
|
|
|
(async () => {
|
|
|
|
|
|
|
|
let whatsapps: any = await Whatsapp.findAll({ attributes: ['id', 'url'] })
|
|
|
|
|
2023-02-09 21:17:19 +00:00
|
|
|
// console.log('whatsapps: ', whatsapps)
|
2023-02-09 21:09:51 +00:00
|
|
|
|
|
|
|
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) {
|
2023-02-10 14:03:23 +00:00
|
|
|
|
2023-02-09 21:09:51 +00:00
|
|
|
await whatsapps[i].update({ status: 'OPENING' });
|
|
|
|
|
|
|
|
console.log(`There was an error on try acess the api sessions ${whatsapps[i].dataValues.url}`)
|
|
|
|
}
|
|
|
|
|
2023-02-10 12:39:50 +00:00
|
|
|
await new Promise(f => setTimeout(f, 100));
|
2023-02-09 21:09:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:03:23 +00:00
|
|
|
|
|
|
|
if (process.env.CACHE) {
|
|
|
|
|
|
|
|
const cacheLength = await cacheSize()
|
|
|
|
|
|
|
|
console.log('cacheSize: ', cacheLength)
|
|
|
|
|
|
|
|
if (cacheLength == 0) {
|
|
|
|
console.log('Loading from cache...')
|
|
|
|
await flushCache()
|
|
|
|
await loadContactsCache()
|
|
|
|
await loadTicketsCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
await loadSchedulesCache()
|
|
|
|
// await loadWhatsappCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
delRestoreControllFile()
|
2023-02-09 21:09:51 +00:00
|
|
|
})()
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
setTimeout(async () => {
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
|
|
|
|
console.log('Triggered socket!')
|
|
|
|
|
|
|
|
let users = await User.findAll({ raw: true, attributes: ["id"], })
|
|
|
|
|
|
|
|
if (users && users.length > 0) {
|
2023-02-09 21:09:51 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
for (let i = 0; i < users.length; i++) {
|
2023-02-09 21:09:51 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
io.emit("reload_page", {
|
|
|
|
action: "update",
|
|
|
|
userId: users[i].id
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('USER ID: ', users[i].id)
|
|
|
|
|
2023-02-10 12:39:50 +00:00
|
|
|
await new Promise(f => setTimeout(f, 300));
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
}
|
2023-02-09 21:09:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
}, 5000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-02-09 21:09:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|