peertube-plugin-livechat/bots/bots.ts

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-12-07 17:57:08 +00:00
import { BotsConfig } from './lib/config'
import { logger } from './lib/logger'
import { ComponentBot } from './lib/bot/component'
import { DemoBot } from './lib/bot/demobot'
2021-12-07 12:14:01 +00:00
2021-12-07 17:57:08 +00:00
if (!process.argv[2]) {
2021-12-07 12:14:01 +00:00
throw new Error('Missing parameter: the demobot configuration file path')
}
2021-12-07 17:57:08 +00:00
const botsConfig = new BotsConfig(process.argv[2])
2021-12-07 12:14:01 +00:00
2021-12-07 17:57:08 +00:00
const runningBots: ComponentBot[] = []
async function start (botsConfig: BotsConfig): Promise<void> {
await botsConfig.load()
let atLeastOne: boolean = false
if (botsConfig.useDemoBot()) {
atLeastOne = true
logger.info('Starting DemoBot...')
const config = botsConfig.getDemoBotConfig()
const instance = new DemoBot(
'DemoBot',
{
service: config.service,
domain: config.domain,
password: config.password
},
config.rooms,
'DemoBot' // FIXME: handle the case where the nick is already taken.
)
runningBots.push(instance)
instance.connect().catch(err => { throw err })
2021-12-07 12:14:01 +00:00
}
2021-12-07 17:57:08 +00:00
if (!atLeastOne) {
logger.info('No bot to launch, exiting.')
process.exit(0)
2021-12-07 12:14:01 +00:00
}
}
2021-12-07 17:57:08 +00:00
async function shutdown (): Promise<void> {
logger.info('Shutdown...')
for (const bot of runningBots) {
logger.info('Stopping the bot ' + bot.botName + '...')
await bot.stop()
}
2021-12-07 12:14:01 +00:00
process.exit(0)
}
2021-12-07 17:57:08 +00:00
// catching signals and do something before exit
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach((sig) => {
process.on(sig, () => {
logger.debug('Receiving signal: ' + sig)
shutdown().catch((err) => {
logger.error(`Error on shutting down: ${err as string}`)
2021-12-07 12:14:01 +00:00
})
2021-12-07 17:57:08 +00:00
})
2021-12-07 12:14:01 +00:00
})
2021-12-07 17:57:08 +00:00
start(botsConfig).catch((err) => {
logger.error(`Function start failed: ${err as string}`)
process.exit(1)
})