An in-memory event queue for Node.js with retry and backoff support.
β
In-memory queue (No Redis, No Database)
β
Auto-retry failed jobs (with configurable attempts)
β
Backoff delay before retry
β
Process jobs sequentially
β
Simple API, Lightweight, and Fast
Install via npm:
npm install @gotocva/queue
Install via yarn:
yarn add @gotocva/queue
const Queue = require('@gotocva/queue');
const jobQueue = new Queue();
// Register event listener to process jobs
jobQueue.on('process', async (job, resolve, reject) => {
console.log(`Processing job: ${JSON.stringify(job)}`);
// Simulate a failing job (50% failure rate)
if (Math.random() > 0.5) {
return reject(new Error('Random Job Failure'));
}
setTimeout(resolve, 1000); // Simulate async task success
});
// Add jobs with retry & backoff
jobQueue.add({ id: 1, task: 'Send Email' }, { attempts: 3, backoff: 5000 });
jobQueue.add({ id: 2, task: 'Generate Report' }, { attempts: 3, backoff: 5000 });
Creates a new instance of the in-memory queue.
Adds a job to the queue.
job
(Object) β Job data (e.g.,{ id: 1, task: 'Send Email' }
)options
(Object, optional):attempts
(Number) β Number of times to retry on failure (default:3
)backoff
(Number) β Delay before retrying in ms (default:5000
)
queue.add({ id: 1, task: 'Send Email' }, { attempts: 3, backoff: 5000 });
Registers a listener that processes jobs.
job
(Object) β The job being processedresolve
(Function) β Call this when the job succeedsreject
(Function) β Call this when the job fails
queue.on('process', async (job, resolve, reject) => {
try {
console.log(`Processing job: ${job.task}`);
resolve(); // Mark job as complete
} catch (error) {
reject(error); // Retry if job fails
}
});
const Queue = require('@gotocva/queue');
const axios = require('axios');
const apiQueue = new Queue();
// Process jobs (making API request)
apiQueue.on('process', async (job, resolve, reject) => {
console.log(`Calling API: ${job.url}`);
try {
const response = await axios.get(job.url);
console.log(`β
API Success: ${response.status}`);
resolve();
} catch (error) {
console.error(`β API Failed: ${error.message}`);
reject(error);
}
});
// Add an API call job
apiQueue.add({ url: 'https://jsonplaceholder.typicode.com/posts/1' }, { attempts: 5, backoff: 3000 });
Step | Description |
---|---|
1οΈβ£ | Jobs are added to the queue using .add(job, options) . |
2οΈβ£ | The process event is triggered for each job. |
3οΈβ£ | If the job succeeds, resolve() is called. |
4οΈβ£ | If the job fails, reject(error) is called, and the job retries based on attempts . |
5οΈβ£ | Failed jobs wait (backoff ms) before retrying. |
β
No Dependencies β No need for Redis or external services
β
Simple API β Just add jobs and listen for events
β
Fast & Lightweight β Built for high performance
β
Supports Retries β Ensures jobs are processed reliably
Feature | @gotocva/queue | Bull (Redis) | Kue (Redis) |
---|---|---|---|
No Redis Required | β Yes | β No | β No |
Retry & Backoff | β Yes | β Yes | β Yes |
In-Memory | β Yes | β No | β No |
Simple API | β Yes | β Complex | β Complex |
Good for Small Apps | β Yes | β No | β No |
This project is licensed under the MIT License - see the LICENSE file for details.
gotocva - GitHub
If you like this project, give it a β on GitHub! π