-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
33 lines (28 loc) · 945 Bytes
/
worker.js
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
// settings
var settings = require('./settings');
// socket.io emitter
var io = require('socket.io-emitter')( settings.redis );
// redis message queue
var kue = require('kue');
kue.app.listen(6666);
var jobs = kue.createQueue({
redis: settings.redis
});
// on new_user job
jobs.process('new_user', function(job, done){
// insert artificial delay
setTimeout(function() {
console.log('process new user ' + job.data.id);
// emit web socket call
io.to(job.data.id) // respond only to affected client (don't broadcast)
.emit('worker-hello', { message: 'worker: hello user ' + job.data.id });
done();
}, 2000);
});
// on challenge job
jobs.process('challenge', function(job, done){
console.log('process new challenge from ' + job.data.id);
io.to(job.data.id) // respond only to affected client (don't broadcast)
.emit('worker-update', { message: Math.pow(parseFloat(job.data.challenge), 2) });
done();
});