forked from dwyl/redis-connection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (70 loc) · 2.23 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var redis = require('redis');
var url = require('url');
var rejson = require('redis-rejson');
var rc; // redis config
if (process.env.REDISCLOUD_URL) {
var redisURL = url.parse(process.env.REDISCLOUD_URL);
rc = {
port: redisURL.port,
host: redisURL.hostname,
auth: redisURL.auth.split(":")[1]
}
}
else {
rc = {
port: 6379,
host: '127.0.0.1'
// auth: '' no auth on localhost see: https://git.io/vH3TN
}
}
var CON = {}; // store redis connections as Object
function new_connection () {
//enable rejson if ENABLE_REJSON flag is set (the flag can have any value, we only check if it is set or not)
if(process.env.ENABLE_REJSON){
rejson(redis);
}
var redis_con = redis.createClient(rc.port, rc.host);
if (process.env.REDISCLOUD_URL && rc.auth) { // only auth on CI/Stage/Prod
redis_con.auth(rc.auth); // see: https://git.io/vH3TN
}
return redis_con;
}
function redis_connection (type) {
type = type || 'DEFAULT'; // allow infinite types of connections
if (!CON[type] || !CON[type].connected) {
CON[type] = new_connection();
}
return CON[type];
}
module.exports = redis_connection;
module.exports.kill = function(type) {
type = type || 'DEFAULT'; // kill specific connection or default one
CON[type].end(true);
delete CON[type];
}
module.exports.killall = function() {
var keys = Object.keys(CON);
keys.forEach(function(k){
CON[k].end(true);
delete CON[k];
})
}
/**
* In the event of a failed connection we don't want our Node.js App to "Die"!
* rather we want to report that the connection failed but then keep running...
* see: github.com/dwyl/redis-connection/issues/38
* @param {Object} err - the error Object thrown by Redis (standard node error)
* @returns {Object} err - unmodified error
*/
var reported; // bit
function report_error (err) {
if (!reported && err.syscall === 'connect' && err.code === 'ECONNREFUSED') {
reported = true; // only report the error once.
console.log('- - - - - - - - Redis Connection Error: - - - - - - - - ')
console.error(err);
console.log('- - - - - - - - - - - - - - - - - - - - - - - - - - - - ')
}
return err;
}
process.on('uncaughtException', report_error);
module.exports.report_error = report_error;