forked from ldejager/hubot-consul-brain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsul-brain.js
33 lines (27 loc) · 892 Bytes
/
consul-brain.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
var consul = require("consul")({
host: process.env.CONSUL_HOST || "127.0.0.1",
port: process.env.CONSUL_PORT || "8500",
defaults: {
token: process.env.CONSUL_TOKEN || ""
}
});
var prefix = "hubot/hubot:storage";
module.exports = function(robot) {
robot.brain.setAutoSave(false);
consul.kv.get(prefix, function(err, res) {
if (err) throw err;
if(res != undefined) {
robot.logger.info("hubot-consul-brain: Data for " + prefix + " brain retrieved from Consul")
robot.brain.mergeData(JSON.parse(res["Value"]));
} else {
robot.logger.info("hubot-consul-brain: Initializing new data for " + prefix + " brain")
robot.brain.mergeData({});
}
robot.brain.setAutoSave(true);
});
robot.brain.on('save', function(data) {
consul.kv.set(prefix, JSON.stringify(data), function(err, result) {
if (err) throw err;
});
});
}