Skip to content

Commit f858074

Browse files
committed
prevent duplicate reloading
1 parent d0ef5f8 commit f858074

5 files changed

Lines changed: 19 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unoapi-cloud",
3-
"version": "2.8.0",
3+
"version": "2.8.1",
44
"description": "Unoapi Cloud",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/services/auto_connect.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ export const autoConnect = async (
3737
try {
3838
const store = await config.getStore(phone, config)
3939
const { sessionStore } = store
40-
if ((await sessionStore.isStatusConnecting(phone)) || (await sessionStore.isStatusOnline(phone))) {
41-
logger.info(`Update session status to auto connect ${phone}...`)
40+
const currentStatus = await sessionStore.getStatus(phone)
41+
if (['connecting', 'online', 'reloading'].includes(currentStatus)) {
42+
logger.info(`Update session current status ${currentStatus} to offline in session ${phone}...`)
4243
await sessionStore.setStatus(phone, 'offline')
4344
}
4445
getClient({ phone, listener, getConfig, onNewLogin })

src/services/reload_baileys.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ export class ReloadBaileys extends Reload {
2727
logger.debug('Reload broker for phone %s', phone)
2828
return super.run(phone)
2929
}
30+
const store = await config.getStore(phone, config)
31+
const { sessionStore } = store
32+
const currentStatus = await sessionStore.getStatus(phone)
33+
if (currentStatus == 'reloading') {
34+
logger.info('Already reloading session %s!', phone)
35+
return
36+
}
37+
await sessionStore.setStatus(phone, 'reloading')
3038
const currentClient = await this.getClient({
3139
phone,
3240
listener: this.listener,
3341
getConfig: this.getConfig,
3442
onNewLogin: this.onNewLogin,
3543
})
36-
const store = await config.getStore(phone, config)
37-
const { sessionStore } = store
38-
if ((await sessionStore.isStatusOnline(phone)) || (await sessionStore.isStatusStandBy(phone)) || (await sessionStore.isStatusConnecting(phone))) {
44+
if (['online', 'standby', 'connecting'].includes(currentStatus)) {
3945
logger.warn('Reload disconnect session %s!', phone)
4046
await currentClient?.disconnect()
4147
}

src/services/session_store.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MAX_CONNECT_RETRY } from '../defaults'
22
import logger from './logger'
33

4-
export type sessionStatus = 'offline' | 'online' | 'disconnected' | 'connecting' | 'standby' | 'restart_required'
4+
export type sessionStatus = 'offline' | 'online' | 'disconnected' | 'connecting' | 'standby' | 'restart_required' | 'reloading'
55

66
const statuses: Map<string, string> = new Map<string, string>()
77
const retries: Map<string, number> = new Map<string, number>()
@@ -23,6 +23,10 @@ export abstract class SessionStore {
2323
return (await this.getStatus(phone)) == 'online'
2424
}
2525

26+
async isStatusReloading(phone: string) {
27+
return (await this.getStatus(phone)) == 'reloading'
28+
}
29+
2630
async isStatusConnecting(phone: string) {
2731
return (await this.getStatus(phone)) == 'connecting'
2832
}

src/services/session_store_redis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class SessionStoreRedis extends SessionStore {
4747

4848
async setStatus(phone: string, status: sessionStatus) {
4949
logger.info(`Session status ${phone} change from ${await this.getStatus(phone)} to ${status}`)
50-
if (['online', 'restart_required'].includes(status)) {
50+
if (['online', 'restart_required', 'reloading'].includes(status)) {
5151
await this.clearConnectCount(phone)
5252
}
5353
return setSessionStatus(phone, status)

0 commit comments

Comments
 (0)