2022-06-29 00:34:43 +00:00
|
|
|
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";
|
2022-12-18 14:45:41 +00:00
|
|
|
import { deleteScheduleByTicketIdCache } from "./SchedulingNotifyCache";
|
|
|
|
import SchedulingNotify from "../models/SchedulingNotify";
|
2022-06-29 00:34:43 +00:00
|
|
|
|
|
|
|
const fastFolderSize = require('fast-folder-size')
|
|
|
|
const { promisify } = require('util')
|
2022-06-29 01:27:17 +00:00
|
|
|
const fs = require('fs')
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
const { exec } = require("child_process");
|
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
let _fifo: any
|
|
|
|
|
2022-06-29 00:34:43 +00:00
|
|
|
let scheduler_monitor: any;
|
2022-05-16 04:03:16 +00:00
|
|
|
let timeInterval = 5
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-06-29 00:34:43 +00:00
|
|
|
const monitor = async () => {
|
2022-03-01 03:27:17 +00:00
|
|
|
|
|
|
|
let date = new Date()
|
|
|
|
|
|
|
|
let day = date.getDate().toString().padStart(2, '0');
|
2022-06-29 00:34:43 +00:00
|
|
|
let month = (date.getMonth() + 1).toString().padStart(2, '0');
|
|
|
|
let year = date.getFullYear();
|
2022-03-01 03:27:17 +00:00
|
|
|
|
|
|
|
let hour = date.getHours()
|
2022-06-29 00:34:43 +00:00
|
|
|
let minute = date.getMinutes()
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-06-29 00:34:43 +00:00
|
|
|
let fullDate = `${year}-${month}-${day}`;
|
2022-03-01 03:27:17 +00:00
|
|
|
let dateParm = `${fullDate} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:00`
|
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
//console.log(dateParm);
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
try {
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" });
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
if (schedulingNotifies && schedulingNotifies.length > 0) {
|
2022-12-18 14:45:41 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < schedulingNotifies.length; i++) {
|
|
|
|
|
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
const ticket = await ShowTicketService(+schedulingNotifies[i].ticketId);
|
|
|
|
|
|
|
|
await new Promise(f => setTimeout(f, 3000));
|
|
|
|
|
|
|
|
if(!ticket.queue){
|
|
|
|
await ticket.update({status: 'open'})
|
|
|
|
}
|
2022-05-18 00:36:55 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
// SetTicketMessagesAsRead(ticket);
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
await SendWhatsAppMessage({
|
|
|
|
body: schedulingNotifies[i].message, ticket
|
|
|
|
});
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
await deleteScheduleByTicketIdCache(schedulingNotifies[i].ticketId)
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
await DeleteSchedulingNotifyService(schedulingNotifies[i].id)
|
2022-12-18 14:45:41 +00:00
|
|
|
|
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
}
|
2022-06-29 00:34:43 +00:00
|
|
|
|
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
stdout = stdout.split(/\r?\n/)
|
|
|
|
stdout = stdout[1].trim().split(/\s+/)
|
|
|
|
|
2022-07-28 17:55:23 +00:00
|
|
|
// DISK SPACE MONITORING
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("diskSpaceMonit", {
|
|
|
|
action: "update",
|
|
|
|
diskSpace: {
|
|
|
|
size: stdout[1],
|
|
|
|
used: stdout[2],
|
|
|
|
available: stdout[3],
|
|
|
|
use: stdout[4]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-06-29 01:27:17 +00:00
|
|
|
|
|
|
|
// WHATS SESSION SIZE MONITORING
|
2022-06-29 00:34:43 +00:00
|
|
|
const whatsapps = await ListWhatsAppsService();
|
|
|
|
|
|
|
|
whatsapps.forEach(async whats => {
|
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
|
2022-06-29 00:34:43 +00:00
|
|
|
|
|
|
|
const sourcePath = path.join(__dirname, `../../.wwebjs_auth/`, `session-bd_${whats.id}`)
|
|
|
|
|
2022-06-29 01:27:17 +00:00
|
|
|
if (fs.existsSync(sourcePath)) {
|
|
|
|
|
|
|
|
try {
|
2022-12-18 14:45:41 +00:00
|
|
|
|
2022-06-29 01:27:17 +00:00
|
|
|
const fastFolderSizeAsync = promisify(fastFolderSize)
|
|
|
|
|
|
|
|
let size = await fastFolderSizeAsync(sourcePath)
|
|
|
|
|
|
|
|
size = convertBytes(size)
|
2022-12-18 14:45:41 +00:00
|
|
|
|
2022-06-29 01:27:17 +00:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
2022-12-30 01:01:57 +00:00
|
|
|
// console.log('Directory not found to get size info: ', sourcePath)
|
2022-06-29 01:27:17 +00:00
|
|
|
|
|
|
|
}
|
2022-06-29 00:34:43 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log('>>> SchedulingNotifiySendMessage.ts error: ', error)
|
2022-06-29 00:34:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 03:27:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
const SchedulingNotifySendMessage = async () => {
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
try {
|
|
|
|
clearInterval(_fifo);
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
await monitor()
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log('error on SchedulingNotifySendMessage: ', error)
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
_fifo = setInterval(SchedulingNotifySendMessage, 5000);
|
|
|
|
}
|
2022-03-01 03:27:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
_fifo = setInterval(SchedulingNotifySendMessage, 5000);
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-12-18 14:45:41 +00:00
|
|
|
module.exports = SchedulingNotifySendMessage
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-03-01 03:27:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|