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

bee-queue-dynamic

v0.1.0

Published

This project helps distribute jobs evenly across multiple queues.

Downloads

25

Readme

bee-queue-dynamic

This project helps distribute jobs evenly across multiple queues.

It uses object-hash to compute hash of the job's payload and then transforms it into a number. Then this number is transformed into a token by applying module operation (token = hashVal % maxToken). Tokens then used to decide on which queue to post the job (queueIndex = token % amountOfQueues).

Why?

Bee Queue is amazing module. As for august 2018 it doesn't support redis cluster. So scaling queues on multiple servers requires synchronised redis instance. This is not as fast as using redis locally. You can set up multiple instances of your app to use different redis servers (available locally) and use this module to distribute jobs between them.

Usage

new Dynamic([options])

Create an instance of dynamic bee-queue wrapper

  • options.algorithm: algorithm to use for hashing. Defaults to sha512
  • options.strategy: strategy to use for job distribution. Can be token or random. Defaults to token

dynamic.registerQueue(queue)

Register queue to use with wrapper

dynamic.unregisterQueue(queue)

Unregister previously registered queue. This is useful in case if redis connection dies. The wrapper will continue to work with queues that have left

  • queue: instance of bee-queue. should be the same object that was used in registerQueue

dynamic.createJob(data)

Create new job on one of registered queues. This will throw an error if no queues are present.

Example

import Queue from 'bee-queue'
import Dynamic from 'bee-queue-dynamic'

const redisServers = [
  {
    host: '0.0.0.1',
    port: 6379,
    db: 0,
    options: {},
  },
  {
    host: '0.0.0.2',
    port: 6379,
    db: 0,
    options: {},
  },
  // ...
]

const dynamic = new Dynamic()

for (const redis of redisServers) {
  const queue = new Queue('test', { redis })

  dynamic.registerQueue(queue)
}

void (async function main() {
  // job will be created on one of the queues
  const job = await dynamic.createJob({ test: 'data' }).save()

  console.log(job.queue.settings.redis.host) // outputs one of: '0.0.0.1', '0.0.0.2'
})()

FAQ

Q: How much exactly even the distribution is?

A: It depends on how different your job payloads are. This project has not been tested in any production environment yet. I will update this QA once I do some actual tests.

Here is test results for distributing 100 000 fully random jobs across 17 queues:

jobsPerQueue { 'queue-0': 5944,
  'queue-1': 5892,
  'queue-2': 5826,
  'queue-3': 5878,
  'queue-4': 5785,
  'queue-5': 5895,
  'queue-6': 5870,
  'queue-7': 5861,
  'queue-8': 5881,
  'queue-9': 5914,
  'queue-10': 5788,
  'queue-11': 5997,
  'queue-12': 5867,
  'queue-13': 5858,
  'queue-14': 6003,
  'queue-15': 5827,
  'queue-16': 5914 }