@tonoid/bull
v2.0.1
Published
Bull plugin for @tonoid/helpers - handling messaging queues and background tasks
Downloads
29
Maintainers
Readme
@tonoid/bull
Bull plugin for @tonoid/helpers - handling messaging queues and background tasks
Init options
redisOptions.url
: (defaults:process.env.BULL_REDIS_URL || process.env.REDIS_URL
) Redis url, if set it overrides other auth options.redisOptions.db
: (defaults:process.env.BULL_REDIS_DB || process.env.REDIS_DB || '0'
) Redis database.redisOptions.errorHandler
: (default:(err) => {}
) Handle redis connect errormiddleware
: function to manipulate{ queues, queuesObject, redisOptions }
, useful if you're using admin ui like @bull-boardqueues
: (Array) Available queuesqueues[].name
: (string - required) Queue namequeues[].consumer
: (function({ queue, queues } - optional) - required) Consumer function to progress the queue
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(
{
redisOptions: {
url: 'redis://locahost:6379',
db: 0,
errorHandler: (err, scope) => console.error(scope, err)
},
queues: [
{ name: 'jsonStringify', consumer: jsonStringifyConsumer },
],
},
'myBull',
),
]);
const job = await context.myBull.jsonStringify.add({ foo: 'bar' });
const result = await job.finished();
console.log(result);
})();
Credits
This module is maintained by Simo Elalj @tonoid