Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/dc 3170 redis connect error #147

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()