Bull: If url is undefined options are ignored

Created on 3 Jan 2020  Â·  3Comments  Â·  Source: OptimalBits/bull

Description

If options are passed as the third argument to the queue constructor but the second argument is undefined the options are ignored. My use case is wanting to provide the redis URL via environment variable in production but connect to local redis in development.

Minimal, Working Test code to reproduce the issue.

const queue = new Queue('task', process.env.BULL_REDIS_URL, {
    prefix: 'my_bull_queue'
  });

Bull version

3.12.1

All 3 comments

Hi! This issue have the same root cause as https://github.com/OptimalBits/bull/issues/1338, the solution is to avoid url parameter at all — parse URL on your side and pass only queue name and options. In my project I use this modified function originally taken from bull sources, with rediss:// (ssl) support added:

const redisOptsFromUrl = (urlString: string): IORedis.RedisOptions => {
  const redisOpts: IORedis.RedisOptions = {};
  try {
    const redisUrl = url.parse(urlString);
    redisOpts.port = Number(redisUrl.port) || 6379;
    redisOpts.host = redisUrl.hostname;
    redisOpts.db = redisUrl.pathname ? Number(redisUrl.pathname.split("/")[1]) : 0;
    if (redisUrl.auth) {
      redisOpts.password = redisUrl.auth.split(":")[1];
    }
    if (redisUrl.protocol === "rediss:") {
      redisOpts.tls = {};
    }
  } catch (e) {
    throw new Error(e.message);
  }
  return redisOpts;
};

I was not able to use URLs with the rediss-protocol directly with Bull, and had to use stansv function to produce valid redis options for connecting to a TLS-server. The rediss protocol should be supported by ioredis, so this is a bit weird to me.

This is very frustrating since ioredis does support 'rediss' as part of the URL. My workaround was to use createClient instead of parsing the URL myself (and change it every time new feature added):

new Bull(name, {createClient: () => new Redis(URL)});
Was this page helpful?
0 / 5 - 0 ratings