3

From the kue docs,creating a queue and adding job is is easy but i cannot follow how the job is being stored

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

adding a job

jobs.create('email', {
    title: 'welcome email for tj'
  , to: 'tj@learnboost.com'
  , template: 'welcome-email'
}).priority('high').save();

The example is easy to understand but,what if i needed more options for instance in the example,like adding an advert option - , ad: 'we are the best'

jobs.create('email', {
    title: 'welcome email for tj'
  , to: 'tj@learnboost.com'
  , ad: 'we are the best'
  , template: 'welcome-email'
}).priority('high').save();

how am i going to go about it?.

Gandalf
  • 1
  • 29
  • 94
  • 165

1 Answers1

3

The second arg to jobs.create is an object that will be accessible in the job processor. You can put whatever fields you want in there. Then once you setup your processor you can use the "ad" field.

Adding to your example:

jobs.process('email', function (job, done) {
    var advertOption = job.data.ad;

    // Do your emailing stuff, like rendering template and sending...

});

You can specify the number of workers you want if you give three args:

jobs.process('email', 1, function (job, done) { // samesame

The associated source is easy to read through and well commented

njwags
  • 1,107
  • 11
  • 22
  • I think the second argument to jobs.process is not the number of workers but the number of jobs each worker should take off the queue (and process synchronously). Depends on your definition of worker though, I see they've used it in the source. – DanS Oct 28 '13 at 13:29
  • @DanS By worker I meant, the lib's [Worker class](https://github.com/LearnBoost/kue/blob/master/lib/queue/worker.js). The second arg is the number of "new Worker()"s that are going to be instantiated. As for async vs. sync processing, workers will take one job at a time, but as with everything in node they are processed asynchronously. So if you've got a job that does high latency networking you may want to setup more workers than the number of cores running on your machine. For example: emailing through SES – njwags Oct 28 '13 at 17:30
  • Sorry jwags, you're right. I was confusing it with the cluster (multi-process) approach but both are async. – DanS Oct 28 '13 at 18:28