2017-07-23 03:56:46 +00:00
|
|
|
const Redis = require('ioredis')
|
|
|
|
const { isPlainObject } = require('lodash')
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-07-23 03:56:46 +00:00
|
|
|
|
2018-02-04 05:53:13 +00:00
|
|
|
module.exports = {
|
2017-07-23 03:56:46 +00:00
|
|
|
init() {
|
2018-03-05 20:49:36 +00:00
|
|
|
if (isPlainObject(WIKI.config.redis)) {
|
|
|
|
let red = new Redis(WIKI.config.redis)
|
2017-08-03 03:47:11 +00:00
|
|
|
red.on('ready', () => {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.info('Redis connection: [ OK ]')
|
2017-08-03 03:47:11 +00:00
|
|
|
})
|
2017-11-13 04:48:19 +00:00
|
|
|
red.on('error', () => {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error('Failed to connect to Redis instance!')
|
2017-11-13 04:48:19 +00:00
|
|
|
process.exit(1)
|
|
|
|
})
|
2017-08-03 03:47:11 +00:00
|
|
|
return red
|
2017-07-23 03:56:46 +00:00
|
|
|
} else {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error('Invalid Redis configuration!')
|
2017-07-23 03:56:46 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
2018-11-18 21:38:00 +00:00
|
|
|
},
|
|
|
|
subscribe() {
|
|
|
|
let red = this.init()
|
|
|
|
red.on('message', (channel, msg) => {
|
|
|
|
WIKI.events.emit(channel, msg)
|
|
|
|
})
|
|
|
|
red.subscribe('localization', (err, count) => {
|
|
|
|
if (err) {
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
WIKI.logger.info('Redis Subscriber connection: [ OK ]')
|
|
|
|
})
|
|
|
|
return red
|
2017-07-23 03:56:46 +00:00
|
|
|
}
|
|
|
|
}
|