@mimik/rediser 1.4.8 → 1.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +39 -15
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -8,8 +8,6 @@ const Promise = require('bluebird');
|
|
|
8
8
|
* const redis = require('@mimik/rediser');
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
Promise.promisifyAll(redis.RedisClient.prototype);
|
|
12
|
-
|
|
13
11
|
const type = 'redis';
|
|
14
12
|
const correlationId = `${type}-cache-start@0/${new Date().toISOString()}`;
|
|
15
13
|
|
|
@@ -17,7 +15,8 @@ const VALIDATION_CHECK = 100; // in ms
|
|
|
17
15
|
const CONNECTED = 1;
|
|
18
16
|
const DISCONNECTED = 2;
|
|
19
17
|
|
|
20
|
-
let
|
|
18
|
+
let displayCreate = true;
|
|
19
|
+
let displayDisconnect = true;
|
|
21
20
|
let state = DISCONNECTED;
|
|
22
21
|
|
|
23
22
|
module.exports = (set, config) => {
|
|
@@ -33,11 +32,35 @@ module.exports = (set, config) => {
|
|
|
33
32
|
*/
|
|
34
33
|
const initializeSync = () => {
|
|
35
34
|
if (set !== 'on') {
|
|
36
|
-
if (
|
|
37
|
-
|
|
35
|
+
if (displayCreate) {
|
|
36
|
+
logger.info('cache not enabled', { type }, correlationId);
|
|
37
|
+
displayCreate = false;
|
|
38
|
+
}
|
|
38
39
|
return null;
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
+
let interval;
|
|
42
|
+
const disconnectHandler = () => {
|
|
43
|
+
state = DISCONNECTED;
|
|
44
|
+
if (!interval) {
|
|
45
|
+
interval = setInterval(() => {
|
|
46
|
+
if (state !== CONNECTED) {
|
|
47
|
+
if (displayDisconnect) {
|
|
48
|
+
logger.error('fatal error: Timeout in connecting to cache', {
|
|
49
|
+
type, error: state, timeout: redisSettings.connectTimeout,
|
|
50
|
+
}, correlationId);
|
|
51
|
+
displayDisconnect = false;
|
|
52
|
+
}
|
|
53
|
+
logger.flushAndExit(1);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
clearInterval(interval);
|
|
57
|
+
interval = null;
|
|
58
|
+
}
|
|
59
|
+
}, config.redisSettings.connectTimeout * 1000); // convert in seconds
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (displayCreate) {
|
|
41
64
|
if (isProd(config.nodeEnvironment)) {
|
|
42
65
|
const redisSettingsClone = { ...redisSettings };
|
|
43
66
|
|
|
@@ -45,22 +68,23 @@ module.exports = (set, config) => {
|
|
|
45
68
|
logger.info('creating a cache connection', { type, settings: redisSettingsClone }, correlationId);
|
|
46
69
|
}
|
|
47
70
|
else logger.info('creating a cache connection', { type, settings: redisSettings }, correlationId);
|
|
71
|
+
displayCreate = false;
|
|
48
72
|
}
|
|
49
|
-
display = false;
|
|
50
73
|
const client = redis.createClient(redisSettings.url, redisSettings.options);
|
|
74
|
+
client.connect();
|
|
51
75
|
|
|
52
76
|
client.on('error', (err) => {
|
|
53
77
|
state = err;
|
|
54
78
|
});
|
|
55
|
-
client.on('reconnecting',
|
|
56
|
-
state = DISCONNECTED;
|
|
57
|
-
});
|
|
79
|
+
client.on('reconnecting', disconnectHandler);
|
|
58
80
|
client.on('ready', () => {
|
|
59
81
|
state = CONNECTED;
|
|
82
|
+
if (interval) {
|
|
83
|
+
clearInterval(interval);
|
|
84
|
+
interval = null;
|
|
85
|
+
}
|
|
60
86
|
});
|
|
61
|
-
client.on('end', () =>
|
|
62
|
-
state = DISCONNECTED;
|
|
63
|
-
});
|
|
87
|
+
client.on('end', () => disconnectHandler);
|
|
64
88
|
|
|
65
89
|
return client;
|
|
66
90
|
};
|
|
@@ -83,7 +107,7 @@ module.exports = (set, config) => {
|
|
|
83
107
|
if (set !== 'on') return Promise.resolve(null);
|
|
84
108
|
const interval = setInterval(() => {
|
|
85
109
|
if (state !== CONNECTED) {
|
|
86
|
-
logger.error('fatal error: Timeout in connecting to
|
|
110
|
+
logger.error('fatal error: Timeout in connecting to cache', {
|
|
87
111
|
type, error: state, timeout: redisSettings.connectTimeout,
|
|
88
112
|
}, correlationId);
|
|
89
113
|
logger.flushAndExit(1);
|
|
@@ -92,7 +116,7 @@ module.exports = (set, config) => {
|
|
|
92
116
|
|
|
93
117
|
return Promise.delay(VALIDATION_CHECK).then(() => {
|
|
94
118
|
if (state !== DISCONNECTED && state !== CONNECTED) {
|
|
95
|
-
const error = new Error(`connection not established: ${state}`);
|
|
119
|
+
const error = new Error(`cache connection not established: ${state}`);
|
|
96
120
|
|
|
97
121
|
logger.error('cache connection error', { type, error }, correlationId);
|
|
98
122
|
throw error;
|