Atualização para o carregamento parcial das sessões do whatsapp quando cair a aplicação e subir novamente

pull/21/head
adriano 2022-12-29 22:01:57 -03:00
parent 56886611a7
commit 40dae953f4
4 changed files with 59 additions and 5 deletions

View File

@ -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) });
}

View File

@ -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)
}

View File

@ -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 });
}

View File

@ -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<void> => {
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
});
}
};