forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisBeacon.js
67 lines (50 loc) · 1.51 KB
/
redisBeacon.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
var log = require('../core/log.js');
var util = require('../core/util');
var config = util.getConfig();
var redisBeacon = config.redisBeacon;
var watch = config.watch;
var subscriptions = require('../subscriptions');
var _ = require('lodash');
var redis = require("redis");
var Actor = function(done) {
_.bindAll(this);
this.market = [
watch.exchange,
watch.currency,
watch.asset
].join('-');
this.init(done);
}
// This actor is dynamically build based on
// what the config specifies it should emit.
//
// This way we limit overhead because Gekko
// only binds to events redis is going to
// emit.
var proto = {};
_.each(redisBeacon.broadcast, function(e) {
// grab the corresponding subscription
var subscription = _.find(subscriptions, function(s) { return s.event === e });
if(!subscription)
util.die('Gekko does not know this event:' + e);
var channel = redisBeacon.channelPrefix + subscription.event
proto[subscription.handler] = function(message, cb) {
if(!_.isFunction(cb))
cb = _.noop;
this.emit(channel, {
market: this.market,
data: message
}, cb);
};
}, this)
Actor.prototype = proto;
Actor.prototype.init = function(done) {
this.client = redis.createClient(redisBeacon.port, redisBeacon.host);
this.client.on('ready', _.once(done));
}
Actor.prototype.emit = function(channel, message) {
log.debug('Going to publish to redis channel:', channel);
var data = JSON.stringify(message);
this.client.publish(channel, data);
}
module.exports = Actor;