Skip to content

Commit

Permalink
fix(core): support bot.isActive, fix reconnect, close #153
Browse files Browse the repository at this point in the history
regression by 8f06f3b
  • Loading branch information
shigma committed Sep 7, 2023
1 parent 798ea7b commit 1525957
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 21 deletions.
2 changes: 0 additions & 2 deletions adapters/discord/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export class WsClient extends Adapter.WsClient<DiscordBot> {
if (parsed.d) return
this._sessionId = ''
logger.warn('offline: invalid session')
this.bot.offline()
this.bot.socket?.close()
}

Expand All @@ -92,7 +91,6 @@ export class WsClient extends Adapter.WsClient<DiscordBot> {
}

if (parsed.op === Gateway.Opcode.RECONNECT) {
this.bot.offline()
logger.warn('offline: discord request reconnect')
this.bot.socket?.close()
}
Expand Down
10 changes: 2 additions & 8 deletions adapters/mail/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@ export class MailBot extends Bot<MailBot.Config> {
}

onClose() {
if (this.status === 'disconnect') {
this.offline()
return
}
if (!this.isActive) return
logger.info('IMAP disconnected, will reconnect in 3s...')
this.status = 'reconnect'
setTimeout(() => {
if (this.status !== 'reconnect') {
this.offline()
return
}
if (!this.isActive) return
this.imap.connect()
}, 3000)
}
Expand Down
8 changes: 2 additions & 6 deletions adapters/telegram/src/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export class HttpPolling extends Adapter.Client<TelegramBot> {
offset: this.offset + 1,
timeout: Math.ceil(bot.config.pollingTimeout / 1000), // in seconds
})
if (bot.status === 'offline' || bot.status === 'disconnect') {
return
}
if (!bot.isActive) return
bot.online()
_retryCount = 0
_initial = false
Expand All @@ -60,9 +58,7 @@ export class HttpPolling extends Adapter.Client<TelegramBot> {
bot.error = e
return bot.status = 'offline'
}
if (bot.status === 'offline' || bot.status === 'disconnect') {
return
}
if (!bot.isActive) return
_retryCount++
bot.status = 'reconnect'
this.timeout = setTimeout(polling, retryInterval)
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ export namespace Adapter {
socket.addEventListener('close', ({ code, reason }) => {
bot.socket = null
logger.debug(`websocket closed with ${code}`)
if (bot.status === 'disconnect') {
return bot.status = 'offline'
}
if (!bot.isActive) return

const message = reason.toString() || `failed to connect to ${url}, code: ${code}`
let timeout = retryInterval
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export abstract class Bot<T extends Bot.Config = Bot.Config> {
}
}

get isActive() {
return this._status !== 'offline' && this._status !== 'disconnect'
}

online() {
this.status = 'online'
this.error = null
Expand All @@ -76,7 +80,7 @@ export abstract class Bot<T extends Bot.Config = Bot.Config> {
}

async start() {
if (['connect', 'reconnect', 'online'].includes(this.status)) return
if (this.isActive) return
this.status = 'connect'
try {
await this.context.parallel('bot-connect', this)
Expand All @@ -87,7 +91,7 @@ export abstract class Bot<T extends Bot.Config = Bot.Config> {
}

async stop() {
if (['disconnect', 'offline'].includes(this.status)) return
if (!this.isActive) return
this.status = 'disconnect'
try {
await this.context.parallel('bot-disconnect', this)
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class Session {
}
this.selfId = bot.selfId
this.platform = bot.platform
this.locales = []
defineProperty(this, 'bot', bot)
defineProperty(this, 'app', bot.ctx.root)
defineProperty(this, 'id', ++bot.ctx.internal.counter)
Expand Down

0 comments on commit 1525957

Please sign in to comment.