Criação de instancia unica do redis

pull/21/head
adriano 2023-01-05 15:22:25 -03:00
parent 5e63b61958
commit 37ab4b9564
7 changed files with 131 additions and 126 deletions

View File

@ -18,7 +18,6 @@
"@sentry/node": "^5.29.2",
"@types/pino": "^6.3.4",
"bcryptjs": "^2.4.3",
"check-internet-connected": "^2.0.6",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"date-fns": "^2.16.1",
@ -36,7 +35,7 @@
"mysql2": "^2.2.5",
"pg": "^8.4.1",
"pino": "^6.9.0",
"pino-pretty": "~4.7.1",
"pino-pretty": "^9.1.1",
"qrcode-terminal": "^0.12.0",
"reflect-metadata": "^0.1.13",
"sequelize": "^5.22.3",

View File

@ -54,12 +54,12 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
searchParam = searchParam.replace(/\s+/g, ' ').trim().toLowerCase();
console.log('QUERY CONTACTS FROM CACHE SEARCH PARAM: ', searchParam)
const data = await searchContactCache(searchParam, offset, 20)
if (data) {
console.log('QUERY CONTACTS FROM CACHE SEARCH PARAM: ', searchParam)
console.log('QUERY CONTACTS FROM CACHE QUERY LENGTH: ', data.length)
return res.json({ contacts: data, count: data.length, hasMore: data.length > 0 ? true : false });
@ -72,6 +72,8 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
}
console.log('QUERY CONTACTS FROM DATABASE SEARCH PARAM: ', searchParam)
const { contacts, count, hasMore } = await ListContactsService({ searchParam, pageNumber });
return res.json({ contacts, count, hasMore });

View File

@ -13,7 +13,7 @@ const deleteContactsByIdCache = async (id: string | number) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const contact_cache: any = await redis.hgetall(`contact:${id}`)
@ -35,7 +35,7 @@ const deleteContactsByIdCache = async (id: string | number) => {
console.log(`There was an error on deleteContactsByIdCache: ${error}`)
}
await redis.quit()
// await redis.quit()
}
const updateContactCache = async (hash: any, json_object: any) => {
@ -44,7 +44,7 @@ const updateContactCache = async (hash: any, json_object: any) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -56,7 +56,7 @@ const updateContactCache = async (hash: any, json_object: any) => {
await pipeline.exec(() => { console.log("Key/value inserted/updated") });
await redis.quit()
// await redis.quit()
}
@ -66,7 +66,7 @@ const updateContactCacheById = async (id: string | number, update_fields: object
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const contact_cache: any = await redis.hgetall(`contact:${id}`)
@ -86,7 +86,7 @@ const updateContactCacheById = async (id: string | number, update_fields: object
console.log(`There was an error on updateContactCacheById: ${error}`)
}
await redis.quit()
// await redis.quit()
}
const createOrUpdateContactCache = async (hash: any, contact: any) => {
@ -95,7 +95,7 @@ const createOrUpdateContactCache = async (hash: any, contact: any) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
if (contact.name) {
contact.escaped_name = escapeCharCache(contact.name)
@ -103,7 +103,7 @@ const createOrUpdateContactCache = async (hash: any, contact: any) => {
await redis.hmset(hash, contact);
await redis.quit()
// await redis.quit()
}
@ -114,12 +114,12 @@ async function searchContactCache(search: string, offset: number, limit: number)
if(!redis) return
if (redis.status !== 'connect') return null
// if (redis.status !== 'connect') return null
search = escapeCharCache(search)
const response: any = await redis.call('FT.SEARCH', 'idx_contact', `(@escaped_name:*${search}*)|(@number:*${search}*)`, 'LIMIT', offset, limit, 'SORTBY', 'escaped_name', 'ASC')
await redis.quit()
// await redis.quit()
if (response.length === 1) {
@ -172,7 +172,7 @@ const insertContactsCache = async (contacts: any) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -185,7 +185,7 @@ const insertContactsCache = async (contacts: any) => {
await pipeline.exec(() => { console.log(`${contacts.length} CONTACTS INSERTED IN CACHE!`) });
await redis.quit()
// await redis.quit()
}
const loadContactsCache = async () => {
@ -196,7 +196,7 @@ const loadContactsCache = async () => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
let contacts = await ListContactsServiceCache()
@ -214,7 +214,7 @@ const loadContactsCache = async () => {
await pipeline.exec(() => { console.log(`${contacts.length} CONTACTS INSERTED IN CACHE!`) });
await redis.quit()
// await redis.quit()
}
const createContactIndexCache = async (hashIndex: string) => {
@ -223,7 +223,7 @@ const createContactIndexCache = async (hashIndex: string) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
try {
@ -242,7 +242,7 @@ const createContactIndexCache = async (hashIndex: string) => {
console.log('There was an error on createContactIndexCache: ', error)
}
await redis.quit()
// await redis.quit()
}
export {

View File

@ -13,7 +13,7 @@ const deleteContactsByIdCache = async (id: string | number) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const contact_cache: any = await redis.hgetall(`contact:${id}`)
@ -31,7 +31,7 @@ const deleteContactsByIdCache = async (id: string | number) => {
console.log(`There was an error on deleteContactsByIdCache: ${error}`)
}
await redis.quit()
// await redis.quit()
}
const updateContactCache = async (hash: any, json_object: any) => {
@ -40,7 +40,7 @@ const updateContactCache = async (hash: any, json_object: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -52,7 +52,7 @@ const updateContactCache = async (hash: any, json_object: any) => {
await pipeline.exec(() => { console.log("Key/value inserted/updated") });
await redis.quit()
// await redis.quit()
}
@ -62,7 +62,7 @@ const updateContactCacheById = async (id: string | number, update_fields: object
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const contact_cache: any = await redis.hgetall(`contact:${id}`)
@ -84,7 +84,7 @@ const updateContactCacheById = async (id: string | number, update_fields: object
console.log(`There was an error on updateContactCacheById: ${error}`)
}
await redis.quit()
// await redis.quit()
}
const createOrUpdateContactCache = async (hash: any, contact: any) => {
@ -93,7 +93,7 @@ const createOrUpdateContactCache = async (hash: any, contact: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
if (contact.name) {
contact.escaped_name = escapeCharCache(contact.name)
@ -101,7 +101,7 @@ const createOrUpdateContactCache = async (hash: any, contact: any) => {
await redis.hmset(hash, contact);
await redis.quit()
// await redis.quit()
}
@ -112,12 +112,12 @@ async function searchContactCache(search: string, offset: number, limit: number)
if (!redis) return
if (redis.status !== 'connect') return null
// if (redis.status !== 'connect') return null
search = escapeCharCache(search)
const response: any = await redis.call('FT.SEARCH', 'idx_contact_message', `(@escaped_name:*${search}*)|(@number:*${search}*)`, 'LIMIT', offset, limit, 'SORTBY', 'escaped_name', 'ASC')
await redis.quit()
// await redis.quit()
if (response.length === 1) {
@ -168,7 +168,7 @@ const getLastId = async (hash: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const contact_cache: any = await redis.hgetall(hash)
@ -182,13 +182,13 @@ const insertMessageContactCache = async (hash: any, contact_message: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
await redis.hmset(hash, contact_message);
console.log('CREATED/UPDATED CONTACT MESSAGE')
await redis.quit()
// await redis.quit()
}
@ -200,7 +200,7 @@ const loadContactsCache = async () => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
let contacts = await ListContactsServiceCache()
@ -218,7 +218,7 @@ const loadContactsCache = async () => {
await pipeline.exec(() => { console.log(`${contacts.length} CONTACTS INSERTED IN CACHE!`) });
await redis.quit()
// await redis.quit()
}
const createContactMessageIndexCache = async (hashIndex: string) => {
@ -227,7 +227,7 @@ const createContactMessageIndexCache = async (hashIndex: string) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
try {
@ -246,7 +246,7 @@ const createContactMessageIndexCache = async (hashIndex: string) => {
console.log('There was an error on contact_message: ', error)
}
await redis.quit()
// await redis.quit()
}
export {

View File

@ -20,7 +20,7 @@ const deleteScheduleByTicketIdCache = async (ticketId: string | number) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const schedule_cache: any = await redis.hgetall(`schedule:${ticketId}`)
@ -38,7 +38,7 @@ const deleteScheduleByTicketIdCache = async (ticketId: string | number) => {
console.log(`There was an error on deleteScheduleByTicketIdCache: ${error}`)
}
await redis.quit()
// await redis.quit()
}
@ -51,7 +51,7 @@ const updateScheduleCacheByTicketId = async (scheduleNotify: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -63,7 +63,7 @@ const updateScheduleCacheByTicketId = async (scheduleNotify: any) => {
await pipeline.exec(() => { console.log("schedule Key/value inserted/updated") });
await redis.quit()
// await redis.quit()
}
@ -76,7 +76,7 @@ const createSchedulingNotifyCache = async (scheduleNotify: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
let date_time: any = format(new Date(scheduleNotify.schedulingTime), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }).split(' ')
@ -96,7 +96,7 @@ const createSchedulingNotifyCache = async (scheduleNotify: any) => {
console.log(`${scheduleNotify.length} SCHEDULE NOTIFY INSERTED IN CACHE!`)
await redis.quit()
// await redis.quit()
}
@ -107,12 +107,12 @@ async function searchScheduleCache(date: string, hour: number | string, minute:
if (!redis) return
if (redis.status !== 'connect') return null
// if (redis.status !== 'connect') return null
date = escapeCharCache(date).trim()
const response: any = await redis.call('FT.SEARCH', 'idx_schedule', `(@date_escaped:${date}) (@hour:${hour}) (@minute:${minute})`)
await redis.quit()
// await redis.quit()
if (response.length === 1) {
@ -145,7 +145,7 @@ const loadSchedulesCache = async () => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
let schedules: any = await SchedulingNotify.findAll({ raw: true, attributes: ["id", "statusChatEndId", "ticketId", "schedulingTime", "message"] });
@ -173,7 +173,7 @@ const loadSchedulesCache = async () => {
await pipeline.exec(() => { console.log(`${schedules.length} SCHEDULES NOTIFY INSERTED IN CACHE!`) });
await redis.quit()
// await redis.quit()
// let test = await searchScheduleCache('2022-12-16', '18', '30')
@ -188,7 +188,7 @@ const createScheduleIndexCache = async (hashIndex: string) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
try {
@ -207,7 +207,7 @@ const createScheduleIndexCache = async (hashIndex: string) => {
console.log('There was an error on createScheduleIndexCache: ', error)
}
await redis.quit()
// await redis.quit()
}
export {

View File

@ -7,6 +7,8 @@ import ListTicketServiceCache from "../services/TicketServices/ListTicketService
import { escapeCharCache } from './ContactsCache'
const redis = new Redis();
const redisConn = async () => {
@ -14,48 +16,50 @@ const redisConn = async () => {
return null
}
try {
// try {
const redis = new Redis();
// const redis = new Redis();
const conn = () => new Promise((resolve, reject) => {
// const conn = () => new Promise((resolve, reject) => {
redis.on('error', async (err) => {
// redis.on('error', async (err) => {
if (err.code === 'ECONNREFUSED') {
console.error(`Redis connection error: ${err}.`)
await redis.quit()
}
else {
console.error(`Redis encountered an error: ${err.message}.`)
}
// if (err.code === 'ECONNREFUSED') {
// console.error(`Redis connection error: ${err}.`)
// // await redis.quit()
// }
// else {
// console.error(`Redis encountered an error: ${err.message}.`)
// }
reject(err)
// reject(err)
})
// })
redis.on('connect', () => {
// redis.on('connect', () => {
// console.log('CacheStore - Connection status: connected');
resolve(redis);
// // console.log('CacheStore - Connection status: connected');
// resolve(redis);
})
// })
redis.on('end', () => {
// console.log('CacheStore - Connection status: disconnected');
});
// redis.on('end', () => {
// // console.log('CacheStore - Connection status: disconnected');
// });
redis.on('reconnecting', () => {
// console.log('CacheStore - Connection status: reconnecting');
});
});
// redis.on('reconnecting', () => {
// // console.log('CacheStore - Connection status: reconnecting');
// });
// });
return await conn();
// return await conn();
} catch (e) {
console.error(`There whas an error on redis connection: ${e}`);
return Promise.resolve([]);
}
// } catch (e) {
// console.error(`There whas an error on redis connection: ${e}`);
// return Promise.resolve([]);
// }
return redis
}
@ -66,12 +70,14 @@ const flushCache = async () => {
if(!redis) return false
if (redis.status === 'connect') {
// if (redis.status === 'connect') {
// console.log('TICKETS CACHE REMOVED')
// await redis.call('FLUSHALL')
// }
console.log('TICKETS CACHE REMOVED')
await redis.call('FLUSHALL')
await redis.quit()
}
}
@ -81,12 +87,10 @@ const cacheSize = async () => {
if(!redis) return null
if (redis.status !== 'connect') {
return -1
}
// if (redis.status !== 'connect') return -1
const size = await redis.call('dbsize')
await redis.quit()
// await redis.quit()
return size
}
@ -97,7 +101,7 @@ const loadTicketsCache = async () => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
await createTicketIndexCache('idx_ticket')
@ -123,7 +127,7 @@ const loadTicketsCache = async () => {
await pipeline.exec(() => { console.log(`${tickets.length} TICKETS INSERTED IN CACHE!`) });
await redis.quit()
// await redis.quit()
}
@ -133,7 +137,7 @@ const createTicketIndexCache = async (hashIndex: string) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
try {
@ -152,7 +156,7 @@ const createTicketIndexCache = async (hashIndex: string) => {
console.log('There was an error on createTicketIndexCache: ', error)
}
await redis.quit()
// await redis.quit()
}
const updateTicketCache = async (hash: any, json_object: any) => {
@ -161,7 +165,7 @@ const updateTicketCache = async (hash: any, json_object: any) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
let entries = Object.entries(json_object)
@ -172,7 +176,7 @@ const updateTicketCache = async (hash: any, json_object: any) => {
await pipeline.exec(() => { console.log("updateTicketCache Key/value inserted/updated") });
await redis.quit()
// await redis.quit()
}
@ -182,7 +186,7 @@ const updateTicketCacheByTicketId = async (ticketId: string | number, update_fie
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const ticket_cache = await redis.hgetall(`ticket:${ticketId}`)
@ -204,7 +208,7 @@ const updateTicketCacheByTicketId = async (ticketId: string | number, update_fie
console.log(`There was an error on updateTicketCacheByTicketId: ${error}`)
}
await redis.quit()
// await redis.quit()
}
@ -216,7 +220,7 @@ const createOrUpdateTicketCache = async (hash: any, ticket: any) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
ticket.escaped_name = escapeCharCache(ticket['contact.name'])
@ -227,7 +231,7 @@ const createOrUpdateTicketCache = async (hash: any, ticket: any) => {
console.log('CREATED/UPDATED TICKET CACHE')
await redis.quit()
// await redis.quit()
}
@ -237,7 +241,7 @@ const deleteTicketsByIdCache = async (ticketId: string | number) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const ticket_cache = await redis.hgetall(`ticket:${ticketId}`)
@ -255,7 +259,7 @@ const deleteTicketsByIdCache = async (ticketId: string | number) => {
console.log(`There was an error on deleteTicketsByIdCache: ${error}`)
}
await redis.quit()
// await redis.quit()
}
@ -265,7 +269,7 @@ const deleteTicketsFieldsCache = async (tickets: any, del_fields: any) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -286,7 +290,7 @@ const deleteTicketsFieldsCache = async (tickets: any, del_fields: any) => {
}
await redis.quit()
// await redis.quit()
}
@ -297,7 +301,7 @@ const updateTicketsByContactsCache = async (oldNumber: string, newName: string,
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -323,7 +327,7 @@ const updateTicketsByContactsCache = async (oldNumber: string, newName: string,
}
await redis.quit()
// await redis.quit()
}
@ -334,7 +338,7 @@ const deleteTicketsByContactsCache = async (number: string) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -356,7 +360,7 @@ const deleteTicketsByContactsCache = async (number: string) => {
}
await redis.quit()
// await redis.quit()
}
@ -366,10 +370,10 @@ const deleteTicketCache = async (hash: any) => {
if(!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
await redis.del(hash)
await redis.quit()
// await redis.quit()
}
@ -379,7 +383,7 @@ async function searchTicketCache(search: string, offset?: number, limit?: number
if(!redis) return
if(redis.status!=='connect') return null
// if(redis.status!=='connect') return null
search = escapeCharCache(search)
@ -391,7 +395,7 @@ async function searchTicketCache(search: string, offset?: number, limit?: number
else {
response = await redis.call('FT.SEARCH', 'idx_ticket', `(@escaped_name:*${search}*)|(@contact_number:*${search}*)`)
}
await redis.quit()
// await redis.quit()
// console.log('response: ', response)

View File

@ -14,7 +14,7 @@ const deleteWhatsappCache = async (hash: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const whatsapp_cache: any = await redis.hgetall(hash)
@ -32,7 +32,7 @@ const deleteWhatsappCache = async (hash: any) => {
console.log(`There was an error on deleteWhatsappCache: ${error}`)
}
await redis.quit()
// await redis.quit()
}
const updateWhatsappCache = async (hash: any, json_object: any) => {
@ -41,7 +41,7 @@ const updateWhatsappCache = async (hash: any, json_object: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -53,7 +53,7 @@ const updateWhatsappCache = async (hash: any, json_object: any) => {
await pipeline.exec(() => { console.log("whatsapp Key/value inserted/updated") });
await redis.quit()
// await redis.quit()
}
@ -63,7 +63,7 @@ const updateWhatsappCacheById = async (hash: any, update_fields: object | any) =
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
const whatsapp_cache: any = await redis.hgetall(hash)
@ -83,7 +83,7 @@ const updateWhatsappCacheById = async (hash: any, update_fields: object | any) =
console.log(`There was an error on updateWhatsappCacheById: ${error}`)
}
await redis.quit()
// await redis.quit()
}
// const createOrUpdateContactCache = async (hash: any, contact: any) => {
@ -92,7 +92,7 @@ const updateWhatsappCacheById = async (hash: any, update_fields: object | any) =
// if(!redis) return
// if (redis.status !== 'connect') return
// // if (redis.status !== 'connect') return
// if (contact.name) {
// contact.escaped_name = escapeCharCache(contact.name)
@ -111,13 +111,13 @@ async function searchWhatsappCache(id: string, status: string) {
if (!redis) return
if (redis.status !== 'connect') return null
// if (redis.status !== 'connect') return null
const number_cache: any = await redis.hgetall(`whatsapp:${id}`)
if (Object.entries(number_cache).length == 0) {
await redis.quit()
// await redis.quit()
return []
}
@ -128,7 +128,7 @@ async function searchWhatsappCache(id: string, status: string) {
const response: any = await redis.call('FT.SEARCH', 'idx_whatsapp', `(@status:*${status}*) (@number:*${number_cache.number}*)`, 'SORTBY', 'status', 'ASC')
await redis.quit()
// await redis.quit()
if (response.length === 1) {
@ -161,7 +161,7 @@ const insertOrUpeateWhatsCache = async (hash: any, whatsapp: any) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
if (Array.isArray(whatsapp)) {
@ -183,7 +183,7 @@ const insertOrUpeateWhatsCache = async (hash: any, whatsapp: any) => {
}
await redis.quit()
// await redis.quit()
}
@ -197,7 +197,7 @@ const loadWhatsappCache = async () => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
let whatsapps: any = await Whatsapp.findAll({ raw: true })
@ -215,7 +215,7 @@ const loadWhatsappCache = async () => {
await pipeline.exec(() => { console.log(`${whatsapps.length} WHATSAPPS INSERTED IN CACHE!`) });
await redis.quit()
// await redis.quit()
}
const createWhatsappIndexCache = async (hashIndex: string) => {
@ -224,7 +224,7 @@ const createWhatsappIndexCache = async (hashIndex: string) => {
if (!redis) return
if (redis.status !== 'connect') return
// if (redis.status !== 'connect') return
try {
@ -243,7 +243,7 @@ const createWhatsappIndexCache = async (hashIndex: string) => {
console.log('There was an error on createWhatsappIndexCache: ', error)
}
await redis.quit()
// await redis.quit()
}
export {