New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

simple-worker-thread-queue

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-worker-thread-queue

Simple Node.js queue that executes jobs asynchronously with worker threads

latest
Source
npmnpm
Version
1.0.8
Version published
Maintainers
1
Created
Source

Simple Worker Thread Queue

Provides an simply in-memory queue that manages the processing of jobs. Jobs are processed asychronously with Node.js Worker Threads.

Queue behavior is as follows:

  • Queue exists only in-memory, not persisted
  • Jobs begin processing immediately when added
  • Jobs are processed sequentially one at a time, unless the CONCURRENT_WORKER_THREADS configuration in a .env file is greater than 1
  • Optional completion callbacks can be provided to Jobs or Batches to track processing completion

Install

To install as a dependency in your project:

npm install simple-worker-thread-queue

Use

Typescript examples are available in ./examples/typescript, but the basic steps are:

  • Define a custom Job processing function in a seperate file and export it as processJob.
// CustomWorker.js
export async function processJob (jobOptions, job) {
  const result = {
    message: `Hello ${jobOptions.name}`,
  };
  // result will be saved to the job data
  return result;
};
  • Create a Queue in your project file and pass the absolute path to the file exporting processJob.
import { Queue } from 'simple-worker-thread-queue';

const queue = new Queue({
  processJobExportPath: path.join(__dirname, './CustomWorker.js')
});
  • Add a Job to the Queue. Monitor Job completion with a completionCallback function.
const completionCallback = async (completedJob) => {
  console.log(`job ${completedJob.getId()} completed with result: ${completedJob.getData().message}`);
};

const jobOptions = {
  name: 'Taylor',
  completionCallback,
};

const job = queue.add(jobOptions);

Batches

Jobs can be grouped together into a Batch. By supplying a batch completion callback function, consuming services can be nofitied when the processing of the batched jobs is complete.

import { Batch } from 'simple-worker-thread-queue';

const completionCallback = async function (completedBatch) {
  console.log(`batch ${completedBatch.getId()} completed with ${completedBatch.getJobs().length} jobs`);
}
const batch = new Batch(completionCallback);

// add jobs to queue with batch parameter
const job1 = queue.addToBatch(job1Options, batch);
const job2 = queue.addToBatch(job2Options, batch);

Inspiration

When AWS announced it would be discontinuing support for their Elastic Transcoder service, I created a small REST-based service to replace it. As part of this service I needed simple job queuing, with job processing offloaded so the main thread could handle further REST requests.

Existing queuing solutions like BullMQ were feature rich but overkill. I spun the queueing code I created off as a seperate npm package incase it could be used else-where.

FAQs

Package last updated on 14 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts