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

pop-queue

v1.0.5

Published

Redis and Mongo backed light weight job queue

Downloads

2

Readme

pop-queue

Redis and Mongo backed light weight job queue

Install package via npm

npm i pop-queue

Pre-requisites:

  1. MongoDB
  2. Redis

The queue is maintained as redis array.

Concurrency is always 1. To run multiple simultaneous runners, fork with pm2. 10 sec idle time (gap between array pop) when no jobs are queued.

Lets start with creating an exporter file queue.js


import { PopQueue } from 'pop-queue';

const dbUrl = 'mongodb://localhost:27017';
const redis = {
	port: '6379',
	host: '127.0.0.1',
	password: '*******'
}
const dbName = 'pqueuedb' // default is circledb

export const popQueue = new PopQueue(
	dbUrl,
	redis,
	dbName,
	"pop_queues" // default collection name. Can be different for different runners
)

Now, lets create runner functions runner.js. Each runner pops a job by name and executes it.

import { popQueue } from './queue.js';

function runner1(job) {
	try {
		console.log("job 1 ran", job.data.name);
	} catch(err) {
		throw new Error("Error while running runner 1");
	}
}

function runner2(job) {
	try {
		console.log("job 2 ran", job.data.name);
	} catch(err) {
		throw new Error("Error while running runner 2");
	}
}

popQueue.define(
	"job1", // Runner name (unique)
	runner1
)
popQueue.define(
	"job2", // Runner name (unique)
	runner2
)

popQueue.connect().then(async (err) => {
	if(err) {
		throw err;
	}
	await popQueue.start();
})

Now we need a file to push jobs to queue scheduler.js. This is to emulate job addition is queue.

import { popQueue } from './queue.js';

setInterval(() => {
    let name = getJobName();
	popQueue.now(
		{ name }, // data
		"job1"
	)
    console.log("Pushing job1", name);
}, 10 * 1000);

setInterval(() => {
    let name = getJobName();
	popQueue.now(
		{ name }, // data
		"job2"
	)
    console.log("Pushing job2", name);
}, 8 * 1000);


function getJobName() {
    return "job_" + parseInt(Math.random() * 100)
}

Finally lets create an index.js file.

import * as runner from "./runner.js";
import * as scheduler from "./scheduler.js";

Run the index file to start pop-queue

node index.js