From 40dae953f4f9919ffcb957e4155439751e07a2c0 Mon Sep 17 00:00:00 2001 From: adriano Date: Thu, 29 Dec 2022 22:01:57 -0300 Subject: [PATCH] =?UTF-8?q?Atualiza=C3=A7=C3=A3o=20para=20o=20carregamento?= =?UTF-8?q?=20parcial=20das=20sess=C3=B5es=20do=20whatsapp=20quando=20cair?= =?UTF-8?q?=20a=20aplica=C3=A7=C3=A3o=20e=20subir=20novamente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/helpers/AutoRestore.ts | 5 +- .../helpers/SchedulingNotifySendMessage.ts | 2 +- .../WbotServices/SendWhatsAppMessage.ts | 3 ++ .../WbotServices/StartAllWhatsAppsSessions.ts | 54 ++++++++++++++++++- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/backend/src/helpers/AutoRestore.ts b/backend/src/helpers/AutoRestore.ts index e4abf5c..e2c3b87 100644 --- a/backend/src/helpers/AutoRestore.ts +++ b/backend/src/helpers/AutoRestore.ts @@ -15,7 +15,7 @@ import { number } from "yargs"; const autoRestore = async (whatsappId: string | number, started_action_by: string = '') => { const whatsapp = await ShowWhatsAppService(whatsappId); - + // console.log('>>>>>>>>>>> Whatsapp.id: ', whatsappId) await restartWhatsSession(whatsapp, true) @@ -37,7 +37,8 @@ const autoRestore = async (whatsappId: string | number, started_action_by: strin number = '' } - fs.writeFileSync(`${sourcePath}/${timestamp}_restore_${number}_triggered_by_${started_action_by}_id_${whatsappId}.txt`, `Whatsapp id: ${whatsapp.id} \nDate: ${dateToday.fullDate} ${dateToday.fullTime}`, (error: any) => { console.log(error) }); + if (started_action_by.trim().length > 0) + fs.writeFileSync(`${sourcePath}/${timestamp}_restore_${number}_triggered_by_${started_action_by}_id_${whatsappId}.txt`, `Whatsapp id: ${whatsapp.id} \nDate: ${dateToday.fullDate} ${dateToday.fullTime}`, (error: any) => { console.log(error) }); } diff --git a/backend/src/helpers/SchedulingNotifySendMessage.ts b/backend/src/helpers/SchedulingNotifySendMessage.ts index d46584d..bd50be4 100644 --- a/backend/src/helpers/SchedulingNotifySendMessage.ts +++ b/backend/src/helpers/SchedulingNotifySendMessage.ts @@ -138,7 +138,7 @@ const monitor = async () => { } else { - console.log('Directory not found to get size info: ', sourcePath) + // console.log('Directory not found to get size info: ', sourcePath) } diff --git a/backend/src/services/WbotServices/SendWhatsAppMessage.ts b/backend/src/services/WbotServices/SendWhatsAppMessage.ts index 9b42891..69e08fc 100644 --- a/backend/src/services/WbotServices/SendWhatsAppMessage.ts +++ b/backend/src/services/WbotServices/SendWhatsAppMessage.ts @@ -63,6 +63,7 @@ const SendWhatsAppMessage = async ({ listWhatsapp = await ListWhatsAppsNumber(ticket.whatsappId, 'CONNECTED') } + if (listWhatsapp.length > 1) { @@ -70,6 +71,8 @@ const SendWhatsAppMessage = async ({ const _whatsapp = listWhatsapp[Math.floor(Math.random() * listWhatsapp.length)]; + console.log('_whatsapp_whatsapp_whatsapp_whatsapp_whatsapp_whatsapp_whatsapp_whatsapp: ',_whatsapp) + await ticket.update({ whatsappId: +_whatsapp.id }); } diff --git a/backend/src/services/WbotServices/StartAllWhatsAppsSessions.ts b/backend/src/services/WbotServices/StartAllWhatsAppsSessions.ts index 9e5e935..0b2862f 100644 --- a/backend/src/services/WbotServices/StartAllWhatsAppsSessions.ts +++ b/backend/src/services/WbotServices/StartAllWhatsAppsSessions.ts @@ -1,11 +1,61 @@ +import path from "path"; +import autoRestore from "../../helpers/AutoRestore"; +import { removeDir } from "../../helpers/DeleteDirectory"; +import { insertOrUpeateWhatsCache, searchWhatsappCache } from "../../helpers/WhatsCache"; +import { getIO } from "../../libs/socket"; import ListWhatsAppsService from "../WhatsappService/ListWhatsAppsService"; import { StartWhatsAppSession } from "./StartWhatsAppSession"; +const fs = require('fs') + +let counter = 0 export const StartAllWhatsAppsSessions = async (): Promise => { + const whatsapps = await ListWhatsAppsService(); + + if (whatsapps.length > 0) { - whatsapps.forEach(whatsapp => { - StartWhatsAppSession(whatsapp); + + whatsapps.forEach(async whatsapp => { + + // StartWhatsAppSession(whatsapp); + + const sourcePath = path.join(process.cwd(), `.wwebjs_auth/`, `session-bd_${whatsapp.id}`) + + if (fs.existsSync(sourcePath)) { + + await removeDir(sourcePath) + + console.log('Directory deleted on start process: ', sourcePath) + } + else { + console.log('Directory not found to delete on start process: ', sourcePath) + } + + await whatsapp.update({ status: "RESTORING" }); + await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { + status: "RESTORING", + }) + + const io = getIO(); + io.emit("whatsappSession", { + action: "update", + session: whatsapp + }); + + console.log('1 counter: ', counter) + + setTimeout(async () => { + + await autoRestore(whatsapp.id,) + + }, counter) + + + + counter += 57000 + }); + } };