()
| 13 | } |
| 14 | |
| 15 | getConnection(): RedisClientType { |
| 16 | if (this.connected) { |
| 17 | return this.client; |
| 18 | } else { |
| 19 | this.client = createClient({ |
| 20 | url: this.conf.URI, |
| 21 | }); |
| 22 | |
| 23 | this.client.on('connect', () => { |
| 24 | this.logger.verbose('redis connecting'); |
| 25 | }); |
| 26 | |
| 27 | this.client.on('ready', () => { |
| 28 | this.logger.verbose('redis ready'); |
| 29 | this.connected = true; |
| 30 | }); |
| 31 | |
| 32 | this.client.on('error', () => { |
| 33 | this.logger.error('redis disconnected'); |
| 34 | this.connected = false; |
| 35 | }); |
| 36 | |
| 37 | this.client.on('end', () => { |
| 38 | this.logger.verbose('redis connection ended'); |
| 39 | this.connected = false; |
| 40 | }); |
| 41 | |
| 42 | try { |
| 43 | this.client.connect(); |
| 44 | this.connected = true; |
| 45 | } catch (e) { |
| 46 | this.connected = false; |
| 47 | this.logger.error('redis connect exception caught: ' + e); |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | return this.client; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | export const redisClient = new Redis(); |
no test coverage detected