Skip to content

Commit

Permalink
[core] WEBJS restart session on WAState.OPENING
Browse files Browse the repository at this point in the history
  • Loading branch information
devlikepro committed Sep 27, 2024
1 parent 94df9a6 commit 72b067c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/core/engines/webjs/session.webjs.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
Message,
MessageMedia,
Reaction,
WAState,
} from 'whatsapp-web.js';
import { Message as MessageInstance } from 'whatsapp-web.js/src/structures';

Expand All @@ -88,6 +89,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
protected engineConfig?: WebJSConfig;

private startDelayedJob: SingleDelayedJobRunner;
private engineStateCheckDelayedJob: SingleDelayedJobRunner;
private shouldRestart: boolean;

whatsapp: WebjsClient;
Expand All @@ -104,6 +106,11 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
this.START_ATTEMPT_DELAY_SECONDS * SECOND,
this.logger,
);
this.engineStateCheckDelayedJob = new SingleDelayedJobRunner(
'engine-state-check',
2 * SECOND,
this.logger,
);
}

/**
Expand Down Expand Up @@ -314,6 +321,45 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
this.qr.save('');
this.logger.info(`Session '${this.name}' has been disconnected!`);
});

this.whatsapp.on(Events.STATE_CHANGED, (state: WAState) => {
const badStates = [WAState.OPENING, WAState.TIMEOUT];
const log = this.logger.child({ state: state, event: 'change_state' });

log.debug('Session engine state changed');
if (!badStates.includes(state)) {
return;
}

log.info(`Session state changed to bad state, waiting for recovery...`);
this.engineStateCheckDelayedJob.schedule(async () => {
if (!this.startDelayedJob.scheduled) {
log.info('Session is restarting already, skip check.');
return;
}

if (!this.whatsapp) {
log.warn('Session is not initialized, skip recovery.');
return;
}

const currentState = await this.whatsapp.getState().catch((error) => {
log.error('Failed to get current state');
log.error(error, error.stack);
return null;
});
log.setBindings({ currentState: currentState });
if (!currentState) {
log.warn('Session has no current state, skip restarting.');
return;
} else if (badStates.includes(currentState)) {
log.info('Session is still in bad state, restarting...');
this.restartClient();
return;
}
log.info('Session has recovered, no need to restart.');
});
});
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/utils/SingleDelayedJobRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ export class SingleDelayedJobRunner {
});
}

get scheduled(): boolean {
return !!this.timeout;
}

schedule(fn: FunctionNoArgs): boolean {
if (this.timeout) {
if (this.scheduled) {
const msg = `Job has been started before, do not schedule it again`;
this.log(this.warningOverride, msg);
return false;
Expand Down

0 comments on commit 72b067c

Please sign in to comment.