fix: Improved how Redis works for instances

This commit is contained in:
Davidson Gomes
2023-07-04 12:38:29 -03:00
parent 8cb431ad40
commit c4f39ab85c
16 changed files with 65 additions and 55 deletions

View File

@@ -10,6 +10,7 @@ import { WAStartupService } from '../services/whatsapp.service';
import { WebhookService } from '../services/webhook.service';
import { Logger } from '../../config/logger.config';
import { wa } from '../types/wa.types';
import { RedisCache } from '../../db/redis.client';
export class InstanceController {
constructor(
@@ -19,6 +20,7 @@ export class InstanceController {
private readonly eventEmitter: EventEmitter2,
private readonly authService: AuthService,
private readonly webhookService: WebhookService,
private readonly cache: RedisCache,
) {}
private readonly logger = new Logger(InstanceController.name);
@@ -47,6 +49,7 @@ export class InstanceController {
this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = instanceName;
this.waMonitor.waInstances[instance.instanceName] = instance;
@@ -92,6 +95,7 @@ export class InstanceController {
this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = instanceName;
this.waMonitor.waInstances[instance.instanceName] = instance;
@@ -171,6 +175,7 @@ export class InstanceController {
this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = instanceName;

View File

@@ -9,7 +9,7 @@ import {
NotFoundException,
} from '../../exceptions';
import { InstanceDto } from '../dto/instance.dto';
import { waMonitor } from '../whatsapp.module';
import { cache, waMonitor } from '../whatsapp.module';
import { Database, Redis, configService } from '../../config/env.config';
import { RedisCache } from '../../db/redis.client';
@@ -20,7 +20,6 @@ async function getInstance(instanceName: string) {
const exists = !!waMonitor.waInstances[instanceName];
if (redisConf.ENABLED) {
const cache = new RedisCache(redisConf, instanceName);
const keyExists = await cache.keyExists();
return exists || keyExists;
}

View File

@@ -14,14 +14,15 @@ import {
import { RepositoryBroker } from '../repository/repository.manager';
import { NotFoundException } from '../../exceptions';
import { Db } from 'mongodb';
import { RedisCache } from '../../db/redis.client';
import { initInstance } from '../whatsapp.module';
import { RedisCache } from '../../db/redis.client';
export class WAMonitoringService {
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly configService: ConfigService,
private readonly repository: RepositoryBroker,
private readonly cache: RedisCache,
) {
this.removeInstance();
this.noConnection();
@@ -33,15 +34,12 @@ export class WAMonitoringService {
this.dbInstance = this.db.ENABLED
? this.repository.dbServer?.db(this.db.CONNECTION.DB_PREFIX_NAME + '-instances')
: undefined;
this.redisCache = this.redis.ENABLED ? new RedisCache(this.redis) : undefined;
}
private readonly db: Partial<Database> = {};
private readonly redis: Partial<Redis> = {};
private dbInstance: Db;
private redisCache: RedisCache;
private readonly logger = new Logger(WAMonitoringService.name);
public readonly waInstances: Record<string, WAStartupService> = {};
@@ -144,6 +142,7 @@ export class WAMonitoringService {
],
});
});
} else if (this.redis.ENABLED) {
} else {
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
for await (const dirent of dir) {
@@ -176,8 +175,8 @@ export class WAMonitoringService {
}
if (this.redis.ENABLED) {
this.redisCache.reference = instanceName;
await this.redisCache.delAll();
this.cache.reference = instanceName;
await this.cache.delAll();
return;
}
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
@@ -189,6 +188,7 @@ export class WAMonitoringService {
this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = name;
await instance.connectToWhatsapp();
@@ -197,7 +197,8 @@ export class WAMonitoringService {
try {
if (this.redis.ENABLED) {
const keys = await this.redisCache.instanceKeys();
await this.cache.connect(this.redis as Redis);
const keys = await this.cache.instanceKeys();
if (keys?.length > 0) {
keys.forEach(async (k) => await set(k.split(':')[1]));
} else {

View File

@@ -115,12 +115,14 @@ import { dbserver } from '../../db/db.connect';
import NodeCache from 'node-cache';
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
import sharp from 'sharp';
import { RedisCache } from '../../db/redis.client';
export class WAStartupService {
constructor(
private readonly configService: ConfigService,
private readonly eventEmitter: EventEmitter2,
private readonly repository: RepositoryBroker,
private readonly cache: RedisCache,
) {
this.cleanStore();
this.instance.qrcode = { count: 0 };
@@ -478,7 +480,8 @@ export class WAStartupService {
const redis = this.configService.get<Redis>('REDIS');
if (redis?.ENABLED) {
return await useMultiFileAuthStateRedisDb(redis, this.instance.name);
this.cache.reference = this.instance.name;
return await useMultiFileAuthStateRedisDb(this.cache);
}
if (db.SAVE_DATA.INSTANCE && db.ENABLED) {

View File

@@ -9,7 +9,6 @@ export enum Events {
MESSAGES_SET = 'messages.set',
MESSAGES_UPSERT = 'messages.upsert',
MESSAGES_UPDATE = 'messages.update',
SEND_MESSAGE = 'send.message',
CONTACTS_SET = 'contacts.set',
CONTACTS_UPSERT = 'contacts.upsert',
CONTACTS_UPDATE = 'contacts.update',

View File

@@ -29,6 +29,7 @@ import { AuthRepository } from './repository/auth.repository';
import { WAStartupService } from './services/whatsapp.service';
import { delay } from '@whiskeysockets/baileys';
import { Events } from './types/wa.types';
import { RedisCache } from '../db/redis.client';
const logger = new Logger('WA MODULE');
@@ -49,7 +50,14 @@ export const repository = new RepositoryBroker(
dbserver?.getClient(),
);
export const waMonitor = new WAMonitoringService(eventEmitter, configService, repository);
export const cache = new RedisCache();
export const waMonitor = new WAMonitoringService(
eventEmitter,
configService,
repository,
cache,
);
const authService = new AuthService(configService, waMonitor, repository);
@@ -64,6 +72,7 @@ export const instanceController = new InstanceController(
eventEmitter,
authService,
webhookService,
cache,
);
export const viewsController = new ViewsController(waMonitor, configService);
export const sendMessageController = new SendMessageController(waMonitor);
@@ -71,7 +80,7 @@ export const chatController = new ChatController(waMonitor);
export const groupController = new GroupController(waMonitor);
export async function initInstance() {
const instance = new WAStartupService(configService, eventEmitter, repository);
const instance = new WAStartupService(configService, eventEmitter, repository, cache);
const mode = configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;