Skip to content

Commit a9fe0dc

Browse files
committed
Merge branch 'master' of https://github.com/benjamine/hubot-scripts into benjamine-master
Conflicts: src/scripts/redis-brain.coffee
2 parents 4928044 + 29cb992 commit a9fe0dc

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/scripts/redis-brain.coffee

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# None
33
#
44
# Dependencies:
5-
# "redis": "0.7.2"
5+
# "redis": "0.8.4"
66
#
77
# Configuration:
8-
# REDISTOGO_URL
8+
# REDISTOGO_URL or BOXEN_REDIS_URL
99
#
1010
# Commands:
1111
# None
@@ -16,39 +16,41 @@
1616
Url = require "url"
1717
Redis = require "redis"
1818

19-
# sets up hooks to persist the brain into redis.
2019
module.exports = (robot) ->
21-
info = Url.parse process.env.REDISTOGO_URL || process.env.BOXEN_REDIS_URL || 'redis://localhost:6379'
20+
info = Url.parse process.env.REDISTOGO_URL or process.env.BOXEN_REDIS_URL or 'redis://localhost:6379'
2221
client = Redis.createClient(info.port, info.hostname)
2322

24-
if info.auth
25-
client.auth info.auth.split(":")[1]
26-
27-
client.on "error", (err) ->
28-
robot.logger.error err
29-
30-
client.on "connect", ->
31-
robot.logger.debug "Successfully connected to Redis"
23+
robot.brain.setAutoSave false
3224

25+
getData = ->
3326
client.get "hubot:storage", (err, reply) ->
3427
if err
3528
throw err
3629
else if reply
37-
robot.logger.info "Brain data retrieved from redis-brain storage"
30+
robot.logger.info "Data for brain retrieved from Redis"
3831
robot.brain.mergeData JSON.parse(reply.toString())
3932
else
40-
robot.logger.info "Initializing new redis-brain storage"
33+
robot.logger.info "Initializing new data for brain"
4134
robot.brain.mergeData {}
4235

43-
robot.logger.info "Enabling brain auto-saving"
4436
robot.brain.setAutoSave true
4537

46-
# Prevent autosaves until connect has occured
47-
robot.logger.info "Disabling brain auto-saving"
48-
robot.brain.setAutoSave false
38+
if info.auth
39+
client.auth info.auth.split(":")[1], (err) ->
40+
if err
41+
robot.logger.error "Failed to authenticate to Redis"
42+
else
43+
robot.logger.info "Successfully authenticated to Redis"
44+
getData()
45+
46+
client.on "error", (err) ->
47+
robot.logger.error err
48+
49+
client.on "connect", ->
50+
robot.logger.debug "Successfully connected to Redis"
51+
getData() if not info.auth
4952

5053
robot.brain.on 'save', (data = {}) ->
51-
robot.logger.debug "Saving brain data"
5254
client.set 'hubot:storage', JSON.stringify data
5355

5456
robot.brain.on 'close', ->

0 commit comments

Comments
 (0)