-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
34 lines (28 loc) · 913 Bytes
/
index.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
var util = require('util');
var zmq = require('zmq');
var parseJSON = require('./lib/parseJSON');
var socket = zmq.socket('pull');
var tasks = {};
module.exports = {
initialise: function (port) {
socket.connect(port);
socket.on('message', function (buffer) {
parseJSON(buffer.toString()).then(function (data) {
if (tasks[data.type]) {
util.log(util.format('Jobsworth:: ' + data.type + ': ' + '%j', data));
tasks[data.type](data);
} else {
util.log('Jobsworth:: Unregistered Task: ' + data.task);
}
}).catch(function () {
util.log('Jobsworth:: Invalid Message: ' + buffer.toString());
});
});
},
registerTask: function (name, fn) {
if (tasks[name] !== undefined) {
util.log('Jobsworth:: Already registered task: ' + name + ' - overwriting the ' + name + 'task');
}
tasks[name] = fn;
}
};