@samet-it/be-redis-common 1.1.3 → 1.1.4
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.
|
@@ -8,11 +8,28 @@ const env_1 = require("@leyyo/env");
|
|
|
8
8
|
exports.redisCommonConfig = env_1.envCore.configure
|
|
9
9
|
.scope('RedisCommon', 'REDIS')
|
|
10
10
|
.start()
|
|
11
|
-
|
|
12
|
-
.field('
|
|
13
|
-
.field('
|
|
14
|
-
.
|
|
15
|
-
.
|
|
16
|
-
.field('
|
|
17
|
-
.
|
|
11
|
+
// @formatter:off
|
|
12
|
+
.field('ENABLED').boolean().def(true).end()
|
|
13
|
+
.field('PROTOCOL')
|
|
14
|
+
.off(v => v.ENABLED)
|
|
15
|
+
.text().def('redis').end()
|
|
16
|
+
.field('HOST')
|
|
17
|
+
.off(v => v.ENABLED)
|
|
18
|
+
.text().required().end()
|
|
19
|
+
.field('PORT')
|
|
20
|
+
.off(v => v.ENABLED)
|
|
21
|
+
.integer().def(6379).end()
|
|
22
|
+
.field('USER')
|
|
23
|
+
.off(v => v.ENABLED)
|
|
24
|
+
.text().end()
|
|
25
|
+
.field('PASS')
|
|
26
|
+
.off(v => v.ENABLED)
|
|
27
|
+
.text().end()
|
|
28
|
+
.field('PREFIX')
|
|
29
|
+
.off(v => v.ENABLED)
|
|
30
|
+
.text().end()
|
|
31
|
+
.field('DB_NUMBER')
|
|
32
|
+
.off(v => v.ENABLED)
|
|
33
|
+
.integer().end()
|
|
34
|
+
// @formatter:on
|
|
18
35
|
.finish();
|
|
@@ -74,18 +74,19 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
74
74
|
* Read credentials from environment
|
|
75
75
|
* */
|
|
76
76
|
_readFromEnv() {
|
|
77
|
-
var _a, _b, _c, _d, _e, _f;
|
|
77
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
78
78
|
const { _props: props } = this;
|
|
79
79
|
let env = config_1.redisCommonConfig.valueShort;
|
|
80
80
|
if (props.envVariant) {
|
|
81
81
|
env = config_1.redisCommonConfig.configure.getVariation(props.envVariant).valueShort;
|
|
82
82
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
props.enabled = (_a = props.enabled) !== null && _a !== void 0 ? _a : env.ENABLED;
|
|
84
|
+
props.protocol = (_b = props.protocol) !== null && _b !== void 0 ? _b : env.PROTOCOL;
|
|
85
|
+
props.host = (_c = props.host) !== null && _c !== void 0 ? _c : env.HOST;
|
|
86
|
+
props.port = (_d = props.port) !== null && _d !== void 0 ? _d : env.PORT;
|
|
87
|
+
props.username = (_e = props.username) !== null && _e !== void 0 ? _e : env.USER;
|
|
88
|
+
props.password = (_f = props.password) !== null && _f !== void 0 ? _f : env.PASS;
|
|
89
|
+
props.dbNumber = (_g = props.dbNumber) !== null && _g !== void 0 ? _g : env.DB_NUMBER;
|
|
89
90
|
}
|
|
90
91
|
/**
|
|
91
92
|
* Build url from credentials
|
|
@@ -124,22 +125,27 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
124
125
|
/** {@inheritDoc} */
|
|
125
126
|
connect() {
|
|
126
127
|
return __awaiter(this, void 0, void 0, function* () {
|
|
127
|
-
|
|
128
|
+
const { _props: props } = this;
|
|
129
|
+
const { enabled, isConnected, tryCount, producedUrl } = props;
|
|
130
|
+
if (!enabled) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (isConnected) {
|
|
128
134
|
return true;
|
|
129
135
|
}
|
|
130
|
-
if (
|
|
131
|
-
const err = new be_cache_common_1.CacheError('Maximum try county', {
|
|
132
|
-
this.logger.error(be_base_common_1.errorHandler.common.logText(err, 'connect', 'try',
|
|
136
|
+
if (tryCount > RedisConnection.TRY_COUNT) {
|
|
137
|
+
const err = new be_cache_common_1.CacheError('Maximum try county', { tryCount });
|
|
138
|
+
this.logger.error(be_base_common_1.errorHandler.common.logText(err, 'connect', 'try', tryCount));
|
|
133
139
|
return false;
|
|
134
140
|
}
|
|
135
141
|
try {
|
|
136
|
-
|
|
137
|
-
|
|
142
|
+
props.client = redis.createClient({ url: producedUrl });
|
|
143
|
+
props.client
|
|
138
144
|
.on("error", err => {
|
|
139
145
|
this.checkError(err, { silent: true, name: 'connection' });
|
|
140
|
-
|
|
141
|
-
const old =
|
|
142
|
-
|
|
146
|
+
props.tryCount++;
|
|
147
|
+
const old = isConnected;
|
|
148
|
+
props.isConnected = false;
|
|
143
149
|
if (old) {
|
|
144
150
|
this._triggerOnCase('disconnected', this._onDisconnected, false);
|
|
145
151
|
}
|
|
@@ -147,19 +153,19 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
147
153
|
})
|
|
148
154
|
.on('connect', () => {
|
|
149
155
|
this.logger.info(`on[connect]`);
|
|
150
|
-
|
|
151
|
-
|
|
156
|
+
props.isConnected = true;
|
|
157
|
+
props.tryCount = 0;
|
|
152
158
|
this._triggerOnCase('connected', this._onConnected, false);
|
|
153
|
-
if (
|
|
159
|
+
if (props.isFirst === undefined) {
|
|
154
160
|
this._triggerOnCase('first-connected', this._onFirstConnected, true);
|
|
155
|
-
|
|
161
|
+
props.isFirst = false;
|
|
156
162
|
}
|
|
157
163
|
})
|
|
158
164
|
.on('ready', () => this.logger.info('on[ready]'))
|
|
159
165
|
.on('close', () => this.logger.info('on[close]'))
|
|
160
166
|
.on('reconnecting', () => this.logger.info('on[reconnecting]'))
|
|
161
167
|
.on('end', () => this.logger.info('on[end]'));
|
|
162
|
-
yield
|
|
168
|
+
yield props.client.connect();
|
|
163
169
|
this.logger.log('Connected');
|
|
164
170
|
setTimeout(() => this.ping(true).then(), 30000);
|
|
165
171
|
}
|
|
@@ -172,11 +178,14 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
172
178
|
/** {@inheritDoc} */
|
|
173
179
|
ping(next) {
|
|
174
180
|
return __awaiter(this, void 0, void 0, function* () {
|
|
175
|
-
|
|
181
|
+
const { enabled, isConnected, client } = this._props;
|
|
182
|
+
if (!enabled) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
176
185
|
let result = false;
|
|
177
|
-
if (
|
|
186
|
+
if (isConnected) {
|
|
178
187
|
try {
|
|
179
|
-
yield
|
|
188
|
+
yield client.ping();
|
|
180
189
|
result = true;
|
|
181
190
|
}
|
|
182
191
|
catch (err) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samet-it/be-redis-common",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Redis common component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"redis",
|
|
@@ -68,9 +68,9 @@
|
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
70
|
"@leyyo/common": "^1.2.3",
|
|
71
|
-
"@leyyo/env": "^1.2.
|
|
71
|
+
"@leyyo/env": "^1.2.5",
|
|
72
72
|
"@samet-it/be-base-common": "^1.1.3",
|
|
73
|
-
"@samet-it/be-cache-common": "^1.1.
|
|
73
|
+
"@samet-it/be-cache-common": "^1.1.4",
|
|
74
74
|
"redis": "^5.10.0"
|
|
75
75
|
}
|
|
76
76
|
}
|