-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
136 lines (110 loc) 路 4.04 KB
/
index.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
BullAdapter,
BullMQAdapter,
createBullBoard,
ExpressAdapter
} from '@bull-board/express'
import Queue3 from 'bull'
import { ConnectionOptions, Queue as QueueMQ } from 'bullmq'
import express from 'express'
import { BaseAdapter } from '@bull-board/api/dist/src/queueAdapters/base'
import styles from 'ansis'
import { splitQueueList } from './utils/split-queue-list/split-queue-list'
const environments = {
port: process.env.PORT || 4000,
queuePrefix: process.env.QUEUE_PREFIX,
queueNames: process.env.QUEUE_NAMES || '',
redisPort: parseInt(process.env.REDIS_PORT || '6379'),
redisHost: process.env.REDIS_HOST || 'localhost',
redisUsername: process.env.REDIS_USERNAME || 'default',
redisPassword: process.env.REDIS_PASSWORD || ''
}
const redisOptions: ConnectionOptions = {
host: environments.redisHost,
port: environments.redisPort,
username: environments.redisUsername,
password: environments.redisPassword
}
const createQueue3 = (name: string) =>
new Queue3(name, {
redis: redisOptions,
prefix: environments.queuePrefix
})
const createQueueMQ = (name: string) =>
new QueueMQ(name, {
connection: redisOptions,
prefix: environments.queuePrefix
})
const run = async () => {
console.log(styles.yellow('Starting Bull Board with: \n'))
console.log('Redis')
console.log(`- redis://${environments.redisHost}:${environments.redisPort}`)
console.log(`- Username: ${environments.redisUsername}`)
console.log(
`- Password: ${environments.redisPassword ? '******' : '<empty>'}`
)
console.log('Configs')
console.log(`- Prefix: ${environments.queuePrefix}`)
console.log(`- Queues: ${environments.queueNames}`)
const app = express()
const queueBullList: Queue3.Queue<any>[] = []
const queueBullMqList: QueueMQ<any, any, string>[] = []
const queuesList = splitQueueList(environments.queueNames)
for await (const queueName of queuesList) {
console.log(`Creating processor to queue: "${queueName}"`)
const queueBull = createQueue3(queueName)
const queueBullMq = createQueueMQ(queueName)
queueBullList.push(queueBull)
queueBullMqList.push(queueBullMq)
}
app.use('/add', (req, res) => {
const opts = req.query.opts || ({} as any)
if (opts.delay) {
opts.delay = +opts.delay * 1000 // delay must be a number
}
const exampleBull = createQueue3('ExampleBull')
const exampleBullMq = createQueueMQ('ExampleBullMQ')
exampleBull.add({ title: req.query.title }, opts)
exampleBullMq.add('Add', { title: req.query.title }, opts)
queueBullList.forEach((queue) =>
queue.add({ title: req.query.title }, opts)
)
queueBullMqList.forEach((queue) =>
queue.add('Add', { title: req.query.title }, opts)
)
res.json({
ok: true
})
})
const serverAdapter: any = new ExpressAdapter()
serverAdapter.setBasePath('/bull-board')
const adaptersList: BaseAdapter[] = []
queueBullList.forEach((queue) => adaptersList.push(new BullAdapter(queue)))
queueBullMqList.forEach((queue) =>
adaptersList.push(new BullMQAdapter(queue))
)
createBullBoard({ queues: adaptersList, serverAdapter })
console.log('')
console.log('Mapped Queues')
console.log('Bull adapter:')
console.log(`${queueBullList.map((queue) => `- ${queue.name}`).join('\n')}`)
console.log('BullMq adapters:')
console.log(`${queueBullMqList.map((queue) => `- ${queue.name}`).join('\n')}`)
console.log('')
app.use('/', serverAdapter.getRouter())
app.listen(environments.port, () => {
console.log(`Running on ${environments.port}...`)
console.log(`For the UI, open http://localhost:${environments.port}`)
console.log(`Make sure Redis is running on port ${environments.redisPort}`)
console.log('')
console.log('To populate the queue, run:')
console.log(
` curl http://localhost:${environments.port}/add?title=Example`
)
console.log('To populate the queue with custom options (opts), run:')
console.log(
` curl http://localhost:${environments.port}/add?title=Test&opts[delay]=10`
)
})
}
run().catch((e) => console.error(e))