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

kqueue

v1.1.4

Published

job queue and job runner

Downloads

8

Readme

kqueue

Kinvey Hackathon, 2014-10-09 - Andras.

Job queue and job runner built on top of beanstalkd. Working prototype. Three days, 1000 lines of code, 95% functionality.

Summary

Kqueue is a reliable real-time job scheduling and execution engine that guarantees delivery and eventual job completion. Queued jobs will remain queued until successfully processed by their respective handlers.

A job is a payload (message) sent to a specific handler (recipient). Messages are matched to message handlers by the jobtype string (channel). Messages added to the job queue remain until explicitly removed. (See the beanstalk job lifecycle description.)

Install

    npm install git://github.com/Kinvey/kqueue

Features

  • real-time (scheduling/routing/execution overhead under .5 ms)
  • guaranteed execution
    • "run at least once": explicity delete required, else will retry
    • "run eventually": handlers do not have to be currently active
    • "queue and forget": waiting jobs are saved to durable store (see notes about race condition and SPOF in TODO below)
  • postdated execution, can be set for later delivery
  • job priorities, more urgent jobs first
  • unlimited number of job types (channels)
  • per-jobtype job handlers (listeners)
  • per-jobtype suspend/resume of message delivery

Example

    var KQueue = require('kqueue').KQueue;
    var queue = new KQueue({
        host: 'localhost',
        port: 11300
    });
    queue.open(function(err) {
        for (var i=1; i<=10; i++) {
            queue.addJob('jobtype1', 'myPayload' + i, function(err, jobid) {
                // added job
            });
        }

        queue.addHandler(
            'jobtype1',
            function handleJob( job, cb ) {
                // process myPayload = job.data
                // ...
                queue.deleteJob(job, function(err) {
                    // job finished and removed
                    cb(err);
                });
            },
            function(err) {
                // registered job handler
            }
        );

        queue.runJobs({countLimit: 100, timeLimitMs: 200}, function(err, count) {
            // processed count jobs
            // once inserts have finished, finish up
            queue.close();
        });
    });

Calls

KQueue( options )

Options:

    host: '0.0.0.0'   // beanstalkd daemon server
    port: 11300       // beanstalkd daemon port
    bulkStore: null   // bulk key-value store for job data with get/set/delete methods

open( callback )

close( callback )

addJob( jobtype, myPayload, options, function(err, jobid ))

Options:

    priority: KQueue.PRIO_NORMAL
                    // PRIO_URGENT, PRIO_HIGH, PRIO_NORMAL, PRIO_LOW, PRIO_BULK
    delay: 0        // seconds before eligible to run
    ttr: 30         // once reserved, must finish in 30 seconds else will be re-queued

addHandler( jobtype, handlerFunc(jobObject, cb), callback )

Register the handler function to process jobs of type jobtype. The queue starts listening for and running jobs of jobtype. The callback is called once the listener has been installed.

The handler is a function taking two arguments, the job object and a callback. The job arguments are in job.data. The callback must be called when the handler is done, else the computation will block.

    var myPayload = jobObject.data

removeHandler( jobtype, callback )

renewJob( jobObject, callback )

deleteJob( jobObject, callback )

ungetJob( jobObject, newPriority, afterDelaySeconds, callback )

retryJob( jobObject, reasonMessage, callback )

runJobs( options, function(err, countRan) )

Options:

    timeLimitMs     0: do not wait, >0: wait ms, -1: unlimited
    countLimit      >0: at most count, <0: unlimited

purgeJobs( jobtype, [matchingRegExp,] cb )

Delete all queued jobs of type jobtype whose serialized form matches the given regular expression (by default all jobs match). Returns the count of jobs deleted. This is an administrative command for advanced users only. Do not call this command on servers that are listening for jobs.


Todo

  • unit tests
  • log more info (there is logging support built in)
  • analyze and address points of failure (and SPOF)
  • add calls for introspection, queue stats, job stats
  • minor refactoring to smooth out the interfaces
  • clean up, remove remnants of scaffolding
  • try bonded mode, single interface to multiple connections
  • try bonded mode: single interface to multiple beanstalkd servers
  • maybe allow multiple job handlers (listeners) for pub/sub like message multicast

Lessons

  • nodejs modules have weird avoidable quirks (async recursion depth, fivebeans order of operations)
  • some information not visible until late into the project, eg beanstalkd job delete rate, max payload size
  • sometimes a hackathon has a mini-hackathon lurking inside: qbean! (batching beanstalkd driver)
  • clustered mode triples throughput (4-core system)