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

mongoose-queue

v0.3.3

Published

A simple queue using mongoose.

Downloads

104

Readme

MongooseQueue

MongooseQueue is a NodeJS package that allows managing and processing Mongoose documents as payload in a queue. Feel free to contribute and share issues you find or features you'd like to see.

Requirements

  • ES6

Usage

General

When a job is fetched from the queue by calling the get method it is blocked for a certain amount of time from being picked up by other instances. For long running jobs make sure the blockDuration in the options passed to the constructor is set accordingly.

Initialization

var MongooseQueue = require('mongoose-queue').MongooseQueue;

Class instantiation

Instantiate the class by calling the constructor:

MongooseQueue.constructor(payloadModel, workerId = '', options = {})

Parameters

  • payloadModel: The name of the Mongoose model used as payload.
  • workerId: A custom name for this instance/worker of the queue. Defaults to ''.
  • options: Additional options to configure the instance.
    • payloadRefType: The mongoose type used for the _id field of your payload schema. Defaults to ObjectId.
    • queueCollection: Name of the queues model/collection. Defaults to 'queue'.
    • blockDuration: Time in ms a job is blocked, when a worker fetched it from the queue. Defaults to 30000.
    • maxRetries: Maximum number of retries until a job is considered failed. Defaults to 5.

Example

var myOptions = {
	payloadRefType: mongoose.Types.UUID,
	queueCollection: 'queue',
	blockDuration: 30000,
	maxRetries: 5
}
var myQueue = new MongooseQueue('payload', 'my-worker-id', myOptions);

Adding a job to the queue

To add a job to the queue call the method:

MongooseQueue.add(payload, cb)

Parameters

  • payload: The Mongoose document to use as payload for this job. The model of the document has to correspond to the payload model defined upon instantiation of the class.
  • cb: fn(err, jobId) Callback called with either an error or the id of the job added to the queue.

Example

mongooseQueue.add(samplePayload, function(err, jobId)
{
	// your callback handler
});

Get a job from the queue

To get a job from the queue for processing call the method:

MongooseQueue.get(cb)

When getting a job it's retries counter is incremented and it is blocked from further get requests until it's blockedUntil expires.

Parameters

  • cb: fn(err, job) Callback called with either an error or the job dequeued for processing. The job is a simple object containing the job's id, it's payload, the date when the block expires and a flag whether the job is done.

Example

mongooseQueue.get(function(err, job)
{
	if(err)
		return done(err);
	
	console.log(job.id);
	console.log(job.payload);
	console.log(job.blockedUntil);
	console.log(job.done);
});

Mark a job as completed

To mark a job as completed/finished call the method:

MongooseQueue.ack(jobId, cb)

Parameters

  • jobId: Id of the job to mark as done/finished. Use the job id returned when getting a job for processing.
  • cb: fn(err, job) Callback called with either an error or the updated job. The job is a simple object containing the job's id, it's payload, the date when the block expires and a flag whether the job is done.

Example

mongooseQueue.ack('123123123' function(err, job)
{
	if(err)
		return done(err);
	
	console.log('The job with id ' + job.id + ' and payload ' + job.payload + ' is done.');

	// Print all info returned in job object
	console.log(job.payload);
	console.log(job.blockedUntil);
	console.log(job.done);
});

Mark a job with an error

To mark a job with an error (error message) call the method:

MongooseQueue.error(jobId, error, cb)

When called on a job the job is considered to be done with error. It will not be returned in subsequent get calls. Only the latest error message is stored in the job. Subsequent calls to this method with the same job id overwrite the previous error messages.

Parameters

  • jobId: Id of the job to mark as done/finished. Use the job id returned when getting a job for processing.
  • error: Error message to store in the job.
  • cb: fn(err, job) Callback called with either an error or the updated job. The job is a simple object containing the job's id, it's payload, the date when the block expires, an error message and a flag whether the job is done.

Example

mongooseQueue.error('12312313213123', 'This one failed horribly', function(err, job)
{
	if(err)
		return done(err);
	
	console.log('The job with id ' + job.id + ' and payload ' + job.payload + ' failed with ' + job.error);

	// Print all info returned in job object
	console.log(job.payload);
	console.log(job.blockedUntil);
	console.log(job.done);
	console.log(job.error);
});

Clean the queue

Removes all jobs from the queue that are marked done (done/error) or reached the maximum retry count.

MongooseQueue.clean(cb)

The jobs affected by clean will be deleted from the queue collection in the database!

Parameters

Callback with null when successful, otherwise the error is passed.

Example

mongooseQueue.clean(function(err)
{
	if(err)
		return done(err);

	console.log('The queue was successfully cleaned.');
});

Reset the queue

Resets the entire queue by deleting ALL jobs.

MongooseQueue.reset(cb)

The queue collection in your MongoDB will be completely empty after calling this method.

Parameters

Callback with null when successful, otherwise the error is passed.

Example

mongooseQueue.reset(function(err)
{
	if(err)
		return done(err);
	
	console.log('The queue was completely purged of all jobs.');
});

Multiple instances

This implementation should work, when launched on multiple instances using the same collection on the same MongoDB server. To help identify which worker is currently processing a job, the hostname is stored alongside the worker id you provide in the class constructor.