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

uniqmanager

v1.0.3

Published

A Redis based nodejs queue, with global workers

Downloads

17

Readme

UniQManager

UniQManager is a Node.js library for managing queues with Redis. It supports adding data to queues, starting worker processes to handle the queued data, and checking the status of jobs.

One unique feature is it support one worker per queue, globally withing multiple instances of nodejs server. This is useful when you want to process data in a queue in a sequential manner.

Installation

To install the UniQManager package, use npm:

npm install uniqmanager

Usage

Initialization

To use UniQManager, first, import it and initialize it with your Redis configuration and options.

const UniQManager = require('uniqmanager');

const uniQManager = new UniQManager({
  redisConfig: {
    host: '127.0.0.1',
    port: '6379'
  },
  options: {
    maxWorkers: 5,
    finishedAge: 10,
    failedAge: 60,
    debug: true,
    callbacksMap: {
      logProject: async (data) => {
        console.log('Running logProject callback');
        await new Promise((resolve) => setTimeout(resolve, 2000));
        console.log('Logging the current project data received from queue:', data);
      }
    }
  }
});

Options

redisConfig: The Redis configuration object containing the host and port. It also supports username and password. options: The options object containing the following properties:

  • maxWorkers: The maximum number of workers to start, Default: 5.
  • finishedAge: The time in second to keep finished jobs in the database, Default: 3600.
  • failedAge: The time in second to keep failed jobs in the database, Default: 86400.
  • debug: A boolean value to enable or disable debug mode, Default: false.
  • callbacksMap: An object containing the action names and their corresponding callback functions, Default: {}.

Callbacks

The callbacksMap object contains the action names and their corresponding callback functions. The callback functions are asynchronous and take the data as an argument. Its important to note that the callback function should be defined before adding data to the queue. and the action name should be the same as the one used when adding data to the queue.

callbacksMap: {
  logProject: async (data) => {
    console.log('Running logProject callback');
    await new Promise((resolve) => setTimeout(resolve, 2000));
    console.log('Logging the current project data received from queue:', data);
  }
}

Adding Data to a Queue

You can add data to a queue using the addToQ method. This method takes the queue name, the data to add, and the action name.

async function addDataToQueue() {
  const noOfQueue = 5;
  const noOfData = 10;

  for (let i = 0; i < noOfQueue; i++) {
    for (let j = 0; j < noOfData; j++) {
      const result = await uniQManager.addToQ(`queue-no-${i}`, { projectName: 'Queue-' + i, projectId: `${i}${j}` }, 'logProject');
      console.log('Data added:', result);
    }
  }
}

Starting Worker Processes

Start worker processes to handle the queued data using the startWorkers method. You can specify the number of workers.

function main() {
  uniQManager.startWorkers(5);
  addDataToQueue();
}

main();

Getting Job Status

You can check the status of a job using the getJobStatus method.

async function checkJobStatus(jobId) {
  const status = await uniQManager.getJobStatus(jobId);
  console.log('Job status:', status);
}

API

UniQManager Class

Constructor

new UniQManager(props);
  • props: An object containing redisConfig and options.

Methods

  • addToQ(queueName, data, action): Adds data to a specified queue.

    • queueName: The name of the queue.
    • data: The data to add to the queue.
    • action: The action name.
  • startWorkers(): Starts worker processes.

  • getJobStatus(jobId): Retrieves the status of a job by its ID.

    • jobId: The job ID.

Example

Here's a complete example to illustrate how to use UniQManager:

const UniQManager = require('uniqmanager');

const uniQManager = new UniQManager({
  redisConfig: {
    host: '127.0.0.1',
    port: '6379'
  },
  options: {
    maxWorkers: 5,
    finishedAge: 10,
    failedAge: 60,
    debug: true,
    callbacksMap: {
      logProject: async (data) => {
        console.log('Running logProject callback');
        await new Promise((resolve) => setTimeout(resolve, 2000));
        console.log('Logging the current project data received from queue:', data);
      }
    }
  }
});

async function addDataToQueue() {
  const noOfQueue = 5;
  const noOfData = 10;

  for (let i = 0; i < noOfQueue; i++) {
    for (let j = 0; j < noOfData; j++) {
      const result = await uniQManager.addToQ(`queue-no-${i}`, { projectName: 'Queue-' + i, projectId: `${i}${j}` }, 'logProject');
      console.log('Data added:', result);
    }
  }
}

function main() {
  uniQManager.startWorkers(5);
  addDataToQueue();
}

main();

License

This project is licensed under the MIT License.