forked from LyoSU/quote-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
38 lines (29 loc) · 1.08 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const { Telegraf } = require('telegraf')
const cluster = require('cluster')
const { QueueManager } = require('./queueManager')
const numCPUs = require('os').cpus().length
const BOT_TOKEN = process.env.BOT_TOKEN
const MAX_WORKERS = process.env.MAX_WORKERS || numCPUs
const MAX_UPDATES_PER_WORKER = 30
const MAX_QUEUE_SIZE = 1000
const QUEUE_WARNING_THRESHOLD = 0.8
const PAUSE_THRESHOLD = 0.9
const RESUME_THRESHOLD = 0.7
if (cluster.isMaster) {
const { setupMaster } = require('./master')
const bot = new Telegraf(BOT_TOKEN, {
handlerTimeout: 100
})
if (!(bot instanceof Telegraf)) {
throw new Error('Не вдалося створити екземпляр Telegraf')
}
const queueManager = new QueueManager(MAX_QUEUE_SIZE, QUEUE_WARNING_THRESHOLD, PAUSE_THRESHOLD, RESUME_THRESHOLD)
setupMaster(bot, queueManager, MAX_WORKERS, MAX_UPDATES_PER_WORKER)
bot.launch()
// Graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))
} else {
const { setupWorker } = require('./worker')
setupWorker(BOT_TOKEN)
}