Criação do monitoramento da sessão

pull/1/head
adriano 2022-06-28 21:34:43 -03:00
parent c5c13f2b34
commit 7fd739f812
6 changed files with 110 additions and 49 deletions

View File

@ -25,6 +25,7 @@
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"fast-folder-size": "^1.7.0",
"fs-extra": "^10.1.0",
"http-graceful-shutdown": "^2.3.2",
"jsonwebtoken": "^8.5.1",

View File

@ -15,6 +15,7 @@ import AppError from "../errors/AppError";
import path from 'path';
interface WhatsappData {
name: string;
queueIds: number[];
@ -120,7 +121,7 @@ export const remove = async (
await DeleteWhatsAppService(whatsappId);
removeDir(path.join(process.cwd(),'.wwebjs_auth', `session-bd_${whatsappId}`))
removeDir(path.join(process.cwd(), '.wwebjs_auth', `session-bd_${whatsappId}`))
removeWbot(+whatsappId);

View File

@ -0,0 +1,23 @@
const fsPromises = require("fs/promises");
const fs = require('fs-extra')
// Delete a directory and its children
export const convertBytes = (bytes: number,) => {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"]
if (bytes == 0) {
return "n/a"
}
const i = +(Math.floor(Math.log(bytes) / Math.log(1024)))
if (i == 0) {
return bytes + " " + sizes[i]
}
// return (bytes / Math.pow(1024, i)).toFixed(1) + " " + sizes[i]
return (bytes / Math.pow(1024, i)).toFixed(1)
}

View File

@ -4,10 +4,16 @@ import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
import ListSchedulingNotifyService from "../services/SchedulingNotifyServices/ListSchedulingNotifyService";
import DeleteSchedulingNotifyService from "../services/SchedulingNotifyServices/DeleteSchedulingNotifyService";
import { getIO } from "../libs/socket";
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
import path from "path";
import { convertBytes } from "./ConvertBytes";
const fastFolderSize = require('fast-folder-size')
const { promisify } = require('util')
let scheduler_monitor:any;
let scheduler_monitor: any;
let timeInterval = 5
const monitor = async () => {
@ -15,7 +21,7 @@ const monitor = async () => {
let date = new Date()
let day = date.getDate().toString().padStart(2, '0');
let month = (date.getMonth()+1).toString().padStart(2, '0');
let month = (date.getMonth() + 1).toString().padStart(2, '0');
let year = date.getFullYear();
let hour = date.getHours()
@ -32,7 +38,7 @@ const monitor = async () => {
// console.log('schedulingNotifies: ',schedulingNotifies)
if(schedulingNotifies && schedulingNotifies.length>0){
if (schedulingNotifies && schedulingNotifies.length > 0) {
const ticket = await ShowTicketService(schedulingNotifies[0].ticketId);
@ -46,6 +52,37 @@ const monitor = async () => {
}
const whatsapps = await ListWhatsAppsService();
whatsapps.forEach(async whats => {
console.log('whats id: ', whats.id)
const sourcePath = path.join(__dirname, `../../.wwebjs_auth/`, `session-bd_${whats.id}`)
const fastFolderSizeAsync = promisify(fastFolderSize)
let size = await fastFolderSizeAsync(sourcePath)
size = convertBytes(size)
console.log(size)
// SESSION MONITORING
// const io = getIO();
// io.emit("whatsappSessionMonit", {
// action: "update",
// whatsappSessionSize: {
// id: whats.id,
// sessionSize: size
// }
// });
});
} catch (error) {
console.log('>>> SchedulingNotifiySendMessage.ts error: ', error)
stopSchedulingMonitor()
@ -55,7 +92,7 @@ const monitor = async () => {
};
export const startSchedulingMonitor =async (mileseconds: number) => {
export const startSchedulingMonitor = async (mileseconds: number) => {
timeInterval = mileseconds
@ -64,7 +101,7 @@ export const startSchedulingMonitor =async (mileseconds: number) => {
}
export const stopSchedulingMonitor =async ( ) => {
export const stopSchedulingMonitor = async () => {
clearInterval(scheduler_monitor)
@ -73,4 +110,3 @@ export const stopSchedulingMonitor =async ( ) => {

View File

@ -157,7 +157,7 @@ const monitor = async () => {
if (countTest == 0) {
try {
// console.log(' Carregando usuárion no listUserId...')
console.log(' Carregando usuárion no listUserId...')
listUserId = await ListUserParamiterService({ profile: 'user' })
} catch (error) {

View File

@ -16,4 +16,4 @@ StartAllWhatsAppsSessions();
gracefulShutdown(server);
startSchedulingMonitor(5000)
startWhoIsOnlineMonitor(2000)
startWhoIsOnlineMonitor(4000)