Created
March 3, 2020 06:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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