mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-18 19:32:21 -06:00
fix: Improved how Redis works for instances
This commit is contained in:
@@ -5,10 +5,16 @@ import { Logger } from '../config/logger.config';
|
||||
const logger = new Logger('Db Connection');
|
||||
|
||||
const db = configService.get<Database>('DATABASE');
|
||||
export const dbserver = db.ENABLED
|
||||
? mongoose.createConnection(db.CONNECTION.URI, {
|
||||
export const dbserver = (() => {
|
||||
if (db.ENABLED) {
|
||||
const dbs = mongoose.createConnection(db.CONNECTION.URI, {
|
||||
dbName: db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api',
|
||||
})
|
||||
: null;
|
||||
});
|
||||
logger.info('ON - dbName: ' + dbs['$dbName']);
|
||||
process.on('beforeExit', () => {
|
||||
dbserver.destroy(true, (error) => logger.error(error));
|
||||
});
|
||||
|
||||
db.ENABLED ? logger.info('ON - dbName: ' + dbserver['$dbName']) : null;
|
||||
return dbs;
|
||||
}
|
||||
})();
|
||||
|
||||
@@ -4,16 +4,29 @@ import { BufferJSON } from '@whiskeysockets/baileys';
|
||||
import { Redis } from '../config/env.config';
|
||||
|
||||
export class RedisCache {
|
||||
constructor(private readonly redisEnv: Partial<Redis>, private instanceName?: string) {
|
||||
this.client = createClient({ url: this.redisEnv.URI });
|
||||
|
||||
this.client.connect();
|
||||
constructor() {
|
||||
process.on('beforeExit', async () => {
|
||||
if (this.statusConnection) {
|
||||
await this.client.disconnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private statusConnection = false;
|
||||
private instanceName: string;
|
||||
private redisEnv: Redis;
|
||||
|
||||
public set reference(reference: string) {
|
||||
this.instanceName = reference;
|
||||
}
|
||||
|
||||
public async connect(redisEnv: Redis) {
|
||||
this.client = createClient({ url: redisEnv.URI });
|
||||
await this.client.connect();
|
||||
this.statusConnection = true;
|
||||
this.redisEnv = redisEnv;
|
||||
}
|
||||
|
||||
private readonly logger = new Logger(RedisCache.name);
|
||||
private client: RedisClientType;
|
||||
|
||||
@@ -35,6 +48,7 @@ export class RedisCache {
|
||||
public async writeData(field: string, data: any) {
|
||||
try {
|
||||
const json = JSON.stringify(data, BufferJSON.replacer);
|
||||
|
||||
return await this.client.hSet(
|
||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||
field,
|
||||
@@ -51,6 +65,7 @@ export class RedisCache {
|
||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||
field,
|
||||
);
|
||||
|
||||
if (data) {
|
||||
return JSON.parse(data, BufferJSON.reviver);
|
||||
}
|
||||
@@ -79,20 +94,4 @@ export class RedisCache {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async closeConnection() {
|
||||
try {
|
||||
await this.client.quit();
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async destructor() {
|
||||
await this.closeConnection();
|
||||
}
|
||||
|
||||
public async destroy() {
|
||||
await this.destructor();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user