-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.js
55 lines (44 loc) · 1.48 KB
/
wrapper.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
var utils = require('./utils/common.js'),
prefix = require('./utils/prefix.js'),
confMod = require('./config.js'),
Utils = require('./lib/controllers/utils/utils.js')
config = confMod.config(),
redis = require('redis').createClient(),
zmq = require('zmq'),
EventEmitter = require('events').EventEmitter;
if(process.argv.length < 5){
throw 'Illegal Argument: ' + arguments.length + ' arguments found, 4 required.';
}
var serviceId = process.argv[2],
brokerAddress = process.argv[4], //'ipc:///tmp/55001';
dealerIdentity = process.argv[3];
nodeId = dealerIdentity.substring(0,3);
var obj,
dealer;
var replyQueue = {},
brokers = [],
services = [];
console.log(process.argv)
confMod.getServiceInfo(serviceId, nodeId, function(err, res){
obj = require(res.worker.config.file);
dealer = zmq.socket('dealer');
dealer.identity = dealerIdentity;
dealer.connect(brokerAddress);
console.log('Dealer: ' + dealer.identity + ', connected to:' + brokerAddress)
dealer.send(['READY']);
var self = this;
dealer.on('message', function(){
var arr = Array.apply(null, arguments);
var msg = utils.parse(arr[arr.length - 1].toString());
console.log('* ', msg)
if(typeof(obj[msg.fn]) == 'function'){
var args = msg.args;
args.push(function(err, res){
arr[arr.length - 1] = utils.stringify({reqId: msg.reqId, err: err, res: res == null ? null : Utils.toJson(res)});
dealer.send(arr);
//console.log(err, Utils.toJson(res))
});
obj[msg.fn].apply(obj, args);
}
});
})