Skip to content

Commit 87640ea

Browse files
committed
[core] WEBJS - avoid STARTING => FAILED cycles
fix #1071
1 parent eb8f771 commit 87640ea

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/core/engines/webjs/session.webjs.core.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
238238
this.logger.debug(
239239
'Should not restart the client, ignoring restart request',
240240
);
241+
this.end().catch((error) => {
242+
this.logger.error(error, 'Failed to end() the client');
243+
});
241244
return;
242245
}
243246

@@ -469,15 +472,20 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
469472
});
470473

471474
this.whatsapp.on(Events.AUTHENTICATION_FAILURE, (args) => {
472-
this.failed();
473475
this.qr.save('');
476+
this.shouldRestart = false;
474477
this.logger.info({ args: args }, `Session has failed to authenticate!`);
478+
this.failed();
475479
});
476480

477481
this.whatsapp.on(Events.DISCONNECTED, (args) => {
478-
this.failed();
482+
if (args === 'LOGOUT') {
483+
this.logger.warn({ args: args }, `Session has been logged out!`);
484+
this.shouldRestart = false;
485+
}
479486
this.qr.save('');
480487
this.logger.info({ args: args }, `Session has been disconnected!`);
488+
this.failed();
481489
});
482490

483491
this.whatsapp.on(Events.STATE_CHANGED, (state: WAState) => {
@@ -508,7 +516,8 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
508516
});
509517
log.setBindings({ currentState: currentState });
510518
if (!currentState) {
511-
log.warn('Session has no current state, skip restarting.');
519+
log.warn('Session has no current state, restarting...');
520+
this.restartClient();
512521
return;
513522
} else if (badStates.includes(currentState)) {
514523
log.info('Session is still in bad state, restarting...');

0 commit comments

Comments
 (0)