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";
|
2022-11-19 22:34:39 +00:00
|
|
|
|
|
|
|
import "./helpers/CloseBotTickets";
|
2023-02-10 14:03:23 +00:00
|
|
|
import { loadSchedulesCache } from "./helpers/SchedulingNotifyCache";
|
2022-12-14 12:29:42 +00:00
|
|
|
import { delRestoreControllFile } from "./helpers/RestoreControll";
|
2023-02-13 23:29:55 +00:00
|
|
|
|
2023-02-10 14:03:23 +00:00
|
|
|
import "./helpers/SchedulingNotifySendMessage"
|
2023-04-08 21:31:37 +00:00
|
|
|
import axios from "axios";
|
|
|
|
import os from 'os';
|
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);
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-11-19 22:34:39 +00:00
|
|
|
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
(async()=>{
|
|
|
|
|
2023-04-08 21:31:37 +00:00
|
|
|
console.log('os.tmpdir(): ', os.tmpdir())
|
|
|
|
|
2023-02-09 21:09:51 +00:00
|
|
|
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 {
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2023-04-08 21:31:37 +00:00
|
|
|
console.log(`API URL: ${whatsapps[i].dataValues.url}/api/connection/status`)
|
2022-11-08 23:16:39 +00:00
|
|
|
|
2023-04-08 21:31:37 +00:00
|
|
|
const response = await axios.get(`${whatsapps[i].dataValues.url}/api/connection/status`, {});
|
|
|
|
|
|
|
|
console.log(`-------> Response: ${response.data.data}`)
|
2023-02-09 21:09:51 +00:00
|
|
|
|
|
|
|
if (!response) {
|
|
|
|
throw new Error('Response null');
|
|
|
|
}
|
|
|
|
|
2023-04-08 21:31:37 +00:00
|
|
|
if (response.data.data && response.data.data == 'CONNECTED') {
|
|
|
|
await whatsapps[i].update({ status: 'CONNECTED' });
|
|
|
|
}
|
|
|
|
|
2023-02-09 21:09:51 +00:00
|
|
|
} 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
|
|
|
|
|
|
|
}
|
2022-10-25 14:16:36 +00:00
|
|
|
}
|
2022-12-18 16:56:45 +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()
|
2022-10-25 14:16:36 +00:00
|
|
|
})()
|
2022-12-18 16:56:45 +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-04-10 12:08:09 +00:00
|
|
|
await new Promise(f => setTimeout(f, 100));
|
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
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
|
|
|
|
|