Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed auto event subscription. Closes #68

  • Loading branch information...
commit e8b00e502410ca6bc5b960e8f2fb6d93c2df2c8a 1 parent f10286e
@tj tj authored
Showing with 26 additions and 109 deletions.
  1. +12 −0 lib/kue.js
  2. +3 −5 lib/queue/events.js
  3. +11 −104 test.js
View
12 lib/kue.js
@@ -97,6 +97,18 @@ Queue.prototype.createJob = function(type, data){
};
/**
+ * Proxy to auto-subscribe to events.
+ *
+ * @api public
+ */
+
+var on = EventEmitter.prototype.on;
+Queue.prototype.on = function(event){
+ if (0 == event.indexOf('job')) events.subscribe();
+ return on.apply(this, arguments);
+};
+
+/**
* Promote delayed jobs, checking every `ms`,
* defaulting to 5 seconds.
*
View
8 lib/queue/events.js
@@ -34,11 +34,7 @@ exports.key = 'q:events';
exports.add = function(job){
if (job.id) exports.jobs[job.id] = job;
- if (!exports.subscribed) {
- exports.subscribe();
- exports.queue = require('../kue').singleton;
- exports.subscribed = true;
- }
+ if (!exports.subscribed) exports.subscribe();
};
/**
@@ -51,6 +47,8 @@ exports.subscribe = function(){
var client = pool.pubsubClient();
client.subscribe(exports.key);
client.on('message', exports.onMessage);
+ exports.queue = require('../kue').singleton;
+ exports.subscribed = true;
};
/**
View
115 test.js
@@ -1,108 +1,15 @@
-var kue = require('./');
+var kue = require('./')
+ , Job = kue.Job;
- var jobs = kue.createQueue();
-
- jobs.process('ping', 100, function(job, done){
- done();
- })
-
- // create packet
- var length = 64;
- var packet = '', n = 0;
- for (var i = 0; i < length; i++) packet += 'a';
-
- // start ping
- setInterval(function(){
-
- // closure on 'n'
- -function(n) {
-
- var start = new Date;
- var job = jobs.create('ping', packet).save();
-
- job.on('complete', function(){
- var ms = new Date - start;
- console.log('%d bytes from kue: icmp_seq=%d time=%d ms', packet.length, n, ms);
- });
-
- }(n++)
-
-
- }, 100)
-
-// cluster = cluster()
-// .use(cluster.debug())
-// .set('workers', 2)
-// .start();
-//
-// if (cluster.isMaster) {
-// // start the UI
-// kue.app.listen(3000);
-// console.log('UI started on port 3000');
-// } else {
-//
-// // create our job queue
-//
-// var jobs = kue.createQueue();
-//
-// // start redis with $ redis-server
-//
-// // create some jobs at random,
-// // usually you would create these
-// // in your http processes upon
-// // user input etc.
-//
-// function create() {
-// var name = ['tobi', 'loki', 'jane', 'manny'][Math.random() * 4 | 0];
-// var job = jobs.create('video conversion', {
-// title: 'converting ' + name + '\'s to avi'
-// , user: 1
-// , frames: 200
-// });
-//
-// job.on('complete', function(){
-// console.log(" Job complete");
-// }).on('failed', function(){
-// console.log(" Job failed");
-// }).on('progress', function(progress){
-// process.stdout.write('\r job #' + job.id + ' ' + progress + '% complete');
-// });
-//
-// job.save();
-//
-// setTimeout(create, Math.random() * 200 | 0);
-// }
-//
-// create();
-//
-// // process video conversion jobs, 1 at a time.
-//
-// jobs.process('video conversion', 1, function(job, done){
-// var frames = job.data.frames;
-//
-// function next(i) {
-// // pretend we are doing some work
-// convertFrame(i, function(err){
-// if (err) return done(err);
-// // report progress, i/frames complete
-// job.progress(i, frames);
-// if (i >= frames) done()
-// else next(i + Math.random() * 10);
-// });
-// }
-//
-// next(0);
-// });
-//
-// function convertFrame(i, fn) {
-// if (Math.random() > .99) {
-// fn(new Error('fail'));
-// fn();
-// } else {
-// setTimeout(fn, Math.random() * 50);
-// }
-// }
-// }
+// create our job queue
+var jobs = kue.createQueue();
+jobs.on('job complete', function(id){
+ console.log('got %d', id);
+ Job.get(id, function(err, job){
+ if (err) return;
+ console.log(job.data);
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.