Skip to content

Commit 643cf8f

Browse files
committed
Added Queue-level job events, useful for removing stale jobs etc. Closes Automattic#51
1 parent 5ef0b81 commit 643cf8f

4 files changed

Lines changed: 37 additions & 13 deletions

File tree

‎Readme.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ job.on('complete', function(){
116116
});
117117
```
118118

119+
### Queue Events
120+
121+
Queue-level events are currently provide access to the job-level events previously mentioned, however scoped to the `Queue` instance to to apply logic at a "global" level. An example of this is removing completed jobs:
122+
123+
```js
124+
jobs.on('job complete', function(id){
125+
Job.get(id, function(err, job){
126+
if (err) return;
127+
job.remove(function(err){
128+
if (err) throw err;
129+
console.log('removed completed job #%d', job.id);
130+
});
131+
});
132+
});
133+
```
134+
135+
The events available are the same as mentioned in "Job Events", however prefixed with "job ".
136+
119137
### Delayed Jobs
120138

121139
Delayed jobs may be scheduled to be queued for an arbitrary distance in time by invoking the `.delay(ms)` method, passing the number of milliseconds relative to _now_. This automatically flags the `Job` as "delayed".

‎lib/kue.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ exports.redis = redis;
6363
*/
6464

6565
exports.createQueue = function(){
66-
return new Queue;
66+
return Queue.singleton = new Queue;
6767
};
6868

6969
/**

‎lib/queue/events.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ exports.add = function(job){
3636
if (job.id) exports.jobs[job.id] = job;
3737
if (!exports.subscribed) {
3838
exports.subscribe();
39+
exports.queue = require('../kue').singleton;
3940
exports.subscribed = true;
4041
}
4142
};
@@ -49,7 +50,7 @@ exports.add = function(job){
4950
exports.subscribe = function(){
5051
var client = pool.pubsubClient();
5152
client.subscribe(exports.key);
52-
client.on('message', this.onMessage);
53+
client.on('message', exports.onMessage);
5354
};
5455

5556
/**
@@ -59,12 +60,21 @@ exports.subscribe = function(){
5960
*/
6061

6162
exports.onMessage = function(channel, msg){
62-
var msg = JSON.parse(msg)
63-
, job = exports.jobs[msg.id];
64-
if (!job) return;
65-
job.emit.apply(job, msg.args);
66-
// TODO: abstract this out
67-
if ('progress' != msg.event) delete exports.jobs[job.id];
63+
// TODO: only subscribe on {Queue,Job}#on()
64+
var msg = JSON.parse(msg);
65+
66+
// map to Job when in-process
67+
var job = exports.jobs[msg.id];
68+
if (job) {
69+
job.emit.apply(job, msg.args);
70+
// TODO: abstract this out
71+
if ('progress' != msg.event) delete exports.jobs[job.id];
72+
}
73+
74+
// emit args on Queues
75+
msg.args[0] = 'job ' + msg.args[0];
76+
msg.args.push(msg.id);
77+
exports.queue.emit.apply(exports.queue, msg.args);
6878
};
6979

7080
/**

‎lib/queue/job.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,7 @@ Job.prototype.save = function(fn){
521521
self.update(fn);
522522

523523
// add the job for event mapping
524-
if (self.listeners('complete').length
525-
+ self.listeners('failed').length
526-
+ self.listeners('progress').length) {
527-
events.add(self);
528-
}
524+
events.add(self);
529525
});
530526

531527
return this;

0 commit comments

Comments
 (0)