Finalização da correção do lembrete para usar cache

pull/21/head
adriano 2022-12-18 13:56:45 -03:00
parent bc4712362d
commit ad9cbafdf2
5 changed files with 128 additions and 126 deletions

View File

@ -94,7 +94,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
}); });
await stopWhoIsOnlineMonitor() // await stopWhoIsOnlineMonitor()
await startWhoIsOnlineMonitor() await startWhoIsOnlineMonitor()
return res.status(200).json(user); return res.status(200).json(user);

View File

@ -111,8 +111,6 @@ async function searchScheduleCache(date: string, hour: number | string, minute:
date = escapeCharCache(date).trim() date = escapeCharCache(date).trim()
console.log('kkkkkkkkkkk date: ', date)
const response: any = await redis.call('FT.SEARCH', 'idx_schedule', `(@date_escaped:${date}) (@hour:${hour}) (@minute:${minute})`) const response: any = await redis.call('FT.SEARCH', 'idx_schedule', `(@date_escaped:${date}) (@hour:${hour}) (@minute:${minute})`)
redis.quit() redis.quit()

View File

@ -42,17 +42,18 @@ const monitor = async () => {
const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" }); const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" });
// console.log('schedulingNotifies: ',schedulingNotifies)
if (schedulingNotifies && schedulingNotifies.length > 0) { if (schedulingNotifies && schedulingNotifies.length > 0) {
console.log('ENTROU NA DATA schedulingNotifies: ', schedulingNotifies)
for (let i = 0; i < schedulingNotifies.length; i++) { for (let i = 0; i < schedulingNotifies.length; i++) {
const ticket = await ShowTicketService(schedulingNotifies[i].ticketId); const ticket = await ShowTicketService(+schedulingNotifies[i].ticketId);
await new Promise(f => setTimeout(f, 3000));
if(!ticket.queue){
await ticket.update({status: 'open'})
}
SetTicketMessagesAsRead(ticket); SetTicketMessagesAsRead(ticket);
@ -67,18 +68,6 @@ const monitor = async () => {
} }
// const ticket = await ShowTicketService(schedulingNotifies[0].ticketId);
// SetTicketMessagesAsRead(ticket);
// await SendWhatsAppMessage({
// body: schedulingNotifies[0].message, ticket
// });
// await deleteScheduleByTicketIdCache(schedulingNotifies[0].ticketId)
// await DeleteSchedulingNotifyService(schedulingNotifies[0].id)
} }
@ -185,20 +174,6 @@ _fifo = setInterval(SchedulingNotifySendMessage, 5000);
module.exports = SchedulingNotifySendMessage module.exports = SchedulingNotifySendMessage
// export const startSchedulingMonitor = async (mileseconds: number) => {
// timeInterval = mileseconds
// scheduler_monitor = setInterval(monitor, mileseconds)
// }
// export const stopSchedulingMonitor = async () => {
// clearInterval(scheduler_monitor)
// }

View File

@ -17,7 +17,7 @@ import { date } from "faker";
import sumOnlineTimeNow from '../helpers/SumOlineTimeNow' import sumOnlineTimeNow from '../helpers/SumOlineTimeNow'
import UserOnlineTime from "../models/UserOnlineTime"; import UserOnlineTime from "../models/UserOnlineTime";
let _fifo: any
let whoIsOnline_monitor: any; let whoIsOnline_monitor: any;
// const listUserId:any[] = [{'id':8, status: 'offline'},{'id':3, status: 'offline'},{'id':5, status: 'offline'}] // const listUserId:any[] = [{'id':8, status: 'offline'},{'id':3, status: 'offline'},{'id':5, status: 'offline'}]
@ -91,7 +91,7 @@ const monitor = async () => {
} else { } else {
usersSocket.ob.listOnline.forEach(async (el: any) => { usersSocket.ob.listOnline.forEach(async (el: any) => {
const indexAux = lstOnline.findIndex((e: any) => e.id == el.id) const indexAux = lstOnline.findIndex((e: any) => e.id == el.id)
@ -175,38 +175,70 @@ const monitor = async () => {
}); });
} catch (error) { } catch (error) {
console.log('>>> There was an error no WhoIsOnlineMonitor.ts: ',error) console.log('>>> There was an error no WhoIsOnlineMonitor.ts: ', error)
stopWhoIsOnlineMonitor()
startWhoIsOnlineMonitor() listUserId = []
count = 0
countTest = 0
uuid = 0
} }
}; };
export const startWhoIsOnlineMonitor = async (mileseconds?: number) => {
const WhoIsOnlineMonitor = async () => {
try {
clearInterval(_fifo);
await monitor()
} catch (error) {
console.log('error on WhoIsOnlineMonitor: ', error)
}
finally {
_fifo = setInterval(WhoIsOnlineMonitor, 3000);
}
}
_fifo = setInterval(WhoIsOnlineMonitor, 3000);
const startWhoIsOnlineMonitor = async (mileseconds?: number) => {
listUserId = [] listUserId = []
count = 0 count = 0
countTest = 0 countTest = 0
uuid = 0 uuid = 0
if (mileseconds) { clearInterval(_fifo);
timeInterval = mileseconds _fifo = setInterval(WhoIsOnlineMonitor, 3000);
}
whoIsOnline_monitor = setInterval(monitor, timeInterval)
// if (mileseconds) {
// timeInterval = mileseconds
// }
} }
export const stopWhoIsOnlineMonitor = async () => { const stopWhoIsOnlineMonitor = async () => {
clearInterval(whoIsOnline_monitor) clearInterval(_fifo);
// _fifo = setInterval(WhoIsOnlineMonitor, 3000);
} }
export { WhoIsOnlineMonitor, startWhoIsOnlineMonitor, stopWhoIsOnlineMonitor }

View File

@ -3,16 +3,14 @@ import app from "./app";
import { initIO } from "./libs/socket"; import { initIO } from "./libs/socket";
import { logger } from "./utils/logger"; import { logger } from "./utils/logger";
import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions"; import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions";
import "./helpers/SchedulingNotifySendMessage"
import "./helpers/SchedulingNotifySendMessage" import "./helpers/WhoIsOnlineMonitor"
import { startWhoIsOnlineMonitor } from "./helpers/WhoIsOnlineMonitor"
import { loadTicketsCache, flushCache, cacheSize } from './helpers/TicketCache' import { loadTicketsCache, flushCache, cacheSize } from './helpers/TicketCache'
import { loadContactsCache } from './helpers/ContactsCache' import { loadContactsCache } from './helpers/ContactsCache'
import { loadWhatsappCache } from './helpers/WhatsCache' import { loadWhatsappCache } from './helpers/WhatsCache'
import { delRestoreControllFile } from "./helpers/RestoreControll"; import { delRestoreControllFile } from "./helpers/RestoreControll";
import { createSessionDir } from "./helpers/CreateSessionDir"; import { createSessionDir } from "./helpers/CreateSessionDir";
import { loadSchedulesCache, } from "./helpers/SchedulingNotifyCache"; import { loadSchedulesCache, } from "./helpers/SchedulingNotifyCache";
const server = app.listen(process.env.PORT, () => { const server = app.listen(process.env.PORT, () => {
logger.info(`Server started on port: ${process.env.PORT}`); logger.info(`Server started on port: ${process.env.PORT}`);
@ -22,26 +20,25 @@ initIO(server);
StartAllWhatsAppsSessions(); StartAllWhatsAppsSessions();
gracefulShutdown(server); gracefulShutdown(server);
(async()=>{ (async () => {
const cacheLength = await cacheSize() const cacheLength = await cacheSize()
console.log('cacheSize: ', cacheLength) console.log('cacheSize: ', cacheLength)
if(cacheLength == 0){ if (cacheLength == 0) {
console.log('Loading from cache...') console.log('Loading from cache...')
await flushCache() await flushCache()
await loadContactsCache() await loadContactsCache()
await loadTicketsCache() await loadTicketsCache()
await loadWhatsappCache()
await loadSchedulesCache()
} }
await loadWhatsappCache()
await loadSchedulesCache()
})() })()
createSessionDir() createSessionDir()
delRestoreControllFile() delRestoreControllFile()
startWhoIsOnlineMonitor(3000)