Skip to content

Commit c4f39ab

Browse files
committed
fix: Improved how Redis works for instances
1 parent 8cb431a commit c4f39ab

File tree

16 files changed

+65
-55
lines changed

16 files changed

+65
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Correction in decryption of poll votes
77
* Change in the way the api sent and saved the sent messages, now it goes in the messages.upsert event
88
* Fixed cash when sending stickers via url
9+
* Improved how Redis works for instances
910

1011
# 1.1.2 (2023-06-28 13:43)
1112

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ ENV WEBHOOK_EVENTS_QRCODE_UPDATED=$WEBHOOK_EVENTS_QRCODE_UPDATED
5454
ENV WEBHOOK_EVENTS_MESSAGES_SET=$WEBHOOK_EVENTS_MESSAGES_SET
5555
ENV WEBHOOK_EVENTS_MESSAGES_UPDATE=$WEBHOOK_EVENTS_MESSAGES_UPDATE
5656
ENV WEBHOOK_EVENTS_MESSAGES_UPSERT=$WEBHOOK_EVENTS_MESSAGES_UPSERT
57-
ENV WEBHOOK_EVENTS_SEND_MESSAGE=$WEBHOOK_EVENTS_SEND_MESSAGE
5857
ENV WEBHOOK_EVENTS_CONTACTS_SET=$WEBHOOK_EVENTS_CONTACTS_SET
5958
ENV WEBHOOK_EVENTS_CONTACTS_UPSERT=$WEBHOOK_EVENTS_CONTACTS_UPSERT
6059
ENV WEBHOOK_EVENTS_CONTACTS_UPDATE=$WEBHOOK_EVENTS_CONTACTS_UPDATE

docker-compose.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ services:
5555
- WEBHOOK_EVENTS_MESSAGES_SET=true
5656
- WEBHOOK_EVENTS_MESSAGES_UPSERT=true
5757
- WEBHOOK_EVENTS_MESSAGES_UPDATE=true
58-
- WEBHOOK_EVENTS_SEND_MESSAGE=true
5958
- WEBHOOK_EVENTS_CONTACTS_SET=true
6059
- WEBHOOK_EVENTS_CONTACTS_UPSERT=true
6160
- WEBHOOK_EVENTS_CONTACTS_UPDATE=true

src/config/env.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export type EventsWebhook = {
6565
MESSAGES_SET: boolean;
6666
MESSAGES_UPSERT: boolean;
6767
MESSAGES_UPDATE: boolean;
68-
SEND_MESSAGE: boolean;
6968
CONTACTS_SET: boolean;
7069
CONTACTS_UPDATE: boolean;
7170
CONTACTS_UPSERT: boolean;
@@ -221,7 +220,6 @@ export class ConfigService {
221220
MESSAGES_SET: process.env?.WEBHOOK_EVENTS_MESSAGES_SET === 'true',
222221
MESSAGES_UPSERT: process.env?.WEBHOOK_EVENTS_MESSAGES_UPSERT === 'true',
223222
MESSAGES_UPDATE: process.env?.WEBHOOK_EVENTS_MESSAGES_UPDATE === 'true',
224-
SEND_MESSAGE: process.env?.WEBHOOK_EVENTS_SEND_MESSAGE === 'true',
225223
CONTACTS_SET: process.env?.WEBHOOK_EVENTS_CONTACTS_SET === 'true',
226224
CONTACTS_UPDATE: process.env?.WEBHOOK_EVENTS_CONTACTS_UPDATE === 'true',
227225
CONTACTS_UPSERT: process.env?.WEBHOOK_EVENTS_CONTACTS_UPSERT === 'true',

src/db/db.connect.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import { Logger } from '../config/logger.config';
55
const logger = new Logger('Db Connection');
66

77
const db = configService.get<Database>('DATABASE');
8-
export const dbserver = db.ENABLED
9-
? mongoose.createConnection(db.CONNECTION.URI, {
8+
export const dbserver = (() => {
9+
if (db.ENABLED) {
10+
const dbs = mongoose.createConnection(db.CONNECTION.URI, {
1011
dbName: db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api',
11-
})
12-
: null;
12+
});
13+
logger.info('ON - dbName: ' + dbs['$dbName']);
14+
process.on('beforeExit', () => {
15+
dbserver.destroy(true, (error) => logger.error(error));
16+
});
1317

14-
db.ENABLED ? logger.info('ON - dbName: ' + dbserver['$dbName']) : null;
18+
return dbs;
19+
}
20+
})();

src/db/redis.client.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@ import { BufferJSON } from '@whiskeysockets/baileys';
44
import { Redis } from '../config/env.config';
55

66
export class RedisCache {
7-
constructor(private readonly redisEnv: Partial<Redis>, private instanceName?: string) {
8-
this.client = createClient({ url: this.redisEnv.URI });
9-
10-
this.client.connect();
7+
constructor() {
8+
process.on('beforeExit', async () => {
9+
if (this.statusConnection) {
10+
await this.client.disconnect();
11+
}
12+
});
1113
}
1214

15+
private statusConnection = false;
16+
private instanceName: string;
17+
private redisEnv: Redis;
18+
1319
public set reference(reference: string) {
1420
this.instanceName = reference;
1521
}
1622

23+
public async connect(redisEnv: Redis) {
24+
this.client = createClient({ url: redisEnv.URI });
25+
await this.client.connect();
26+
this.statusConnection = true;
27+
this.redisEnv = redisEnv;
28+
}
29+
1730
private readonly logger = new Logger(RedisCache.name);
1831
private client: RedisClientType;
1932

@@ -35,6 +48,7 @@ export class RedisCache {
3548
public async writeData(field: string, data: any) {
3649
try {
3750
const json = JSON.stringify(data, BufferJSON.replacer);
51+
3852
return await this.client.hSet(
3953
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
4054
field,
@@ -51,6 +65,7 @@ export class RedisCache {
5165
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
5266
field,
5367
);
68+
5469
if (data) {
5570
return JSON.parse(data, BufferJSON.reviver);
5671
}
@@ -79,20 +94,4 @@ export class RedisCache {
7994
this.logger.error(error);
8095
}
8196
}
82-
83-
public async closeConnection() {
84-
try {
85-
await this.client.quit();
86-
} catch (error) {
87-
this.logger.error(error);
88-
}
89-
}
90-
91-
public async destructor() {
92-
await this.closeConnection();
93-
}
94-
95-
public async destroy() {
96-
await this.destructor();
97-
}
9897
}

src/dev-env.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ WEBHOOK:
9393
MESSAGES_SET: true
9494
MESSAGES_UPSERT: true
9595
MESSAGES_UPDATE: true
96-
SEND_MESSAGE: true
9796
CONTACTS_SET: true
9897
CONTACTS_UPSERT: true
9998
CONTACTS_UPDATE: true

src/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ function initWA() {
1616
}
1717

1818
function bootstrap() {
19-
initWA();
20-
2119
const logger = new Logger('SERVER');
2220
const app = express();
2321

@@ -34,8 +32,8 @@ function bootstrap() {
3432
methods: [...configService.get<Cors>('CORS').METHODS],
3533
credentials: configService.get<Cors>('CORS').CREDENTIALS,
3634
}),
37-
urlencoded({ extended: true, limit: '50mb' }),
38-
json({ limit: '50mb' }),
35+
urlencoded({ extended: true, limit: '136mb' }),
36+
json({ limit: '136mb' }),
3937
compression(),
4038
);
4139

@@ -73,6 +71,8 @@ function bootstrap() {
7371
logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT),
7472
);
7573

74+
initWA();
75+
7676
onUnexpectedError();
7777
}
7878

src/utils/use-multi-file-auth-state-redis-db.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,12 @@ import { RedisCache } from '../db/redis.client';
99
import { Logger } from '../config/logger.config';
1010
import { Redis } from '../config/env.config';
1111

12-
export async function useMultiFileAuthStateRedisDb(
13-
redisEnv: Partial<Redis>,
14-
instanceName: string,
15-
): Promise<{
12+
export async function useMultiFileAuthStateRedisDb(cache: RedisCache): Promise<{
1613
state: AuthenticationState;
1714
saveCreds: () => Promise<void>;
1815
}> {
1916
const logger = new Logger(useMultiFileAuthStateRedisDb.name);
2017

21-
const cache = new RedisCache(redisEnv, instanceName);
22-
2318
const writeData = async (data: any, key: string): Promise<any> => {
2419
try {
2520
return await cache.writeData(key, data);

src/validate/validate.schema.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export const instanceNameSchema: JSONSchema7 = {
3939
'MESSAGES_SET',
4040
'MESSAGES_UPSERT',
4141
'MESSAGES_UPDATE',
42-
'SEND_MESSAGE',
4342
'CONTACTS_SET',
4443
'CONTACTS_UPSERT',
4544
'CONTACTS_UPDATE',
@@ -802,7 +801,6 @@ export const webhookSchema: JSONSchema7 = {
802801
'MESSAGES_SET',
803802
'MESSAGES_UPSERT',
804803
'MESSAGES_UPDATE',
805-
'SEND_MESSAGE',
806804
'CONTACTS_SET',
807805
'CONTACTS_UPSERT',
808806
'CONTACTS_UPDATE',

0 commit comments

Comments
 (0)