@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.
@@ -1,5 +1,9 @@
1
1
  import type { EnvBase } from "@leyyo/env";
2
2
  export interface RedisCommonConf extends EnvBase {
3
+ /**
4
+ * Redis Enabled
5
+ * */
6
+ readonly ENABLED: boolean;
3
7
  /**
4
8
  * Redis Protocol
5
9
  * */
@@ -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
- .field('PROTOCOL').text().def('redis').end()
12
- .field('HOST').text().required().end()
13
- .field('PORT').integer().def(6379).end()
14
- .field('USER').text().end()
15
- .field('PASS').text().end()
16
- .field('PREFIX').text().end()
17
- .field('DB_NUMBER').integer().end()
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
- this._props.protocol = (_a = this._props.protocol) !== null && _a !== void 0 ? _a : env.PROTOCOL;
84
- this._props.host = (_b = this._props.host) !== null && _b !== void 0 ? _b : env.HOST;
85
- this._props.port = (_c = this._props.port) !== null && _c !== void 0 ? _c : env.PORT;
86
- this._props.username = (_d = this._props.username) !== null && _d !== void 0 ? _d : env.USER;
87
- this._props.password = (_e = this._props.password) !== null && _e !== void 0 ? _e : env.PASS;
88
- this._props.dbNumber = (_f = this._props.dbNumber) !== null && _f !== void 0 ? _f : env.DB_NUMBER;
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
- if (this._props.isConnected) {
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 (this._props.tryCount > RedisConnection.TRY_COUNT) {
131
- const err = new be_cache_common_1.CacheError('Maximum try county', { count: this._props.tryCount });
132
- this.logger.error(be_base_common_1.errorHandler.common.logText(err, 'connect', 'try', this._props.tryCount));
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
- this._props.client = redis.createClient({ url: this._props.producedUrl });
137
- this._props.client
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
- this._props.tryCount++;
141
- const old = this._props.isConnected;
142
- this._props.isConnected = false;
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
- this._props.isConnected = true;
151
- this._props.tryCount = 0;
156
+ props.isConnected = true;
157
+ props.tryCount = 0;
152
158
  this._triggerOnCase('connected', this._onConnected, false);
153
- if (this._props.isFirst === undefined) {
159
+ if (props.isFirst === undefined) {
154
160
  this._triggerOnCase('first-connected', this._onFirstConnected, true);
155
- this._props.isFirst = false;
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 this._props.client.connect();
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
- // this._props.client.destroy()
181
+ const { enabled, isConnected, client } = this._props;
182
+ if (!enabled) {
183
+ return false;
184
+ }
176
185
  let result = false;
177
- if (this._props.isConnected) {
186
+ if (isConnected) {
178
187
  try {
179
- yield this._props.client.ping();
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",
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.1",
71
+ "@leyyo/env": "^1.2.5",
72
72
  "@samet-it/be-base-common": "^1.1.3",
73
- "@samet-it/be-cache-common": "^1.1.3",
73
+ "@samet-it/be-cache-common": "^1.1.4",
74
74
  "redis": "^5.10.0"
75
75
  }
76
76
  }