-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevtcmdbus.js
180 lines (151 loc) · 4.27 KB
/
evtcmdbus.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var redis = require("redis")
, domain = require("./domain/domain").domain
, conf = require('./config').conf
, logger = require("winston")
, _ = require('underscore')
// , amqpDsl = require("ampq-dsl")
, amqp = require("amqp")
;
var redisHost = conf.get("redis:host");
var redisPort = conf.get("redis:port");
var redisPass = conf.get("redis:pass");
var evtTopic="domainevent";
var cmdTopic="domaincommand";
//var cmd = redis.createClient(redisPort,redisHost);
//cmd.auth(redisPass,function(val){
// logger.info("command bus: "+val);
//
//
//});
//var evt = redis.createClient(redisPort,redisHost);
//evt.auth(redisPass,function(val){
// logger.info("event bus: "+val);
//
//});
//
//var cmdSub = redis.createClient(redisPort,redisHost);
//cmdSub.auth(redisPass,function(val){
// logger.info("event bus: "+val);
//
//});
//
//var evtSub = redis.createClient(redisPort,redisHost);
//evtSub.auth(redisPass,function(val){
// logger.info("event bus: "+val);
//
//});
//todo: don't start processing until everyhign is wired up
var eventHandlers = {};
var eventSinks=[];
function handleEvent(event) {
logger.debug("Handling Event: " + JSON.stringify(event));
var handlers = eventHandlers[event.event];
// send to sinks
_.each(eventSinks,function(sink,index,list){
sink(event);
});
if(!handlers) {
logger.warn("No handler registered for: "+event.event);
} else {
_.each(handlers, function (handler, index, list) {
logger.debug("Executing handler for: " + event.event);
handler(event);
});
}
}
//-- rabbit mq for events
var amqpConn = amqp.createConnection({
host:conf.get("evtbusamqp_host"),
port:conf.get("evtbusamqp_port"),
login:conf.get("evtbusamqp_login"),
password:conf.get("evtbusamqp_password"),
vhost:conf.get("evtbusamqp_vhost")
});
amqpConn.on('error',function(err){
logger.error("error logging into amqp: " + err);
});
amqpConn.on('ready',function(){
logger.info("amqp connection ready");
amqpConn.queue(evtTopic,
{
durable:true
},
function(queue){
logger.info("Event queue initialized: " + queue);
queue.subscribe({
ack:true
},function(message,headers,deliveryInfo){
//var event = JSON.parse(message);
handleEvent(message);
queue.shift();
});
});
});
domain.on('event',function(event){
//console.log('event: ' + JSON.stringify(evt));
//evt.publish(evtTopic,JSON.stringify(event));
logger.debug("Event Captured: " + JSON.stringify(event));
amqpConn.publish(evtTopic,event);
});
// listen to commands from redis and call each callback from subscribers
//cmdSub.on('message', function(channel, message) {
//
// var command = JSON.parse(message);
//
// if (channel === 'commands') {
//
// console.log(colors.green('\nhub -- received command ' + command.command + ' from redis:'));
// console.log(command);
//
// cmdSubscriptions.forEach(function(subscriber){
// subscriber(command);
// });
//
// }
//});
//cmdSub.subscribe(cmdTopic);
//
//
//
//
//// listen to events from redis and call each callback from subscribers
//
//evtSub.on('message', function(channel, message) {
//
// logger.debug("event: " + message);
// var event = JSON.parse(message);
//
// if (channel === evtTopic) {
//
// //console.log(colors.green('\nhub -- received event ' + event.event + ' from redis:'));
// //console.log(event);
// handleEvent(event);
//// evtSubscriptions.forEach(function(subscriber){
//// subscriber(event);
//// });
//
// }
//});
//
//evtSub.subscribe(evtTopic);
module.exports = {
emitCommand: function(command) {
logger.debug("handling command"+JSON.stringify(command));
domain.handle(command);
},
onCommand: function(command) {
},
emitEvent: function(event) {
},
onEvent: function(event) {
},
addEventSink:function(handler) { // all events go to sinks
eventSinks.push(handler);
},
addEventSubscriber:function(event,handler){
if(!eventHandlers[event]) {
eventHandlers[event]=[];
}
eventHandlers[event].push(handler);
}
}