forked from Automattic/kue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstale.js
More file actions
executable file
·65 lines (53 loc) · 1.58 KB
/
Copy pathstale.js
File metadata and controls
executable file
·65 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var kue = require('../')
, express = require('express');
// create our job queue
var jobs = kue.createQueue()
, Job = kue.Job;
// 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;
console.log("job process %d", job.id);
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 + 5);
});
}
next(0);
});
function convertFrame(i, fn) {
setTimeout(fn, Math.random() * 100);
}
// remove stale jobs
jobs.on('job complete', function (id) {
Job.get(id, function (err, job) {
if (err) return;
job.remove(function (err) {
if (err) throw err;
console.log('removed completed job #%d', job.id);
});
});
});
// start the UI
var app = express.createServer();
app.use(kue.app);
app.listen(3000);
console.log('UI started on port 3000');