Criação do restoreMonit

pull/21/head
adriano 2023-01-04 12:17:59 -03:00
parent 10d15930da
commit 72087f8adb
3 changed files with 95 additions and 85 deletions

View File

@ -0,0 +1,86 @@
import Whatsapp from "../models/Whatsapp"
import autoRestore from "./AutoRestore"
import { getRestoreControll } from "./RestoreControll"
let lstAutoRestore: any = []
// Delete a directory and its children
export const restoreMonit = (whatsappId: string | number) => {
try {
let lstRestore: any = getRestoreControll()
console.log('ENTROU NO AUTO RESTORE..................................')
if (Object.keys(lstRestore.filter((e: any) => +e.id == +whatsappId)).length) {
console.log('Restore executed by human whatasappId: ', whatsappId)
}
else {
lstAutoRestore.push({ 'whatsappId': +whatsappId })
setTimeout(async () => {
lstRestore = getRestoreControll()
let count = lstAutoRestore.length
for (let i = 0; i < count; i++) {
let autoR = lstAutoRestore.shift()
console.log('----------------> autoR: ', autoR)
if (autoR && autoR.whatsappId) {
if (Object.keys(lstRestore.filter((e: any) => +e.id == +autoR.whatsappId)).length) {
console.log(' EXECUTING RESTORING autoR: ', autoR)
continue
}
const _whatsapp = await Whatsapp.findOne({ where: { id: autoR.whatsappId } });
console.log('----------------> autoR exec after 180 seconds: ', autoR)
let whatsappStatus = ["CONFLICT",
"DEPRECATED_VERSION",
"OPENING",
"PAIRING",
"PROXYBLOCK",
"SMB_TOS_BLOCK",
"TIMEOUT",
"TOS_BLOCK",
"UNLAUNCHED",
"UNPAIRED",
"UNPAIRED_IDLE"]
if (_whatsapp?.status) {
if (whatsappStatus.includes(_whatsapp.status)) {
await autoRestore(autoR.whatsappId, 'auto_monit')
}
}
}
}
console.log('888888888888888888 ENTROU NO SET TIMEOUT -----------> ')
}, 180000);
}
} catch (error) {
console.log('There was an error on try execute AUTO-RESTORE: ', error)
}
}

View File

@ -21,11 +21,6 @@ import ptBR from 'date-fns/locale/pt-BR';
import path from "path"; import path from "path";
const fs = require('fs') const fs = require('fs')
let lstAutoRestore: any = []
export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: boolean = false): Promise<void> => { export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: boolean = false): Promise<void> => {
await whatsapp.update({ status: "OPENING" }); await whatsapp.update({ status: "OPENING" });

View File

@ -24,6 +24,7 @@ import { splitDateTime } from "../../helpers/SplitDateTime";
import { format } from "date-fns"; import { format } from "date-fns";
import ptBR from 'date-fns/locale/pt-BR'; import ptBR from 'date-fns/locale/pt-BR';
import path from "path"; import path from "path";
import { restoreMonit } from "../../helpers/RestoreMonit";
const fs = require('fs') const fs = require('fs')
@ -46,81 +47,6 @@ const wbotMonitor = async (
console.log('>>>>>>>>>>>>>> change_state wbotMonitor.ts MOBILE NUMBER: ', wbot.info["wid"]["user"]) console.log('>>>>>>>>>>>>>> change_state wbotMonitor.ts MOBILE NUMBER: ', wbot.info["wid"]["user"])
try {
let lstRestore: any = getRestoreControll()
if (Object.keys(lstRestore.filter((e: any) => +e.id == +whatsapp.id)).length) {
console.log('Restore executed by human whatasappId: ', whatsapp.id)
}
else {
lstAutoRestore.push({ 'whatsappId': +whatsapp.id })
setTimeout(async () => {
lstRestore = getRestoreControll()
let count = lstAutoRestore.length
for (let i = 0; i < count; i++) {
let autoR = lstAutoRestore.shift()
console.log('----------------> autoR: ', autoR)
if (autoR && autoR.whatsappId) {
if (Object.keys(lstRestore.filter((e: any) => +e.id == +autoR.whatsappId)).length) {
console.log(' EXECUTING RESTORING autoR: ', autoR)
continue
}
const _whatsapp = await Whatsapp.findOne({ where: { id: autoR.whatsappId } });
console.log('----------------> autoR exec after 180 seconds: ', autoR)
let whatsappStatus = ["CONFLICT",
"DEPRECATED_VERSION",
"OPENING",
"PAIRING",
"PROXYBLOCK",
"SMB_TOS_BLOCK",
"TIMEOUT",
"TOS_BLOCK",
"UNLAUNCHED",
"UNPAIRED",
"UNPAIRED_IDLE"]
if (_whatsapp?.status) {
if (whatsappStatus.includes(_whatsapp.status)) {
await autoRestore(autoR.whatsappId, 'auto_monit')
}
}
}
}
console.log('888888888888888888 ENTROU NO SET TIMEOUT -----------> ')
}, 180000);
}
} catch (error) {
console.log('There was an error on try execute AUTO-RESTORE: ', error)
}
try { try {
await whatsapp.update({ status: newState }); await whatsapp.update({ status: newState });
// await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: newState }) // await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: newState })
@ -133,6 +59,9 @@ const wbotMonitor = async (
action: "update", action: "update",
session: whatsapp session: whatsapp
}); });
restoreMonit(whatsapp.id)
}); });
wbot.on("change_battery", async batteryInfo => { wbot.on("change_battery", async batteryInfo => {