Skip to content

Commit

Permalink
fix(cache): loop error thrown while can't connect to the redis
Browse files Browse the repository at this point in the history
  • Loading branch information
a632079 committed May 17, 2020
1 parent 59f5dee commit 2167415
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const colors = require('colors')
// Promisify Redis
const redis = bluebrid.promisifyAll(require('redis'))

let connectionFailedAttemp = 0
class cache {
static connect (newConnection = false) {
// Get Config
Expand All @@ -22,9 +23,16 @@ class cache {
// Connect Redis
if (!newConnection) {
this.redis = redis.createClient(config)
this.redis.on('connect', () => {
connectionFailedAttemp = 0 // clear the attemp count
})
this.redis.on('error', err => {
console.log(colors.red(err.stack))
winston.error('redis connection is closed. try to reconnect.')
if (connectionFailedAttemp >= 3) {
winston.error('Attemp to connect to redis ' + connectionFailedAttemp + ' times, but all failed. Process exiting.')
}
winston.error('failed to connect to redis. Attemp again')
connectionFailedAttemp++
cache.connect()
})
return true
Expand Down

0 comments on commit 2167415

Please sign in to comment.