Skip to content

Commit

Permalink
Fix CLI exit process.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnXLivingston committed Sep 19, 2023
1 parent 5a4cd9c commit e147b19
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.2.4

* Fix CLI exit.

## 0.2.3

* Fix the CLI connection process.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xmppjs-chat-bot",
"version": "0.2.3",
"version": "0.2.4",
"description": "Server-side XMPP chat bot",
"engines": {
"node": ">= 14.4.0"
Expand Down
23 changes: 21 additions & 2 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,32 @@ runCommand.action(async (options) => {
process.on(sig, () => {
console.info('Receiving signal: ' + sig)
console.info('Shutdown...')
const promises: Array<Promise<any>> = []
bots.forEach(bot => {
console.info('Stopping the bot ' + bot.botName + '...')
bot.disconnect().then(() => {}, (err) => {
console.error(`Error when stopping the bot ${bot.botName}: ${err as string}`)
const p = new Promise<void>(resolve => {
bot.disconnect().then(() => {}, (err) => {
console.error(`Error when stopping the bot ${bot.botName}: ${err as string}`)
}).finally(() => resolve())
})
promises.push(p)
})
bots.clear()

console.info('Waiting all bots to disconnect...')
Promise.all(promises).then(
() => {},
() => {}
).finally(() => {
console.info('We can now exit.')
process.exit()
})

// Just in case, we also set a max timeout
setTimeout(() => {
console.error('It seems the bots have not disconnected within 1000ms, exiting anyway.')
process.exit(1)
}, 1000)
})
})

Expand Down

0 comments on commit e147b19

Please sign in to comment.