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

microq

v1.1.0

Published

Micro job queue built on mongo

Downloads

11

Readme

microq

Build Status Coverage Status

Microq is a simple but reliable message queue built on mongodb.

Installation

$ npm install microq

Notice microq needs async/await support. So use node.js 8.x please

Usage

Adding a job

const microq = require('microq');
const queue = microq(connectionUrl);

const job = await queue.enqueue('foo', { data: 'hello' });

Starting workers

const microq = require('microq');
const queue = microq(connectionUrl);

queue.start({
  foo(params, job) => {
    // Work on job data passed in params ({ data: 'hello' })
    // To fail, throw an error
    // To succeed return something, even undefined is accepted:)
  }
}, { interval: 500 });

Notice microq uses the debug (https://www.npmjs.com/package/debug) with prefix microq. So if you need some log output turn debug on.

Writing a worker

Microq supports workers to use async/await or to return Promises. A worker may throw an exception to fail. The queue will NOT shut down if a worker fails.

Worker function should return a promise or should be defined as async functions.

async foo(params, job) => {
  await something();

  await somthingOther();

  // ...
  return;
}

In case the worker queue is started with option parallel set to false this allows the worker queue to wait until the job finishes and to set the status of the job correctly.

API

microq(connectionUrl, [options])

Creates a new queue connected to a mongodb database specified by connectionUrl. You can optionally pass connection options to mongodb.

queue.enqueue(jobName, [params], [options])

Enqueues a job with name jobName and optional params passed to the worker.

Options

  • priority - defines the dequeue priority. jobs with higher priority are dequeued first.

Returns a promise resolving to the persisted job

queue.start(workers, options)

Starts the queue with workers passed in the workers object. A worker must be a function.

Options

  • recover (Boolean) - Defines if jobs that are in status dequeued should be recovered when starting the queue. Defaults to true.
  • interval (number in milliseconds) - Defines the poll interval. Defaults to 5000 ms.
  • parallel (Boolean) - Defines whether jobs are executed in parallel. Defaults to true.

queue.query(status)

Query the queue for jobs with the given status. Status must be one of enqueued, dequeued, completed, failed.

Returns a promise resolving to a list of jobs

queue.recover()

Updates all jobs currently in dequeue status to enqueued status and adds a date field recoveredAt

queue.cleanup([date])

Removes all jobs before a specified date (new Date() if not given) in status completed or failed.

queue.stop()

Stops polling. No workers are started after stopping the queue.

Events

The queue object returned by microq(connectionUrl) is an EventEmitter emitting events:

  • failed - A worker failed to process a job
  • completed - A woker processed a job successfully
  • empty - The job queue is currently empty