@tonoid/bull

Bull plugin for @tonoid/helpers - handling messaging queues and background tasks


Keywords
bull, messaging queue, background task, queue, redis, helper, backend, microservice
License
MIT
Install
npm install @tonoid/bull@0.0.4

Documentation

@tonoid/bull

npm npm npm David GitHub stars

Bull plugin for @tonoid/helpers - handling messaging queues and background tasks

Init options

  • redis.host: (defaults: process.env.REDIS_HOST || 'mongo') Redis host.
  • redis.port: (defaults: process.env.REDIS_PORT || 27017) Redis port.
  • redis.password: (defaults: process.env.REDIS_PASSWORD || 'mongo') Redis password.
  • redis.db: (defaults: process.env.REDIS_DB || 'admin') Redis database.
  • redis.url: (defaults: process.env.REDIS_URL) Redis url, if set it overrides other auth options.
  • middleware: function to manipulate { queues, queuesObject, redis }, useful if you're using admin ui like @bull-board

Exported context attributes

  • .queues: Object containing all queues (useful to add to a queue)

Usage example

const { context, init } = require('@tonoid/helpers');
const bull = require('@tonoid/bull');

const jsonStringifyQueue = ({ queue }) => {
  queue.process(async (job) => {
    // Do something with job.data
    job.progress(10);
    job.log('Computing data');
    await new Promise((r) => setTimeout(r, 1000));
    job.log('Almost there');
    job.progress(90);
    return JSON.stringify(job.data);
  });
}

(async () => {
  await init([
    bull({
      redis: {
        url: 'redis://locahost:6379',
      },
      queues: [
        { name: 'jsonStringify', consumer: jsonStringifyConsumer },
      ],
    }),
  ]);

  const job = await context.bull.jsonStringify.add({ foo: 'bar' });
  const result = await job.finished();
  console.log(result);
})();

Credits

This module is maintained by Simo Elalj @tonoid