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

redis-locking-worker

v0.1.0

Published

Event based implementation of global worker locks using Redis

Downloads

4

Readme

redis-locking-worker

Overview

An event driven implementation of global locks using Redis.

Install

npm install redis-locking-worker

Usage

var RedisLockingWorker = require("redis-locking-worker");

var worker = new RedisLockingWorker({
	"lockKey" : "mylock",
	"statusLevel" : RedisLockingWorker.StatusLevels.Verbose,
	"lockTimeout" : 5000,
	"maxAttempts" : 5
});

worker.on("acquired", function(lastAttempt) {
	if (Math.random() <= SUCCESS_CHANCE) {
		console.log("Completed work successfully, cleaning up!");
		lock.done(lastAttempt);
	} else {
		// oh no, we failed to do work!
		console.log("Failed to do work, hopefully someone else will have better luck!");
	}	
});

worker.on("locked", function() {
	console.log("Someone else acquired the lock");
});

worker.on("error", function(error) {
	console.error("Error from lock: %j", error);
});

worker.on("status", function(message) {
	console.log("Status message from lock: %s", message);
});

worker.acquire();

Options

You can specify a variety of options when creating a new lock instance:

{
	"client" : null,										/* Instance of a node-redis redis client (https://github.com/mranney/node_redis) */
	"host" : "localhost",									/* Redis host to connect to if a client wasn't explicitly passed in */
	"port" : 6379,											/* Redis port to connect to if a client wasn't explicitly passed in */
	"lockKey" : null,										/* Name of the key to use for this lock, defaults to null, not optional */
	"statusLevel" : RedisLockingWorker.StatusLevel.Normal,	/* Verbosity to use when emitting status events */
	"lockTimeout" : 5000,									/* Time, in milliseconds, before a lock should expire. */
	"maxAttempts" : 5,										/* Number of attempts to complete work before giving up */
}

Example

You can run:

node examples/cluster.js

For an example implementation that uses cluster to fork one worker per CPU core, each one with a 15% chance of completing the "work" successfully. You should see output along these lines:

Worker 1 failed to do work
Worker 2 did not acquire lock
Worker 3 did not acquire lock
Worker 5 did not acquire lock
Worker 4 did not acquire lock
Worker 6 did not acquire lock
Worker 7 did not acquire lock
Worker 8 did not acquire lock
Worker 2 Status message from lock: Checking status of work from attempt #1
Worker 2 Status message from lock: Work was not completed, trying to reacquire lock for attempt #1
Worker 2 Status message from lock: Trying to reacquire lock
Worker 2 Status message from lock: Current attempt count: 1
Worker 2 Status message from lock: This is attempt #2
Worker 2 failed to do work
Worker 3 Status message from lock: Checking status of work from attempt #1
Worker 3 Status message from lock: Attempt count has been incremented (expected 1, but it's 2), someone else got the lock
Worker 5 Status message from lock: Checking status of work from attempt #1
Worker 5 Status message from lock: Attempt count has been incremented (expected 1, but it's 2), someone else got the lock
Worker 4 Status message from lock: Checking status of work from attempt #1
Worker 4 Status message from lock: Attempt count has been incremented (expected 1, but it's 2), someone else got the lock
Worker 6 Status message from lock: Checking status of work from attempt #1
Worker 6 Status message from lock: Attempt count has been incremented (expected 1, but it's 2), someone else got the lock
Worker 7 Status message from lock: Checking status of work from attempt #1
Worker 7 Status message from lock: Attempt count has been incremented (expected 1, but it's 2), someone else got the lock
Worker 8 Status message from lock: Checking status of work from attempt #1
Worker 8 Status message from lock: Attempt count has been incremented (expected 1, but it's 2), someone else got the lock
Worker 3 Status message from lock: Checking status of work from attempt #2
Worker 3 Status message from lock: Work was not completed, trying to reacquire lock for attempt #2
Worker 3 Status message from lock: Trying to reacquire lock
Worker 3 Status message from lock: Current attempt count: 2
Worker 3 Status message from lock: This is attempt #3
Worker 3 failed to do work
Worker 5 Status message from lock: Checking status of work from attempt #2
Worker 5 Status message from lock: Attempt count has been incremented (expected 2, but it's 3), someone else got the lock
Worker 4 Status message from lock: Checking status of work from attempt #2
Worker 4 Status message from lock: Attempt count has been incremented (expected 2, but it's 3), someone else got the lock
Worker 6 Status message from lock: Checking status of work from attempt #2
Worker 6 Status message from lock: Attempt count has been incremented (expected 2, but it's 3), someone else got the lock
Worker 7 Status message from lock: Checking status of work from attempt #2
Worker 7 Status message from lock: Attempt count has been incremented (expected 2, but it's 3), someone else got the lock
Worker 8 Status message from lock: Checking status of work from attempt #2
Worker 8 Status message from lock: Attempt count has been incremented (expected 2, but it's 3), someone else got the lock
Worker 5 Status message from lock: Checking status of work from attempt #3
Worker 5 Status message from lock: Work was not completed, trying to reacquire lock for attempt #3
Worker 5 Status message from lock: Trying to reacquire lock
Worker 5 Status message from lock: Current attempt count: 3
Worker 5 Status message from lock: This is attempt #4
Worker 5 completed work successfully, last attempt? false
Worker 4 Status message from lock: Checking status of work from attempt #3
Worker 4 Status message from lock: Work completed successfully by primary process, deleting lock
Worker 6 Status message from lock: Checking status of work from attempt #3
Worker 6 Status message from lock: Lock key is gone, someone else completed the work and deleted the lock
Worker 7 Status message from lock: Checking status of work from attempt #3
Worker 7 Status message from lock: Lock key is gone, someone else completed the work and deleted the lock
Worker 8 Status message from lock: Checking status of work from attempt #3
Worker 8 Status message from lock: Lock key is gone, someone else completed the work and deleted the lock