161 lines
4.4 KiB
TypeScript
161 lines
4.4 KiB
TypeScript
import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead";
|
|
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
|
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')
|
|
const fs = require('fs')
|
|
|
|
const { exec } = require("child_process");
|
|
|
|
let scheduler_monitor: any;
|
|
let timeInterval = 5
|
|
|
|
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 year = date.getFullYear();
|
|
|
|
let hour = date.getHours()
|
|
let minute = date.getMinutes()
|
|
|
|
let fullDate = `${year}-${month}-${day}`;
|
|
let dateParm = `${fullDate} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:00`
|
|
|
|
//console.log(dateParm);
|
|
|
|
try {
|
|
|
|
const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" });
|
|
|
|
// console.log('schedulingNotifies: ',schedulingNotifies)
|
|
|
|
if (schedulingNotifies && schedulingNotifies.length > 0) {
|
|
|
|
const ticket = await ShowTicketService(schedulingNotifies[0].ticketId);
|
|
|
|
SetTicketMessagesAsRead(ticket);
|
|
|
|
await SendWhatsAppMessage({
|
|
body: schedulingNotifies[0].message, ticket
|
|
});
|
|
|
|
DeleteSchedulingNotifyService(schedulingNotifies[0].id)
|
|
|
|
}
|
|
|
|
|
|
exec("df -h /", (error: any, stdout: any, stderr: any) => {
|
|
|
|
if (error) {
|
|
console.log(`exec error: ${error.message}`);
|
|
return;
|
|
}
|
|
if (stderr) {
|
|
console.log(`exec stderr: ${stderr}`);
|
|
return;
|
|
}
|
|
|
|
stdout = stdout.split(/\r?\n/)
|
|
stdout = stdout[1].trim().split(/\s+/)
|
|
|
|
// DISK SPACE MONITORING
|
|
const io = getIO();
|
|
io.emit("diskSpaceMonit", {
|
|
action: "update",
|
|
diskSpace: {
|
|
size: stdout[1],
|
|
used: stdout[2],
|
|
available: stdout[3],
|
|
use: stdout[4]
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// WHATS SESSION SIZE MONITORING
|
|
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}`)
|
|
|
|
if (fs.existsSync(sourcePath)) {
|
|
|
|
try {
|
|
// console.log('dir path exist!')
|
|
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 (err) {
|
|
console.log('An error occurred while get info from the directory: ', err);
|
|
}
|
|
|
|
}
|
|
else {
|
|
|
|
console.log('Directory not found to get size info: ', sourcePath)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (error) {
|
|
console.log('>>> SchedulingNotifiySendMessage.ts error: ', error)
|
|
stopSchedulingMonitor()
|
|
startSchedulingMonitor(timeInterval)
|
|
}
|
|
|
|
};
|
|
|
|
|
|
export const startSchedulingMonitor = async (mileseconds: number) => {
|
|
|
|
timeInterval = mileseconds
|
|
|
|
scheduler_monitor = setInterval(monitor, mileseconds)
|
|
|
|
}
|
|
|
|
|
|
export const stopSchedulingMonitor = async () => {
|
|
|
|
clearInterval(scheduler_monitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
|