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

easy-worker-queue

v1.0.3

Published

Simple worker thread namespace based queue with support for scheduled jobs

Downloads

74

Readme

Easy Worker Queue

Easy Worker Queue is a lightweight library designed for managing asynchronous tasks in Node.js applications efficiently, using worker threads. It simplifies the process of handling task execution, errors, and completion.

Installation

You can install Easy Worker Queue via npm:

npm install easy-worker-queue

Usage

const { Queue } = require("easy-worker-queue");
const path = require("path");

// Create a new instance of the Queue
const queue = new Queue();

// Create a named queue "firstQueue" with a worker file and optional configurations
queue.createNameSpace("firstQueue", path.join(__dirname, "worker.js"), {
  isAsync: true,
  onError: (err) => {
    console.log(err);
  },
  onExit: (exitCode) => {
    console.log(exitCode);
  },
  onSuccess: (data) => {
    console.log(data);
  },
  jobsLimit: 5,
});

// Create another named queue "secondQueue" with a different worker file
queue.createNameSpace("secondQueue", path.join(__dirname, "other-worker.js"), {
  isAsync: true,
  onError: (err) => {
    console.log(err);
  },
  onExit: (exitCode) => {
    console.log(exitCode);
  },
  onSuccess: (data) => {
    console.log(data);
  },
});

// Enqueue tasks to the created queues at intervals
setInterval(() => {
  queue.enqueue("firstQueue", 123);
  queue.enqueue("secondQueue", { message: "ok" });
}, 5000);

Explanation

In this example:

  • Note: The worker.js file must be a JavaScript file that exports a default function using module.exports.
  • Two named queues, "firstQueue" and "secondQueue", are created using queue.createNameSpace().
  • Each queue is associated with a specific worker file (worker.js and other-worker.js).
  • Optional configuration options such as error handling (onError), exit handling (onExit), success handling (onSuccess), and job limit (jobsLimit) can be specified.
  • Tasks are enqueued to the queues at regular intervals using queue.enqueue().

Using CronQueue for Scheduled Tasks

const { CronQueue } = require("easy-worker-queue");
const path = require("path");

// Create a new instance of the CronQueue
const queue = new CronQueue();

// Create a named queue "firstQueue" with a worker file and optional configurations
queue.createNameSpace("firstQueue", path.join(__dirname, "worker.js"), {
  isAsync: true,
  onError: (err) => {
    console.log(err);
  },
  onExit: (exitCode) => {
    console.log(exitCode);
  },
  onSuccess: (data) => {
    console.log(data);
  },
  jobsLimit: 5,
});

// Create another named queue "secondQueue" with a different worker file
queue.createNameSpace("secondQueue", path.join(__dirname, "other-worker.js"), {
  isAsync: true,
  onError: (err) => {
    console.log(err);
  },
  onExit: (exitCode) => {
    console.log(exitCode);
  },
  onSuccess: (data) => {
    console.log(data);
  },
});

// Schedule tasks for the created queues using cron expressions
queue.schedule("firstQueue", "* * * * *");
queue.schedule("secondQueue", "*/3 * * * *");

Explanation

In this example:

  • Note: The worker.js file must be a JavaScript file that exports a default function using module.exports.
  • Two named queues, "firstQueue" and "secondQueue", are created using queue.createNameSpace().
  • Each queue is associated with a specific worker file (worker.js and other-worker.js).
  • Optional configuration options such as error handling (onError), exit handling (onExit), success handling (onSuccess), and job limit (jobsLimit) can be specified.
  • The CronQueue class allows scheduling tasks based on cron expressions.
  • The schedule() method of the CronQueue class is used to schedule tasks for each queue, specifying the cron expression.

Properties

name

  • Description: Specifies the name of the queue namespace.
  • Type: String
  • Example: "firstQueue"

workerPath

  • Description: Specifies the path to the worker file associated with the queue.
  • Type: String
  • Example: path.join(__dirname, "worker.js")

options

  • Description: Optional configuration options for the queue.
  • Type: Object
  • Properties:
    • isAsync: Indicates whether the worker function is asynchronous.
      • Type: Boolean
      • Default: false
      • Example: true
    • onError: Callback function to handle errors that occur during task execution.
      • Type: Function
      • Default: undefined
      • Example:
        onError: (err) => {
          console.log(err);
        };
    • onExit: Callback function to handle the exit event of the worker thread.
      • Type: Function
      • Default: undefined
      • Example:
        onExit: (exitCode) => {
          console.log(exitCode);
        };
    • onSuccess: Callback function to handle successful completion of tasks.
      • Type: Function
      • Default: undefined
      • Example:
        onSuccess: (data) => {
          console.log(data);
        };
    • jobsLimit: Specifies the maximum number of jobs that can be queued before skipping new ones.
      • Type: Number
      • Default: undefined
      • Example: 5