forked from hubotio/hubot-redis-brain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis-brain.coffee
84 lines (70 loc) · 3 KB
/
redis-brain.coffee
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
# Description:
# Persist hubot's brain to redis
#
# Configuration:
# REDISTOGO_URL or REDISCLOUD_URL or BOXEN_REDIS_URL or REDIS_URL or REDIS_PORT_6379_TCP.
# URL format: redis://<host>:<port>[/<brain_prefix>]
# If not provided, '<brain_prefix>' will default to 'hubot'.
#
# Commands:
# None
Url = require "url"
Redis = require "redis"
module.exports = (robot) ->
redisUrl = if process.env.REDISTOGO_URL?
redisUrlEnv = "REDISTOGO_URL"
process.env.REDISTOGO_URL
else if process.env.REDISCLOUD_URL?
redisUrlEnv = "REDISCLOUD_URL"
process.env.REDISCLOUD_URL
else if process.env.BOXEN_REDIS_URL?
redisUrlEnv = "BOXEN_REDIS_URL"
process.env.BOXEN_REDIS_URL
else if process.env.REDIS_URL?
redisUrlEnv = "REDIS_URL"
process.env.REDIS_URL
else if process.env.REDIS_PORT_6379_TCP_ADDR? and process.env.REDIS_PORT_6379_TCP_PORT?
redisUrlEnv = "REDIS_PORT_6379_TCP"
"redis://#{process.env.REDIS_PORT_6379_TCP_ADDR}:#{process.env.REDIS_PORT_6379_TCP_PORT}"
else if process.env.REDIS_1_PORT_6379_TCP_ADDR? and process.env.REDIS_1_PORT_6379_TCP_PORT?
redisUrlEnv = "REDIS_PORT_6379_TCP"
"redis://#{process.env.REDIS_1_PORT_6379_TCP_ADDR}:#{process.env.REDIS_1_PORT_6379_TCP_PORT}"
else
'redis://localhost:6379'
if redisUrlEnv?
robot.logger.info "hubot-redis-brain: Discovered redis from #{redisUrlEnv} environment variable"
else
robot.logger.info "hubot-redis-brain: Using default redis on localhost:6379"
info = Url.parse redisUrl, true
client = if info.auth then Redis.createClient(info.port, info.hostname, {no_ready_check: true}) else Redis.createClient(info.port, info.hostname)
prefix = info.path?.replace('/', '') or 'hubot'
robot.brain.setAutoSave false
getData = ->
client.get "#{prefix}:storage", (err, reply) ->
if err
throw err
else if reply
robot.logger.info "hubot-redis-brain: Data for #{prefix} brain retrieved from Redis"
robot.brain.mergeData JSON.parse(reply.toString())
else
robot.logger.info "hubot-redis-brain: Initializing new data for #{prefix} brain"
robot.brain.mergeData {}
robot.brain.setAutoSave true
if info.auth
client.auth info.auth.split(":")[1], (err) ->
if err
robot.logger.error "hubot-redis-brain: Failed to authenticate to Redis"
else
robot.logger.info "hubot-redis-brain: Successfully authenticated to Redis"
getData()
client.on "error", (err) ->
if /ECONNREFUSED/.test err.message
else
robot.logger.error err.stack
client.on "connect", ->
robot.logger.debug "hubot-redis-brain: Successfully connected to Redis"
getData() if not info.auth
robot.brain.on 'save', (data = {}) ->
client.set "#{prefix}:storage", JSON.stringify data
robot.brain.on 'close', ->
client.quit()