Skip to content

Commit 61bd5b3

Browse files
committed
fix: Adjusts in redis for save instances
1 parent 4ed1335 commit 61bd5b3

File tree

18 files changed

+179
-209
lines changed

18 files changed

+179
-209
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
### Fixed
44
* Revert fix audio encoding
55
* Recovering messages lost with redis cache
6+
* Adjusts in redis for save instances
67
* Adjusts in proxy
78
* Revert pull request #523
89
* Added instance name on logs
910
* Added support for Spanish
10-
* Fix error: invalid operator. The allowed operators for identifier are equal_to,not_equal_to
11+
* Fix error: invalid operator. The allowed operators for identifier are equal_to,not_equal_to in chatwoot
1112

1213
# 1.7.2 (2024-04-12 17:31)
1314

Docker/.env.example

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ CLEAN_STORE_CHATS=true
3333

3434
# Permanent data storage
3535
DATABASE_ENABLED=false
36-
DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
36+
DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin &
37+
readPreference=primary &
38+
ssl=false &
39+
directConnection=true
3740
DATABASE_CONNECTION_DB_PREFIX_NAME=evdocker
3841

3942
# Choose the data you want to save in the application's database or store
@@ -43,10 +46,6 @@ DATABASE_SAVE_MESSAGE_UPDATE=false
4346
DATABASE_SAVE_DATA_CONTACTS=false
4447
DATABASE_SAVE_DATA_CHATS=false
4548

46-
REDIS_ENABLED=false
47-
REDIS_URI=redis://redis:6379
48-
REDIS_PREFIX_KEY=evdocker
49-
5049
RABBITMQ_ENABLED=false
5150
RABBITMQ_RABBITMQ_MODE=global
5251
RABBITMQ_EXCHANGE_NAME=evolution_exchange
@@ -73,7 +72,7 @@ WEBHOOK_GLOBAL_URL=''
7372
WEBHOOK_GLOBAL_ENABLED=false
7473
# With this option activated, you work with a url per webhook event, respecting the global url and the name of each event
7574
WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=false
76-
## Set the events you want to hear
75+
## Set the events you want to hear
7776
WEBHOOK_EVENTS_APPLICATION_STARTUP=false
7877
WEBHOOK_EVENTS_QRCODE_UPDATED=true
7978
WEBHOOK_EVENTS_MESSAGES_SET=true
@@ -129,6 +128,14 @@ CHATWOOT_MESSAGE_READ=false # false | true
129128
CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=postgres://user:password@hostname:port/dbname
130129
CHATWOOT_IMPORT_DATABASE_PLACEHOLDER_MEDIA_MESSAGE=true
131130

131+
CACHE_REDIS_ENABLED=false
132+
CACHE_REDIS_URI=redis://redis:6379
133+
CACHE_REDIS_PREFIX_KEY=evolution
134+
CACHE_REDIS_TTL=604800
135+
CACHE_REDIS_SAVE_INSTANCES=false
136+
CACHE_LOCAL_ENABLED=false
137+
CACHE_LOCAL_TTL=604800
138+
132139
# Defines an authentication type for the api
133140
# We recommend using the apikey because it will allow you to use a custom token,
134141
# if you use jwt, a random token will be generated and may be expired and you will have to generate a new token
@@ -143,4 +150,4 @@ AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
143150
AUTHENTICATION_JWT_EXPIRIN_IN=0
144151
AUTHENTICATION_JWT_SECRET='L=0YWt]b2w[WF>#>:&E`'
145152

146-
LANGUAGE=en # pt-BR, en
153+
LANGUAGE=en # pt-BR, en

Docker/evolution-api-all-services/.env.example

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ CLEAN_STORE_CHATS=true
3333

3434
# Permanent data storage
3535
DATABASE_ENABLED=true
36-
DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
36+
DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin &
37+
readPreference=primary &
38+
ssl=false &
39+
directConnection=true
3740
DATABASE_CONNECTION_DB_PREFIX_NAME=evolution
3841

3942
# Choose the data you want to save in the application's database or store
@@ -43,18 +46,14 @@ DATABASE_SAVE_MESSAGE_UPDATE=false
4346
DATABASE_SAVE_DATA_CONTACTS=false
4447
DATABASE_SAVE_DATA_CHATS=false
4548

46-
REDIS_ENABLED=true
47-
REDIS_URI=redis://redis:6379
48-
REDIS_PREFIX_KEY=evolution
49-
5049
# Global Webhook Settings
5150
# Each instance's Webhook URL and events will be requested at the time it is created
5251
## Define a global webhook that will listen for enabled events from all instances
5352
WEBHOOK_GLOBAL_URL=''
5453
WEBHOOK_GLOBAL_ENABLED=false
5554
# With this option activated, you work with a url per webhook event, respecting the global url and the name of each event
5655
WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=false
57-
## Set the events you want to hear
56+
## Set the events you want to hear
5857
WEBHOOK_EVENTS_APPLICATION_STARTUP=false
5958
WEBHOOK_EVENTS_QRCODE_UPDATED=true
6059
WEBHOOK_EVENTS_MESSAGES_SET=true
@@ -87,6 +86,14 @@ CONFIG_SESSION_PHONE_NAME=chrome
8786
# Set qrcode display limit
8887
QRCODE_LIMIT=30
8988

89+
CACHE_REDIS_ENABLED=false
90+
CACHE_REDIS_URI=redis://redis:6379
91+
CACHE_REDIS_PREFIX_KEY=evolution
92+
CACHE_REDIS_TTL=604800
93+
CACHE_REDIS_SAVE_INSTANCES=false
94+
CACHE_LOCAL_ENABLED=false
95+
CACHE_LOCAL_TTL=604800
96+
9097
# Defines an authentication type for the api
9198
# We recommend using the apikey because it will allow you to use a custom token,
9299
# if you use jwt, a random token will be generated and may be expired and you will have to generate a new token
@@ -109,4 +116,4 @@ AUTHENTICATION_INSTANCE_NAME=evolution
109116
AUTHENTICATION_INSTANCE_WEBHOOK_URL=''
110117
AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID=1
111118
AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN=123456
112-
AUTHENTICATION_INSTANCE_CHATWOOT_URL=''
119+
AUTHENTICATION_INSTANCE_CHATWOOT_URL=''

Dockerfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ ENV DATABASE_SAVE_MESSAGE_UPDATE=false
5858
ENV DATABASE_SAVE_DATA_CONTACTS=false
5959
ENV DATABASE_SAVE_DATA_CHATS=false
6060

61-
ENV REDIS_ENABLED=false
62-
ENV REDIS_URI=redis://redis:6379
63-
ENV REDIS_PREFIX_KEY=evolution
64-
6561
ENV RABBITMQ_ENABLED=false
6662
ENV RABBITMQ_MODE=global
6763
ENV RABBITMQ_EXCHANGE_NAME=evolution_exchange
@@ -129,6 +125,14 @@ ENV QRCODE_COLOR=#198754
129125

130126
ENV TYPEBOT_API_VERSION=latest
131127

128+
ENV CACHE_REDIS_ENABLED=false
129+
ENV CACHE_REDIS_URI=redis://redis:6379
130+
ENV CACHE_REDIS_PREFIX_KEY=evolution
131+
ENV CACHE_REDIS_TTL=604800
132+
ENV CACHE_REDIS_SAVE_INSTANCES=false
133+
ENV CACHE_LOCAL_ENABLED=false
134+
ENV CACHE_LOCAL_TTL=604800
135+
132136
ENV AUTHENTICATION_TYPE=apikey
133137

134138
ENV AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976

src/api/abstract/abstract.cache.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
export interface ICache {
22
get(key: string): Promise<any>;
33

4+
hGet(key: string, field: string): Promise<any>;
5+
46
set(key: string, value: any, ttl?: number): void;
57

8+
hSet(key: string, field: string, value: any): Promise<void>;
9+
610
has(key: string): Promise<boolean>;
711

812
keys(appendCriteria?: string): Promise<string[]>;
913

1014
delete(key: string | string[]): Promise<number>;
1115

16+
hDelete(key: string, field: string): Promise<any>;
17+
1218
deleteAll(appendCriteria?: string): Promise<number>;
1319
}

src/api/controllers/instance.controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { v4 } from 'uuid';
66
import { ConfigService, HttpServer, WaBusiness } from '../../config/env.config';
77
import { Logger } from '../../config/logger.config';
88
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
9-
import { RedisCache } from '../../libs/redis.client';
109
import { InstanceDto, SetPresenceDto } from '../dto/instance.dto';
1110
import { ChatwootService } from '../integrations/chatwoot/services/chatwoot.service';
1211
import { RabbitmqService } from '../integrations/rabbitmq/services/rabbitmq.service';
@@ -41,7 +40,7 @@ export class InstanceController {
4140
private readonly typebotService: TypebotService,
4241
private readonly integrationService: IntegrationService,
4342
private readonly proxyService: ProxyController,
44-
private readonly cache: RedisCache,
43+
private readonly cache: CacheService,
4544
private readonly chatwootCache: CacheService,
4645
private readonly messagesLostCache: CacheService,
4746
) {}

src/api/guards/instance.guard.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { NextFunction, Request, Response } from 'express';
22
import { existsSync } from 'fs';
33
import { join } from 'path';
44

5-
import { configService, Database, Redis } from '../../config/env.config';
5+
import { CacheConf, configService, Database } from '../../config/env.config';
66
import { INSTANCE_DIR } from '../../config/path.config';
77
import {
88
BadRequestException,
@@ -17,12 +17,13 @@ import { cache, waMonitor } from '../server.module';
1717
async function getInstance(instanceName: string) {
1818
try {
1919
const db = configService.get<Database>('DATABASE');
20-
const redisConf = configService.get<Redis>('REDIS');
20+
const cacheConf = configService.get<CacheConf>('CACHE');
2121

2222
const exists = !!waMonitor.waInstances[instanceName];
2323

24-
if (redisConf.ENABLED) {
25-
const keyExists = await cache.keyExists();
24+
if (cacheConf.REDIS.ENABLED && cacheConf.REDIS.SAVE_INSTANCES) {
25+
const keyExists = await cache.has(instanceName);
26+
2627
return exists || keyExists;
2728
}
2829

src/api/server.module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { configService } from '../config/env.config';
33
import { eventEmitter } from '../config/event.config';
44
import { Logger } from '../config/logger.config';
55
import { dbserver } from '../libs/db.connect';
6-
import { RedisCache } from '../libs/redis.client';
76
import { ChatController } from './controllers/chat.controller';
87
import { GroupController } from './controllers/group.controller';
98
import { InstanceController } from './controllers/instance.controller';
@@ -107,7 +106,7 @@ export const repository = new RepositoryBroker(
107106
dbserver?.getClient(),
108107
);
109108

110-
export const cache = new RedisCache();
109+
export const cache = new CacheService(new CacheEngine(configService, 'instance').getEngine());
111110
const chatwootCache = new CacheService(new CacheEngine(configService, ChatwootService.name).getEngine());
112111
const messagesLostCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine());
113112

src/api/services/cache.service.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { BufferJSON } from '@whiskeysockets/baileys';
2+
13
import { Logger } from '../../config/logger.config';
24
import { ICache } from '../abstract/abstract.cache';
35

@@ -20,6 +22,21 @@ export class CacheService {
2022
return this.cache.get(key);
2123
}
2224

25+
public async hGet(key: string, field: string) {
26+
try {
27+
const data = await this.cache.hGet(key, field);
28+
29+
if (data) {
30+
return JSON.parse(data, BufferJSON.reviver);
31+
}
32+
33+
return null;
34+
} catch (error) {
35+
this.logger.error(error);
36+
return null;
37+
}
38+
}
39+
2340
async set(key: string, value: any) {
2441
if (!this.cache) {
2542
return;
@@ -28,6 +45,16 @@ export class CacheService {
2845
this.cache.set(key, value);
2946
}
3047

48+
public async hSet(key: string, field: string, value: any) {
49+
try {
50+
const json = JSON.stringify(value, BufferJSON.replacer);
51+
52+
await this.cache.hSet(key, field, json);
53+
} catch (error) {
54+
this.logger.error(error);
55+
}
56+
}
57+
3158
async has(key: string) {
3259
if (!this.cache) {
3360
return;
@@ -44,6 +71,16 @@ export class CacheService {
4471
return this.cache.delete(key);
4572
}
4673

74+
async hDelete(key: string, field: string) {
75+
try {
76+
await this.cache.hDelete(key, field);
77+
return true;
78+
} catch (error) {
79+
this.logger.error(error);
80+
return false;
81+
}
82+
}
83+
4784
async deleteAll(appendCriteria?: string) {
4885
if (!this.cache) {
4986
return;

src/api/services/monitor.service.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import { Db } from 'mongodb';
55
import { Collection } from 'mongoose';
66
import { join } from 'path';
77

8-
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
8+
import { Auth, CacheConf, ConfigService, Database, DelInstance, HttpServer } from '../../config/env.config';
99
import { Logger } from '../../config/logger.config';
1010
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
1111
import { NotFoundException } from '../../exceptions';
12-
import { RedisCache } from '../../libs/redis.client';
1312
import {
1413
AuthModel,
1514
ChamaaiModel,
@@ -34,7 +33,7 @@ export class WAMonitoringService {
3433
private readonly eventEmitter: EventEmitter2,
3534
private readonly configService: ConfigService,
3635
private readonly repository: RepositoryBroker,
37-
private readonly cache: RedisCache,
36+
private readonly cache: CacheService,
3837
private readonly chatwootCache: CacheService,
3938
private readonly messagesLostCache: CacheService,
4039
) {
@@ -44,15 +43,15 @@ export class WAMonitoringService {
4443
this.noConnection();
4544

4645
Object.assign(this.db, configService.get<Database>('DATABASE'));
47-
Object.assign(this.redis, configService.get<Redis>('REDIS'));
46+
Object.assign(this.redis, configService.get<CacheConf>('CACHE'));
4847

4948
this.dbInstance = this.db.ENABLED
5049
? this.repository.dbServer?.db(this.db.CONNECTION.DB_PREFIX_NAME + '-instances')
5150
: undefined;
5251
}
5352

5453
private readonly db: Partial<Database> = {};
55-
private readonly redis: Partial<Redis> = {};
54+
private readonly redis: Partial<CacheConf> = {};
5655

5756
private dbInstance: Db;
5857

@@ -213,7 +212,7 @@ export class WAMonitoringService {
213212
});
214213
this.logger.verbose('instance files deleted: ' + name);
215214
});
216-
} else if (!this.redis.ENABLED) {
215+
} else if (!this.redis.REDIS.ENABLED && !this.redis.REDIS.SAVE_INSTANCES) {
217216
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
218217
for await (const dirent of dir) {
219218
if (dirent.isDirectory()) {
@@ -248,10 +247,9 @@ export class WAMonitoringService {
248247
return;
249248
}
250249

251-
if (this.redis.ENABLED) {
250+
if (this.redis.REDIS.ENABLED && this.redis.REDIS.SAVE_INSTANCES) {
252251
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
253-
this.cache.reference = instanceName;
254-
await this.cache.delAll();
252+
await this.cache.delete(instanceName);
255253
return;
256254
}
257255

@@ -304,7 +302,7 @@ export class WAMonitoringService {
304302
this.logger.verbose('Loading instances');
305303

306304
try {
307-
if (this.redis.ENABLED) {
305+
if (this.redis.REDIS.ENABLED && this.redis.REDIS.SAVE_INSTANCES) {
308306
await this.loadInstancesFromRedis();
309307
} else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
310308
await this.loadInstancesFromDatabase();
@@ -377,12 +375,11 @@ export class WAMonitoringService {
377375

378376
private async loadInstancesFromRedis() {
379377
this.logger.verbose('Redis enabled');
380-
await this.cache.connect(this.redis as Redis);
381-
const keys = await this.cache.getInstanceKeys();
378+
const keys = await this.cache.keys();
382379

383380
if (keys?.length > 0) {
384381
this.logger.verbose('Reading instance keys and setting instances');
385-
await Promise.all(keys.map((k) => this.setInstance(k.split(':')[1])));
382+
await Promise.all(keys.map((k) => this.setInstance(k.split(':')[2])));
386383
} else {
387384
this.logger.verbose('No instance keys found');
388385
}

0 commit comments

Comments
 (0)