Skip to content

Commit 5c74cbf

Browse files
committed
improvement in restart instance to completely redo the connection
1 parent 3fdb3fa commit 5c74cbf

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Fixed
44

55
* Remove rabbitmq queues when delete instances
6+
* Improvement in restart instance to completely redo the connection
67

78
# 1.5.2 (2023-09-28 17:56)
89

src/whatsapp/controllers/instance.controller.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,19 @@ export class InstanceController {
475475
try {
476476
this.logger.verbose('requested restartInstance from ' + instanceName + ' instance');
477477

478-
this.logger.verbose('logging out instance: ' + instanceName);
479-
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
478+
const instance = this.waMonitor.waInstances[instanceName];
479+
const state = instance?.connectionStatus?.state;
480+
481+
switch (state) {
482+
case 'open':
483+
this.logger.verbose('logging out instance: ' + instanceName);
484+
await instance.reloadConnection();
485+
await delay(2000);
480486

481-
return { status: 'SUCCESS', error: false, response: { message: 'Instance restarted' } };
487+
return await this.connectionState({ instanceName });
488+
default:
489+
return await this.connectionState({ instanceName });
490+
}
482491
} catch (error) {
483492
this.logger.error(error);
484493
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,74 @@ export class WAStartupService {
12471247
}
12481248
}
12491249

1250+
public async reloadConnection(): Promise<WASocket> {
1251+
try {
1252+
this.instance.authState = await this.defineAuthState();
1253+
1254+
const { version } = await fetchLatestBaileysVersion();
1255+
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
1256+
const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
1257+
1258+
let options;
1259+
1260+
if (this.localProxy.enabled) {
1261+
this.logger.verbose('Proxy enabled');
1262+
options = {
1263+
agent: new ProxyAgent(this.localProxy.proxy as any),
1264+
fetchAgent: new ProxyAgent(this.localProxy.proxy as any),
1265+
};
1266+
}
1267+
1268+
const socketConfig: UserFacingSocketConfig = {
1269+
...options,
1270+
auth: {
1271+
creds: this.instance.authState.state.creds,
1272+
keys: makeCacheableSignalKeyStore(this.instance.authState.state.keys, P({ level: 'error' })),
1273+
},
1274+
logger: P({ level: this.logBaileys }),
1275+
printQRInTerminal: false,
1276+
browser,
1277+
version,
1278+
markOnlineOnConnect: this.localSettings.always_online,
1279+
connectTimeoutMs: 60_000,
1280+
qrTimeout: 40_000,
1281+
defaultQueryTimeoutMs: undefined,
1282+
emitOwnEvents: false,
1283+
msgRetryCounterCache: this.msgRetryCounterCache,
1284+
getMessage: async (key) => (await this.getMessage(key)) as Promise<proto.IMessage>,
1285+
generateHighQualityLinkPreview: true,
1286+
syncFullHistory: true,
1287+
userDevicesCache: this.userDevicesCache,
1288+
transactionOpts: { maxCommitRetries: 1, delayBetweenTriesMs: 10 },
1289+
patchMessageBeforeSending: (message) => {
1290+
const requiresPatch = !!(message.buttonsMessage || message.listMessage || message.templateMessage);
1291+
if (requiresPatch) {
1292+
message = {
1293+
viewOnceMessageV2: {
1294+
message: {
1295+
messageContextInfo: {
1296+
deviceListMetadataVersion: 2,
1297+
deviceListMetadata: {},
1298+
},
1299+
...message,
1300+
},
1301+
},
1302+
};
1303+
}
1304+
1305+
return message;
1306+
},
1307+
};
1308+
1309+
this.client = makeWASocket(socketConfig);
1310+
1311+
return this.client;
1312+
} catch (error) {
1313+
this.logger.error(error);
1314+
throw new InternalServerErrorException(error?.toString());
1315+
}
1316+
}
1317+
12501318
private readonly chatHandle = {
12511319
'chats.upsert': async (chats: Chat[], database: Database) => {
12521320
this.logger.verbose('Event received: chats.upsert');

0 commit comments

Comments
 (0)