mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-25 22:57:44 -06:00
fix: Improved how Redis works for instances
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user