Giter Site home page Giter Site logo

kue_example's Introduction

Exmaple of using kue with node's cluster

Quick modification of the cluster example code in the README for kue

Run

run the kue monitor server:

 node kue_server.js

run the process:

 node test_cluster.js

This will also spew jobs to the console.

Open up a browser to http://localhost:3001

If you tweak the processing times in test_cluster.js, you can make the jobs stick around long enough to see them. On my computer they barely show up.

Note

I separated the server monitor from the job creation/processing code because it was confusing to me on first glance what was going on.

The difference with kue's README

The primary difference with the stock kue readme is that I'm just following the node.js cluster documentation found here

Quite literally, I just cut and paste the code, then made it work.

So:

var kue = require('kue')
  , cluster = require('cluster')
  , jobs = kue.createQueue();

var numCPUs = require('os').cpus().length;


if (cluster.isMaster) {
    var redis = kue.redis.createClient()

    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
    function create2() {
        var name = ['tobi', 'loki', 'jane', 'manny'][Math.random() * 4 | 0];
        var job = jobs.create('email', {
            title: 'emailing ' + name + '', body: 'hello'
        }).save();

        setTimeout(create2, Math.random() * 1000 | 0);
    }

    create2();
    jobs.on('job complete', function(id){
        kue.Job.get(id, function(err, job){
            if (err) return;
            job.remove(function(err){
                if (err) throw err;
                console.log('removed completed job #%d', job.id);
            });
        });
    });

} else {

  // not cluster.isMaster, but rather a spawned job

  jobs.process('email', function(job, done){
    var pending = 5
      , total = pending;

    var interval = setInterval(function(){
      job.log('sending!');
      job.progress(total - pending, total);
      --pending || done();
      pending || clearInterval(interval);
    }, 10);
  });
}

kue_example's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.