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

@anephenix/job-queue

v1.3.1

Published

Job Queue

Downloads

13

Readme

Job Queue

npm version CircleCI Coverage Status Maintainability

A Node.js Job Queue library using Redis.

Features

  • Create job queues
  • Create workers to process jobs on those queues
  • Store the queues and jobs in Redis for data persistence
  • Use hooks to trigger actions during the job lifecycle

Dependencies

Install

npm i @anephenix/job-queue

Usage

You will need a create a Redis client that is promisified with bluebird.

// Dependencies
const bluebird = require('bluebird');
const redisLib = require('redis');
bluebird.promisifyAll(redisLib.RedisClient.prototype);
bluebird.promisifyAll(redisLib.Multi.prototype);
const redisConfig = {};
const redis = redisLib.createClient(redisConfig);

Once you have that, you can create a queue like this:

const { Queue } = require('@anephenix/job-queue');

const emailQueue = new Queue({ queueKey: 'email', redis });

Adding jobs

Once you have the queue ready, you can add jobs like this:

const job = {
	name: 'job-001',
	data: {
		from: '[email protected]',
		to: '[email protected]',
		subject: 'Have you got the document for ML results?',
		body: 'I want to check what the loss rate was. Thanks.',
	},
};

emailQueue.add(job);

Setting up workers to process those jobs

Workers can be setup like this:

const { Worker } = require('@anephenix/job-queue');
const sendEmail = require('./sendEmail');

class EmailWorker extends Worker {
	async processJob(job) {
		this.status = 'processing';
		try {
			await sendEmail(job);
			await this.completeJob(job);
		} catch (err) {
			await this.failJob(job);
		}
		return;
	}
}

const emailWorker = new EmailWorker(emailQueue);

Workers are the base class on which to create Workers tailored to processing the job. In the example above, we have an EmailWorker whose processJob function is customised to send an email via the 'sendEmail' function. The worker is now setup to start processing jobs.

Starting the worker

await emailWorker.start();

The worker will now poll the queue for available jobs. Once it has one, it will take the job and process it.

Stopping the worker

await emailWorker.stop();

Advanced Features

Using hooks in the Queue

Hooks are a way to trigger functions before and after these actions are called on the queue:

  • add
  • take
  • complete
  • fail

This gives you the ability to do things like collect data on how many jobs are being added to a queue, how quickly they are being processed, and so on.

There are 2 types of hook, pre and post. A pre hook is called before the action is triggered, and a post hook is called after.

The way to setup hooks to call can be demonstrated in the example below:

const queueKey = 'email';
const queue = new Queue({
	queueKey,
	redis,
	hooks: {
		add: {
			pre: async job => {
				// Do something with the job before it is added
				return job;
			},
			post: async job => {
				// Do something with the job after it is added
				return job;
			},
		},
		take: {},
		complete: {},
		fail: {},
	},
});

License and Credits

©2021 Anephenix OÜ. Job Queue is licensed under the MIT license.