fix: Adjusts in redis for save instances

This commit is contained in:
Davidson Gomes
2024-04-18 10:39:24 -03:00
parent 4ed1335f89
commit 61bd5b3484
18 changed files with 178 additions and 208 deletions

View File

@@ -1,13 +1,19 @@
export interface ICache {
get(key: string): Promise<any>;
hGet(key: string, field: string): Promise<any>;
set(key: string, value: any, ttl?: number): void;
hSet(key: string, field: string, value: any): Promise<void>;
has(key: string): Promise<boolean>;
keys(appendCriteria?: string): Promise<string[]>;
delete(key: string | string[]): Promise<number>;
hDelete(key: string, field: string): Promise<any>;
deleteAll(appendCriteria?: string): Promise<number>;
}

View File

@@ -6,7 +6,6 @@ import { v4 } from 'uuid';
import { ConfigService, HttpServer, WaBusiness } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
import { RedisCache } from '../../libs/redis.client';
import { InstanceDto, SetPresenceDto } from '../dto/instance.dto';
import { ChatwootService } from '../integrations/chatwoot/services/chatwoot.service';
import { RabbitmqService } from '../integrations/rabbitmq/services/rabbitmq.service';
@@ -41,7 +40,7 @@ export class InstanceController {
private readonly typebotService: TypebotService,
private readonly integrationService: IntegrationService,
private readonly proxyService: ProxyController,
private readonly cache: RedisCache,
private readonly cache: CacheService,
private readonly chatwootCache: CacheService,
private readonly messagesLostCache: CacheService,
) {}

View File

@@ -2,7 +2,7 @@ import { NextFunction, Request, Response } from 'express';
import { existsSync } from 'fs';
import { join } from 'path';
import { configService, Database, Redis } from '../../config/env.config';
import { CacheConf, configService, Database } from '../../config/env.config';
import { INSTANCE_DIR } from '../../config/path.config';
import {
BadRequestException,
@@ -17,12 +17,13 @@ import { cache, waMonitor } from '../server.module';
async function getInstance(instanceName: string) {
try {
const db = configService.get<Database>('DATABASE');
const redisConf = configService.get<Redis>('REDIS');
const cacheConf = configService.get<CacheConf>('CACHE');
const exists = !!waMonitor.waInstances[instanceName];
if (redisConf.ENABLED) {
const keyExists = await cache.keyExists();
if (cacheConf.REDIS.ENABLED && cacheConf.REDIS.SAVE_INSTANCES) {
const keyExists = await cache.has(instanceName);
return exists || keyExists;
}

View File

@@ -3,7 +3,6 @@ import { configService } from '../config/env.config';
import { eventEmitter } from '../config/event.config';
import { Logger } from '../config/logger.config';
import { dbserver } from '../libs/db.connect';
import { RedisCache } from '../libs/redis.client';
import { ChatController } from './controllers/chat.controller';
import { GroupController } from './controllers/group.controller';
import { InstanceController } from './controllers/instance.controller';
@@ -107,7 +106,7 @@ export const repository = new RepositoryBroker(
dbserver?.getClient(),
);
export const cache = new RedisCache();
export const cache = new CacheService(new CacheEngine(configService, 'instance').getEngine());
const chatwootCache = new CacheService(new CacheEngine(configService, ChatwootService.name).getEngine());
const messagesLostCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine());

View File

@@ -1,3 +1,5 @@
import { BufferJSON } from '@whiskeysockets/baileys';
import { Logger } from '../../config/logger.config';
import { ICache } from '../abstract/abstract.cache';
@@ -20,6 +22,21 @@ export class CacheService {
return this.cache.get(key);
}
public async hGet(key: string, field: string) {
try {
const data = await this.cache.hGet(key, field);
if (data) {
return JSON.parse(data, BufferJSON.reviver);
}
return null;
} catch (error) {
this.logger.error(error);
return null;
}
}
async set(key: string, value: any) {
if (!this.cache) {
return;
@@ -28,6 +45,16 @@ export class CacheService {
this.cache.set(key, value);
}
public async hSet(key: string, field: string, value: any) {
try {
const json = JSON.stringify(value, BufferJSON.replacer);
await this.cache.hSet(key, field, json);
} catch (error) {
this.logger.error(error);
}
}
async has(key: string) {
if (!this.cache) {
return;
@@ -44,6 +71,16 @@ export class CacheService {
return this.cache.delete(key);
}
async hDelete(key: string, field: string) {
try {
await this.cache.hDelete(key, field);
return true;
} catch (error) {
this.logger.error(error);
return false;
}
}
async deleteAll(appendCriteria?: string) {
if (!this.cache) {
return;

View File

@@ -5,11 +5,10 @@ import { Db } from 'mongodb';
import { Collection } from 'mongoose';
import { join } from 'path';
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
import { Auth, CacheConf, ConfigService, Database, DelInstance, HttpServer } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
import { NotFoundException } from '../../exceptions';
import { RedisCache } from '../../libs/redis.client';
import {
AuthModel,
ChamaaiModel,
@@ -34,7 +33,7 @@ export class WAMonitoringService {
private readonly eventEmitter: EventEmitter2,
private readonly configService: ConfigService,
private readonly repository: RepositoryBroker,
private readonly cache: RedisCache,
private readonly cache: CacheService,
private readonly chatwootCache: CacheService,
private readonly messagesLostCache: CacheService,
) {
@@ -44,7 +43,7 @@ export class WAMonitoringService {
this.noConnection();
Object.assign(this.db, configService.get<Database>('DATABASE'));
Object.assign(this.redis, configService.get<Redis>('REDIS'));
Object.assign(this.redis, configService.get<CacheConf>('CACHE'));
this.dbInstance = this.db.ENABLED
? this.repository.dbServer?.db(this.db.CONNECTION.DB_PREFIX_NAME + '-instances')
@@ -52,7 +51,7 @@ export class WAMonitoringService {
}
private readonly db: Partial<Database> = {};
private readonly redis: Partial<Redis> = {};
private readonly redis: Partial<CacheConf> = {};
private dbInstance: Db;
@@ -213,7 +212,7 @@ export class WAMonitoringService {
});
this.logger.verbose('instance files deleted: ' + name);
});
} else if (!this.redis.ENABLED) {
} else if (!this.redis.REDIS.ENABLED && !this.redis.REDIS.SAVE_INSTANCES) {
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
for await (const dirent of dir) {
if (dirent.isDirectory()) {
@@ -248,10 +247,9 @@ export class WAMonitoringService {
return;
}
if (this.redis.ENABLED) {
if (this.redis.REDIS.ENABLED && this.redis.REDIS.SAVE_INSTANCES) {
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
this.cache.reference = instanceName;
await this.cache.delAll();
await this.cache.delete(instanceName);
return;
}
@@ -304,7 +302,7 @@ export class WAMonitoringService {
this.logger.verbose('Loading instances');
try {
if (this.redis.ENABLED) {
if (this.redis.REDIS.ENABLED && this.redis.REDIS.SAVE_INSTANCES) {
await this.loadInstancesFromRedis();
} else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
await this.loadInstancesFromDatabase();
@@ -377,12 +375,11 @@ export class WAMonitoringService {
private async loadInstancesFromRedis() {
this.logger.verbose('Redis enabled');
await this.cache.connect(this.redis as Redis);
const keys = await this.cache.getInstanceKeys();
const keys = await this.cache.keys();
if (keys?.length > 0) {
this.logger.verbose('Reading instance keys and setting instances');
await Promise.all(keys.map((k) => this.setInstance(k.split(':')[1])));
await Promise.all(keys.map((k) => this.setInstance(k.split(':')[2])));
} else {
this.logger.verbose('No instance keys found');
}

View File

@@ -55,11 +55,10 @@ import qrcode, { QRCodeToDataURLOptions } from 'qrcode';
import qrcodeTerminal from 'qrcode-terminal';
import sharp from 'sharp';
import { ConfigService, ConfigSessionPhone, Database, Log, QrCode, Redis } from '../../../config/env.config';
import { CacheConf, ConfigService, ConfigSessionPhone, Database, Log, QrCode } from '../../../config/env.config';
import { INSTANCE_DIR } from '../../../config/path.config';
import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../../exceptions';
import { dbserver } from '../../../libs/db.connect';
import { RedisCache } from '../../../libs/redis.client';
import { makeProxyAgent } from '../../../utils/makeProxyAgent';
import { useMultiFileAuthStateDb } from '../../../utils/use-multi-file-auth-state-db';
import { useMultiFileAuthStateRedisDb } from '../../../utils/use-multi-file-auth-state-redis-db';
@@ -126,7 +125,7 @@ export class BaileysStartupService extends WAStartupService {
public readonly configService: ConfigService,
public readonly eventEmitter: EventEmitter2,
public readonly repository: RepositoryBroker,
public readonly cache: RedisCache,
public readonly cache: CacheService,
public readonly chatwootCache: CacheService,
public readonly messagesLostCache: CacheService,
) {
@@ -149,19 +148,23 @@ export class BaileysStartupService extends WAStartupService {
public mobile: boolean;
private async recoveringMessages() {
setTimeout(async () => {
this.logger.info('Recovering messages');
this.messagesLostCache.keys().then((keys) => {
keys.forEach(async (key) => {
const message = await this.messagesLostCache.get(key.split(':')[2]);
const cacheConf = this.configService.get<CacheConf>('CACHE');
if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node') {
this.logger.verbose('Message absent from node, retrying to send, key: ' + key.split(':')[2]);
await this.client.sendMessageAck(JSON.parse(message.messageStubParameters[1], BufferJSON.reviver));
}
if ((cacheConf?.REDIS?.ENABLED && cacheConf?.REDIS?.URI !== '') || cacheConf?.LOCAL?.ENABLED) {
setTimeout(async () => {
this.logger.info('Recovering messages');
this.messagesLostCache.keys().then((keys) => {
keys.forEach(async (key) => {
const message = await this.messagesLostCache.get(key.split(':')[2]);
if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node') {
this.logger.verbose('Message absent from node, retrying to send, key: ' + key.split(':')[2]);
await this.client.sendMessageAck(JSON.parse(message.messageStubParameters[1], BufferJSON.reviver));
}
});
});
});
}, 30000);
}, 30000);
}
}
public get connectionStatus() {
@@ -456,12 +459,11 @@ export class BaileysStartupService extends WAStartupService {
private async defineAuthState() {
this.logger.verbose('Defining auth state');
const db = this.configService.get<Database>('DATABASE');
const redis = this.configService.get<Redis>('REDIS');
const cache = this.configService.get<CacheConf>('CACHE');
if (redis?.ENABLED) {
this.logger.verbose('Redis enabled');
this.cache.reference = this.instance.name;
return await useMultiFileAuthStateRedisDb(this.cache);
if (cache?.REDIS.ENABLED && cache?.REDIS.SAVE_INSTANCES) {
this.logger.info('Redis enabled');
return await useMultiFileAuthStateRedisDb(this.instance.name, this.cache);
}
if (db.SAVE_DATA.INSTANCE && db.ENABLED) {

View File

@@ -7,7 +7,6 @@ import { getMIMEType } from 'node-mime-types';
import { ConfigService, Database, WaBusiness } from '../../../config/env.config';
import { BadRequestException, InternalServerErrorException } from '../../../exceptions';
import { RedisCache } from '../../../libs/redis.client';
import { NumberBusiness } from '../../dto/chat.dto';
import {
ContactMessage,
@@ -34,7 +33,7 @@ export class BusinessStartupService extends WAStartupService {
public readonly configService: ConfigService,
public readonly eventEmitter: EventEmitter2,
public readonly repository: RepositoryBroker,
public readonly cache: RedisCache,
public readonly cache: CacheService,
public readonly chatwootCache: CacheService,
public readonly messagesLostCache: CacheService,
) {