Enhance Your Workflows with Bee-Queue: A Robust Node.js Queue Library
Bee-Queue is a high-performance, robust job and message queue library for Node.js applications. It aims to keep your jobs flowing seamlessly, providing a plethora of APIs to handle your job queues effectively. Below, we will introduce some of the commonly used APIs along with code snippets to help you understand their functionality.
Installing Bee-Queue
npm install bee-queue
Creating a Queue
const Queue = require('bee-queue'); const exampleQueue = new Queue('example');
Adding Jobs to the Queue
exampleQueue.createJob({ x: 2, y: 3 }).save();
Processing Jobs
exampleQueue.process((job, done) => {
const result = job.data.x + job.data.y;
done(null, result);
});
Job Events
Job Progress
exampleQueue.on('progress', (jobId, progress) => {
console.log('Job', jobId, 'is', progress, '% complete');
});
Job Succeeded
exampleQueue.on('succeeded', (job, result) => {
console.log('Job', job.id, 'succeeded with result', result);
});
Job Failed
exampleQueue.on('failed', (job, err) => {
console.log('Job', job.id, 'failed with error', err);
});
Pausing and Resuming the Queue
exampleQueue.pause().then(() => {
console.log('Queue paused');
// Resume and process the queue
return exampleQueue.resume();
}).then(() => {
console.log('Queue resumed');
});
Example Application with Bee-Queue
const Queue = require('bee-queue');
// Initialize the queue const mathQueue = new Queue('math');
// Add a job const job = mathQueue.createJob({ x: 1, y: 2 }).save();
// Process the job mathQueue.process((job, done) => {
const { x, y } = job.data;
const result = x + y;
done(null, result);
});
// Listen for job success mathQueue.on('succeeded', (job, result) => {
console.log(`Job ${job.id} succeeded with result: ${result}`);
});
// Listen for job failure mathQueue.on('failed', (job, err) => {
console.log(`Job ${job.id} failed with error: ${err}`);
});
// Pause and resume the queue mathQueue.pause().then(() => {
console.log('Queue is paused');
return mathQueue.resume();
}).then(() => {
console.log('Queue is resumed');
});
By leveraging Bee-Queue, you can ensure your Node.js applications handle jobs efficiently and effectively, maintaining high performance and robustness.
Hash: c06be98e5cdcc13ca72d61f559430b0d8dcc8e9c8999a9b22277ed7c719268bc