|
2 | 2 | # None |
3 | 3 | # |
4 | 4 | # Dependencies: |
5 | | -# "redis": "0.7.2" |
| 5 | +# "redis": "0.8.4" |
6 | 6 | # |
7 | 7 | # Configuration: |
8 | | -# REDISTOGO_URL |
| 8 | +# REDISTOGO_URL or BOXEN_REDIS_URL |
9 | 9 | # |
10 | 10 | # Commands: |
11 | 11 | # None |
|
16 | 16 | Url = require "url" |
17 | 17 | Redis = require "redis" |
18 | 18 |
|
19 | | -# sets up hooks to persist the brain into redis. |
20 | 19 | 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' |
22 | 21 | client = Redis.createClient(info.port, info.hostname) |
23 | 22 |
|
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 |
32 | 24 |
|
| 25 | + getData = -> |
33 | 26 | client.get "hubot:storage", (err, reply) -> |
34 | 27 | if err |
35 | 28 | throw err |
36 | 29 | else if reply |
37 | | - robot.logger.info "Brain data retrieved from redis-brain storage" |
| 30 | + robot.logger.info "Data for brain retrieved from Redis" |
38 | 31 | robot.brain.mergeData JSON.parse(reply.toString()) |
39 | 32 | else |
40 | | - robot.logger.info "Initializing new redis-brain storage" |
| 33 | + robot.logger.info "Initializing new data for brain" |
41 | 34 | robot.brain.mergeData {} |
42 | 35 |
|
43 | | - robot.logger.info "Enabling brain auto-saving" |
44 | 36 | robot.brain.setAutoSave true |
45 | 37 |
|
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 |
49 | 52 |
|
50 | 53 | robot.brain.on 'save', (data = {}) -> |
51 | | - robot.logger.debug "Saving brain data" |
52 | 54 | client.set 'hubot:storage', JSON.stringify data |
53 | 55 |
|
54 | 56 | robot.brain.on 'close', -> |
|
0 commit comments