Skip to content

Instantly share code, notes, and snippets.

@moltar
Created March 3, 2020 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moltar/4582e74fe4b03fb3158a23f13c1f1a06 to your computer and use it in GitHub Desktop.
Save moltar/4582e74fe4b03fb3158a23f13c1f1a06 to your computer and use it in GitHub Desktop.
A script that uses dynamic queue name discovery to build Bull Arena object, without having to manually specify each queue name.
import Arena from 'bull-arena'
import { uniq } from 'lodash'
import { getConnection } from './redis-connection'
import { config } from '../config'
const BULL_PREFIX = 'bull'
const HOST_ID = '__'
type Queue = Parameters<typeof Arena>[0]['queues'][0]
async function queueNames() {
const redis = getConnection()
// key format: bull:sum:active
const keys = await redis.keys(`${BULL_PREFIX}:*`)
// we no longer need the connection
redis.disconnect()
return uniq(keys.map(key => key.split(':')[1])).sort()
}
export async function arena() {
const names = await queueNames()
const queues = names.map<Queue>(name => {
return {
name,
redis: config.REDIS_URL,
hostId: HOST_ID,
prefix: BULL_PREFIX,
}
})
return Arena(
{ queues },
{
// port on which Arena will run
port: config.PORT,
},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment