From a66460f345d1ac9bdffa40e2272d76199d062e19 Mon Sep 17 00:00:00 2001 From: Jenny Chan Date: Mon, 30 Dec 2024 16:30:41 +0800 Subject: [PATCH 1/4] chore(redis): add max retry delay option & logs when reconnecting --- README.md | 1 + example/usingRedis/.env | 3 ++- lib/config/redis.js | 2 +- lib/plugins/redis/lib/Redis.js | 28 +++++++++++++++------------- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 9be5f05..a6cb4dc 100644 --- a/README.md +++ b/README.md @@ -484,6 +484,7 @@ REDIS_HOST REDIS_PORT REDIS_DATABASE REDIS_TIMEOUT_MS +REDIS_MAX_RETRY_DELAY ``` **_Please config your config/app.js_** diff --git a/example/usingRedis/.env b/example/usingRedis/.env index 890e231..de0d95a 100644 --- a/example/usingRedis/.env +++ b/example/usingRedis/.env @@ -3,4 +3,5 @@ REDIS_HOST=localhost REDIS_PORT=6379 REDIS_DATABASE=nodejs-framework-redis-example -REDIS_TIMEOUT_MS=4000 \ No newline at end of file +REDIS_TIMEOUT_MS=4000 +REDIS_MAX_RETRY_DELAY=2000 \ No newline at end of file diff --git a/lib/config/redis.js b/lib/config/redis.js index 699f339..30de1e0 100644 --- a/lib/config/redis.js +++ b/lib/config/redis.js @@ -1,9 +1,9 @@ module.exports = { - host: process.env.REDIS_HOST, port: process.env.REDIS_PORT, database: process.env.REDIS_DATABASE, user: process.env.REDIS_USER, pass: process.env.REDIS_PASS, timeoutMs: process.env.REDIS_TIMEOUT_MS, + maxRetryDelayMs: process.env.REDIS_MAX_RETRY_DELAY_MS, } diff --git a/lib/plugins/redis/lib/Redis.js b/lib/plugins/redis/lib/Redis.js index 94e7cc7..f99c3fd 100644 --- a/lib/plugins/redis/lib/Redis.js +++ b/lib/plugins/redis/lib/Redis.js @@ -52,33 +52,35 @@ class Redis { this.port = config.port this.database = config.database + this.maxRetryDelayMs = parseInt(config.maxRetryDelayMs, 10) || 0; + this.client = null return this } connect() { - return new Promise((resolve, reject) => { + this.client = this.redis.createClient({ + host: this.host, + port: this.port, + ...this.maxRetryDelayMs && { retry_max_delay: this.maxRetryDelayMs }, + }); - this.client = this.redis.createClient({ host: this.host, port: this.port }) - - this.client.select(this.database) + this.client.select(this.database); this.client.on('error', (err) => { - - return reject(err) - - }) + log('system', 'info', { message: `Redis Error: ${err}` }); + return reject(err); + }); this.client.on('connect', () => { + return resolve(this); + }); - return resolve(this) - - }) - + this.client.on('reconnecting', () => { log('system', 'info', { message: 'Redis Reconnecting' }) }); + this.client.on('ready', () => { log('system', 'info', { message: 'Redis ready' })}); }) - } get(id) { From 81aa69a54a6c1d76705312219aa676bcbb2a8088 Mon Sep 17 00:00:00 2001 From: Jenny Chan Date: Mon, 30 Dec 2024 18:09:26 +0800 Subject: [PATCH 2/4] fix(app): add config to throw & stop immediately when fail to start app --- lib/config/app.js | 1 + lib/models/App.js | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/config/app.js b/lib/config/app.js index 33341f3..09868ee 100644 --- a/lib/config/app.js +++ b/lib/config/app.js @@ -9,4 +9,5 @@ module.exports = { keepAliveTimeout: parseInt(process.env.KEEP_ALIVE_TIMEOUT || 65) * 1000, headersTimeout: parseInt(process.env.HEADERS_TIMEOUT || 60) * 1000, requestTimeout: parseInt(process.env.REQUEST_TIMEOUT || 60) * 1000, + throwAndStopWhenError: process.env.APP_THROW_AND_STOP_WHEN_ERROR === 'true', } diff --git a/lib/models/App.js b/lib/models/App.js index 8ac4c04..da3f97c 100644 --- a/lib/models/App.js +++ b/lib/models/App.js @@ -513,21 +513,28 @@ class App { */ async start() { + try { + await this.prepare() - await this.prepare() + await this.connectDependencies() - await this.connectDependencies() + await this.executePluginPhase('willStartService') - await this.executePluginPhase('willStartService') + await this.startService() - await this.startService() + process.on('SIGTERM', this.stop.bind(this)) - process.on('SIGTERM', this.stop.bind(this)) - - await this.executePluginPhase('didStartService') - - return this + await this.executePluginPhase('didStartService') + return this + } catch (err) { + console.error('error when starting app', err); + if (this.config && this.config.app && this.config.app.throwAndStopWhenError) { + // only throw & stop app if config is true (for backward compatibility) + await this.stop(); + throw err; + } + } } /* * @description the stop point of the app. it SHOULD NOT be overrided unless you know what you are doing From 4d093426eb83b8b509f6d430e9871703b04ef6de Mon Sep 17 00:00:00 2001 From: Jenny Chan Date: Tue, 31 Dec 2024 10:40:13 +0800 Subject: [PATCH 3/4] chore(app): remove catch error --- lib/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/server.js b/lib/server.js index 3560ff9..4aa42c5 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,3 +1,3 @@ let app = require(`${process.cwd()}/app.js`) -app.start().catch(console.log) +app.start() From d0745c3022b759c5868f197dfc544e08964cd215 Mon Sep 17 00:00:00 2001 From: Jenny Chan Date: Thu, 2 Jan 2025 16:51:39 +0800 Subject: [PATCH 4/4] fix(app): remove backward compatibility for error when starting app --- lib/config/app.js | 1 - lib/models/App.js | 7 ++----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/config/app.js b/lib/config/app.js index 09868ee..33341f3 100644 --- a/lib/config/app.js +++ b/lib/config/app.js @@ -9,5 +9,4 @@ module.exports = { keepAliveTimeout: parseInt(process.env.KEEP_ALIVE_TIMEOUT || 65) * 1000, headersTimeout: parseInt(process.env.HEADERS_TIMEOUT || 60) * 1000, requestTimeout: parseInt(process.env.REQUEST_TIMEOUT || 60) * 1000, - throwAndStopWhenError: process.env.APP_THROW_AND_STOP_WHEN_ERROR === 'true', } diff --git a/lib/models/App.js b/lib/models/App.js index da3f97c..358cac5 100644 --- a/lib/models/App.js +++ b/lib/models/App.js @@ -529,11 +529,8 @@ class App { return this } catch (err) { console.error('error when starting app', err); - if (this.config && this.config.app && this.config.app.throwAndStopWhenError) { - // only throw & stop app if config is true (for backward compatibility) - await this.stop(); - throw err; - } + await this.stop(); + throw err; } }