Skip to content

Commit

Permalink
Merge pull request #147 from shoplineapp/fix/DC-3170-redis-connect-error
Browse files Browse the repository at this point in the history
Fix/dc 3170 redis connect error
  • Loading branch information
travis-shoplineapp authored Jan 7, 2025
2 parents 5fb181e + d0745c3 commit bdc792e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ REDIS_HOST
REDIS_PORT
REDIS_DATABASE
REDIS_TIMEOUT_MS
REDIS_MAX_RETRY_DELAY
```

**_Please config your config/app.js_**
Expand Down
3 changes: 2 additions & 1 deletion example/usingRedis/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DATABASE=nodejs-framework-redis-example
REDIS_TIMEOUT_MS=4000
REDIS_TIMEOUT_MS=4000
REDIS_MAX_RETRY_DELAY=2000
2 changes: 1 addition & 1 deletion lib/config/redis.js
Original file line number Diff line number Diff line change
@@ -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,
}
22 changes: 13 additions & 9 deletions lib/models/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,21 +513,25 @@ 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);
await this.stop();
throw err;
}
}

/* * @description the stop point of the app. it SHOULD NOT be overrided unless you know what you are doing
Expand Down
28 changes: 15 additions & 13 deletions lib/plugins/redis/lib/Redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let app = require(`${process.cwd()}/app.js`)

app.start().catch(console.log)
app.start()

0 comments on commit bdc792e

Please sign in to comment.