npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@avocajs/mq

v1.0.0

Published

A memory queue package for AvocaJS

Downloads

4

Readme

@AvocaJS/MQ

A simple, efficient in-memory queue for managing and processing jobs using a First-In-First-Out (FIFO) approach with support for timed execution and size limits.

Table of Contents

Introduction

The @avocajs/mq package provides an in-memory queue for managing and processing jobs. It is designed to handle jobs using a FIFO (First In, First Out) approach, with support for timed execution and size limits.

What is a Queue?

A queue is a data structure that manages a collection of elements in a particular order. The most common use case for queues is to manage tasks or jobs, where each task is processed in the order it was added to the queue.

In-Memory Queue vs. Database Queue

  • In-Memory Queue: Stores all jobs in the memory of the running application. This approach offers very fast access and processing times but is limited by the available memory and is volatile, meaning all data is lost if the application crashes or restarts.
  • Database Queue: Stores jobs in a persistent database. This approach is more resilient to application crashes and restarts, can handle larger volumes of jobs, and can be distributed across multiple instances. However, it typically involves slower access and processing times due to database read/write operations.

When to Use This Package

Use the @avocajs/mq package when you need a lightweight, fast, and simple way to manage and process jobs within the memory of your application. This package is ideal for scenarios where:

  • Low latency job processing is critical.
  • The job volume is manageable within the available memory.
  • Persistence is not a primary concern, and losing jobs on crashes or restarts is acceptable.

Installation

Install the package using npm:

npm install @avocajs/mq

Usage

Creating a Queue

You can create a new queue by instantiating the MQ class. The constructor requires two arguments: MaxQueueTime and maxQueueSize.

import { MQ } from "@avocajs/mq";

const queue = new MQ(1000, 10); // MaxQueueTime = 1000ms (1s), maxQueueSize = 10

Adding Jobs to the Queue

You can add jobs (functions) to the queue using the push method.

queue.push(() => console.log("Job 1"));
queue.push(() => console.log("Job 2"));

Pulling Jobs from the Queue

You can pull a single job from the queue using the pull method.

const job = queue.pull();
if (job) {
  job(); // Execute the job
}

Batch Processing

You can pull all jobs from the queue using the batch method.

const jobs = queue.batch();
if (jobs) {
  jobs.forEach((job) => job()); // Execute all jobs
}

Queue Size

You can check the current size of the queue using the size method.

console.log(queue.size()); // Outputs the number of jobs in the queue

Checking for Jobs

You can check if the queue has any jobs using the hasJob and hasNoJob methods.

console.log(queue.hasJob()); // Returns true if there are jobs in the queue
console.log(queue.hasNoJob()); // Returns true if there are no jobs in the queue

Check if Job Can Be Added

You should always use the shouldPush method to check if a job can be added to the queue before using the push method. This helps prevent errors and ensures that you do not exceed the maximum queue size.

if (queue.shouldPush()) {
  queue.push(() => console.log("Job 1"));
} else {
  console.log("Queue is full. Cannot add job.");
}

Event Handling

The queue emits events when certain conditions are met, such as MaxQueueSize and MaxQueueTime.

MaxQueueSize Event

This event is emitted when the queue is full.

queue.on("MaxQueueSize", (job) => {
  // Log issue, push the job back, or increase the MaxQueueTime!
  console.log("MaxQueueSize event triggered");
  job(); // You have access to the job
});

MaxQueueTime Event

This event is emitted when a job has been in the queue longer than MaxQueueTime.

queue.on("MaxQueueTime", (job) => {
  // Log issue, push the job back, or increase the MaxQueueTime!
  console.log("MaxQueueTime event triggered");
  job(); // You have access to the job
});

Error Handling

The MQ class throws errors for invalid MaxQueueTime and maxQueueSize values.

try {
  const queue = new MQ(0, 10); // Invalid MaxQueueTime
} catch (error) {
  console.error(error.message); // The 'MaxQueueTime' must be an integer greater than 0
}

try {
  const queue = new MQ(1000, 0); // Invalid maxQueueSize
} catch (error) {
  console.error(error.message); // The 'MaxQueueSize' must be an integer greater than 0
}

Differences Between In-Memory Queues and Database Queues

  • In-Memory Queues:
    • Pros: Very fast access and processing times, simple implementation.
    • Cons: Limited by available memory, data loss on application crash/restart.
  • Database Queues:
    • Pros: Persistent storage, can handle large volumes of jobs, resilient to crashes/restarts, can be distributed.
    • Cons: Slower access and processing times due to database operations, more complex implementation.

License

This project is licensed under the ISC License. See the LICENSE file for more details.

Author

Essefri Mohamed - [email protected]