improvement in restart instance to completely redo the connection

This commit is contained in:
Davidson Gomes 2023-09-30 16:36:32 -03:00
parent 3fdb3fa673
commit 5c74cbfe19
3 changed files with 81 additions and 3 deletions

View File

@ -3,6 +3,7 @@
### Fixed
* Remove rabbitmq queues when delete instances
* Improvement in restart instance to completely redo the connection
# 1.5.2 (2023-09-28 17:56)

View File

@ -475,10 +475,19 @@ export class InstanceController {
try {
this.logger.verbose('requested restartInstance from ' + instanceName + ' instance');
this.logger.verbose('logging out instance: ' + instanceName);
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
const instance = this.waMonitor.waInstances[instanceName];
const state = instance?.connectionStatus?.state;
return { status: 'SUCCESS', error: false, response: { message: 'Instance restarted' } };
switch (state) {
case 'open':
this.logger.verbose('logging out instance: ' + instanceName);
await instance.reloadConnection();
await delay(2000);
return await this.connectionState({ instanceName });
default:
return await this.connectionState({ instanceName });
}
} catch (error) {
this.logger.error(error);
}

View File

@ -1247,6 +1247,74 @@ export class WAStartupService {
}
}
public async reloadConnection(): Promise<WASocket> {
try {
this.instance.authState = await this.defineAuthState();
const { version } = await fetchLatestBaileysVersion();
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
let options;
if (this.localProxy.enabled) {
this.logger.verbose('Proxy enabled');
options = {
agent: new ProxyAgent(this.localProxy.proxy as any),
fetchAgent: new ProxyAgent(this.localProxy.proxy as any),
};
}
const socketConfig: UserFacingSocketConfig = {
...options,
auth: {
creds: this.instance.authState.state.creds,
keys: makeCacheableSignalKeyStore(this.instance.authState.state.keys, P({ level: 'error' })),
},
logger: P({ level: this.logBaileys }),
printQRInTerminal: false,
browser,
version,
markOnlineOnConnect: this.localSettings.always_online,
connectTimeoutMs: 60_000,
qrTimeout: 40_000,
defaultQueryTimeoutMs: undefined,
emitOwnEvents: false,
msgRetryCounterCache: this.msgRetryCounterCache,
getMessage: async (key) => (await this.getMessage(key)) as Promise<proto.IMessage>,
generateHighQualityLinkPreview: true,
syncFullHistory: true,
userDevicesCache: this.userDevicesCache,
transactionOpts: { maxCommitRetries: 1, delayBetweenTriesMs: 10 },
patchMessageBeforeSending: (message) => {
const requiresPatch = !!(message.buttonsMessage || message.listMessage || message.templateMessage);
if (requiresPatch) {
message = {
viewOnceMessageV2: {
message: {
messageContextInfo: {
deviceListMetadataVersion: 2,
deviceListMetadata: {},
},
...message,
},
},
};
}
return message;
},
};
this.client = makeWASocket(socketConfig);
return this.client;
} catch (error) {
this.logger.error(error);
throw new InternalServerErrorException(error?.toString());
}
}
private readonly chatHandle = {
'chats.upsert': async (chats: Chat[], database: Database) => {
this.logger.verbose('Event received: chats.upsert');