Skip to content

gotocva/queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ @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

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.

πŸ“Œ 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/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

πŸ“œ 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! πŸš€

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published