Skip to content

Commit f58069e

Browse files
committed
nicer example for events
1 parent d825518 commit f58069e

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

‎examples/events.js‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,30 @@ var jobs = kue.createQueue();
1414

1515
function create() {
1616
var name = ['tobi', 'loki', 'jane', 'manny'][Math.random() * 4 | 0];
17-
console.log('- creating job for %s', name);
18-
jobs.create('video conversion', {
17+
var job = jobs.create('video conversion', {
1918
title: 'converting ' + name + '\'s to avi'
2019
, user: 1
2120
, frames: 200
22-
}).on('complete', function(){
21+
});
22+
23+
job.on('complete', function(){
2324
console.log("Job complete");
2425
}).on('failed', function(){
2526
console.log("Job failed");
2627
}).on('progress', function(progress){
27-
console.log(progress);
28-
}).save();
28+
process.stdout.write('\r job #' + job.id + ' ' + progress + '% complete');
29+
});
30+
31+
job.save();
32+
2933
setTimeout(create, Math.random() * 2000 | 0);
3034
}
3135

3236
create();
3337

34-
// process video conversion jobs, 3 at a time.
38+
// process video conversion jobs, 1 at a time.
3539

36-
jobs.process('video conversion', 3, function(job, done){
40+
jobs.process('video conversion', 1, function(job, done){
3741
var frames = job.data.frames;
3842

3943
function next(i) {
@@ -42,16 +46,16 @@ jobs.process('video conversion', 3, function(job, done){
4246
if (err) return done(err);
4347
// report progress, i/frames complete
4448
job.progress(i, frames);
45-
if (i == frames) done()
46-
else next(i + 4);
49+
if (i >= frames) done()
50+
else next(i + Math.random() * 10);
4751
});
4852
}
4953

5054
next(0);
5155
});
5256

5357
function convertFrame(i, fn) {
54-
setTimeout(fn, Math.random() * 100);
58+
setTimeout(fn, Math.random() * 50);
5559
}
5660

5761
// start the UI

0 commit comments

Comments
 (0)