-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
55 lines (43 loc) · 1.34 KB
/
worker.js
File metadata and controls
55 lines (43 loc) · 1.34 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
"use strict";
var nconf = require("nconf").env();
var kue = require('kue');
// Create Kue connection
var jobs = kue.createQueue({
prefix: nconf.get("KUE_PREFIX"),
redis: {
port: nconf.get("REDIS_PORT_6379_TCP_PORT"),
host: nconf.get("REDIS_PORT_6379_TCP_ADDR"),
db: nconf.get("REDIS_DATABASE")
}
});
console.log("Connected to Kue. Waiting for job...");
jobs.on('job enqueue', function(id,type){
console.log( 'Queue: Job %s of %s got queud', id, type );
jobs.process(type, function(job, done){
console.log( 'Job: Job %s of %s is executed', job.id, job.type );
jobHasard(job.data, done);
});
});
function jobHasard(data, callback){
function hasardBoolean(){
return Math.random()<.5; // Readable, succint
}
var tempsAttente = Math.random() * 10000;
console.log("Job hasard ! hou hou ! La roue tourne... ("+tempsAttente+"ms)");
setTimeout(function(callback){
if ( hasardBoolean() ) {
console.log("Booh! looser!");
return callback(new Error());
} else {
console.log("Yeah you won !");
return callback(null, "OK");
};
}, 2000, callback);
};
kue.app.listen(3000);
process.once( 'SIGTERM', function ( sig ) {
queue.shutdown(function(err) {
console.log( 'Kue is shut down.', err||'' );
process.exit( 0 );
}, 5000 );
});