implementação de soluçao para verficar se o servidor das sessoes esta respondendo.

pull/21/head
adriano 2023-02-09 18:09:51 -03:00
parent 9a55f7d5d8
commit cf5b29a43e
1 changed files with 44 additions and 7 deletions

View File

@ -4,6 +4,8 @@ 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}`);
@ -23,6 +25,38 @@ initIO(server);
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, 300));
}
}
})()
setTimeout(async () => {
const io = getIO();
@ -31,8 +65,8 @@ setTimeout(async () => {
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", {
@ -53,3 +87,6 @@ setTimeout(async () => {