1.0.3 • Published 6 months ago

@gotocva/queue v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

šŸ“¦ @gotocva/queue

An in-memory event queue for Node.js with retry and backoff support.

NPM Version
License
Downloads


šŸš€ Features

āœ… 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


šŸ“„ Installation

Install via npm:

npm install @gotocva/queue

Install via yarn:

yarn add @gotocva/queue

šŸ“Œ Usage

1ļøāƒ£ Basic Example

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 });

šŸ“– API Reference

šŸ— new Queue()

Creates a new instance of the in-memory queue.

šŸ“Œ .add(job, options)

Adds a job to the queue.

Parameters

  • 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)

Example

queue.add({ id: 1, task: 'Send Email' }, { attempts: 3, backoff: 5000 });

šŸ”„ .on('process', async (job, resolve, reject) => { ... })

Registers a listener that processes jobs.

Parameters

  • job (Object) → The job being processed
  • resolve (Function) → Call this when the job succeeds
  • reject (Function) → Call this when the job fails

Example

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
  }
});

⚔ Advanced Example

Simulating API Calls & Retrying on Failure

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 });

šŸ›  How It Works

StepDescription
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.

šŸ“Œ Why Use @gotocva/queue?

āœ… 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


āš–ļø Comparison with Other Queues

Feature@gotocva/queueBull (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

šŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.


šŸ‘Øā€šŸ’» Author

gotocva - GitHub


⭐ Support

If you like this project, give it a ⭐ on GitHub! šŸš€