forked from Automattic/kue
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
var kue = require('../'); | ||
|
||
// 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]; | ||
console.log('- creating job for %s', name); | ||
jobs.create('video conversion', { | ||
title: 'converting ' + name + '\'s to avi' | ||
, user: 1 | ||
, frames: 200 | ||
}).save(); | ||
setTimeout(create, Math.random() * 3000 | 0); | ||
} | ||
|
||
create(); | ||
|
||
// process video conversion jobs, 3 at a time. | ||
|
||
jobs.process('video conversion', 3, 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 + 1); | ||
}); | ||
} | ||
|
||
next(0); | ||
}); | ||
|
||
function convertFrame(i, fn) { | ||
setTimeout(fn, Math.random() * 100); | ||
} | ||
|
||
// start the UI | ||
kue.app.listen(3000); |