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

job-dispatcher

v1.0.0

Published

When you don't create all jobs equally but they do similar jobs, use `job-dispatcher` to easily dispatch the tasks. This pool of workers is very useful when you have uniquely defined workers.

Downloads

4

Readme

Job Dispatcher

When you don't create all jobs equally but they do similar jobs, use job-dispatcher to easily dispatch the tasks. This pool of workers is very useful when you have uniquely defined workers.

Quick Start

Prepare the dispatcher's pool with workers

This is an example of a 2-worker pool. Each worker will return a greeting but the one will return a greeting in English and the other in Spanish. When running the process through the pool the pool will select an available pool to run. The consumer might experience this as random.

const dispatcher = require('job-dispatcher').create()

// prepare the workers
dispatcher.addWorker(async ({name, gender}) => {
    const salutation = gender === 'm' ? 'Mr' : 'Mrs'
    return `Hello, ${salutation} {name}`
})

dispatcher.addWorker(async ({name, gender}) => {
    const salutation = gender === 'm' ? 'Señor' : 'Señora'
    return `Hola, ${salutation} {name}`
})

Prepare data for this demonstration:

// prepare data
const data =[ 
    { name: 'Heisenberg', gender: 'm' },
    { name: 'Wrexler', gender: 'f' },
    { name: 'Bruin', gender: 'f' },
    { name: 'Trop', gender: 'm' },
    { name: 'Fring', gender: 'm' } 
]

No-Wait Processing

The simplest way to run the process from the pool is just invoke run. Successful invocation will go without any error thrown.

// process data from pool
data.forEach(async d => {
    try {
        // pick an available worker from the pool and run the process
        const msg = await dispatcher.run(d)
        console.log(msg) // will salute in English or Spanish (randomly)
    } catch (err) {
        // when all workers are busy, an error will be thrown
        // This is the 1st style.
        if (err.WorkerNotAvailable) console.error('All workers are busy.')
        console.error(err)
    }
})

Waiting Process

To let the pool to wait for an available worker and the execute the process, give the option wait: true and the timeout timeout: <milliseconds>. The wait option is defaulted to false obviously and timeout is defaulted to 5000 (5 seconds). Of course the timeout is ignored when wait: false. If the timeout is passed and there is still no worker is available, the WorkerNotAvailableError will be thrown.

data.forEach(async d => {
    // wait for an available worker for max 2000 msecs
    // and then run the process
    try {
        const msg = await dispatcher.run(d, { wait: true, timeout: 2000} )
        console.log(msg) // will salute in English or Spanish (randomly)
    } catch (err) {
        // 2nd style of catching running out of workers
        if (typeof err === 'WorkerNotAvailableError')
            console.error('All workers are busy.')
    }
})