forked from Hylozoic/hylo-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
62 lines (51 loc) · 1.58 KB
/
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
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
var skiff = require('./lib/skiff'); // this must be first
require('./config/kue'); // this must be second
var _ = require('lodash'),
colors = require('colors'),
Promise = require('bluebird'),
queue = require('kue').createQueue(),
rollbar = skiff.rollbar,
sails = skiff.sails,
util = require('util');
// define new jobs here.
// each job should return a promise.
// use Promise.method if the job is synchronous.
//
// TODO these job definitions should go elsewhere.
// the common case of queueing a class method could also be handled with
// a single job.
var jobDefinitions = {
'test': Promise.method(function(job) {
console.log(new Date().toString().magenta);
throw new Error('whoops!');
}),
'classMethod': function(job) {
sails.log.debug(format('Job %s: %s.%s', job.id, job.data.className, job.data.methodName));
return global[job.data.className][job.data.methodName](_.omit(job.data, 'className', 'methodName'));
}
};
var processJobs = function() {
// load jobs
_.forIn(jobDefinitions, function(promise, name) {
queue.process(name, 10, function(job, ctx, done) {
// put common behavior for all jobs here
var label = util.format('Job %s: ', job.id);
sails.log.debug(label + name);
promise(job).then(function() {
sails.log.debug(label + 'done');
done();
})
.catch(function(err) {
sails.log.error(label + err.message.red);
rollbar.handleError(err);
done(err);
});
});
});
};
skiff.lift({
start: processJobs,
stop: function(done) {
queue.shutdown(5000, done);
}
});