You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
906 B

  1. const Redis = require('ioredis')
  2. const { isPlainObject } = require('lodash')
  3. /* global WIKI */
  4. module.exports = {
  5. init() {
  6. if (isPlainObject(WIKI.config.redis)) {
  7. let red = new Redis(WIKI.config.redis)
  8. red.on('ready', () => {
  9. WIKI.logger.info('Redis connection: [ OK ]')
  10. })
  11. red.on('error', () => {
  12. WIKI.logger.error('Failed to connect to Redis instance!')
  13. process.exit(1)
  14. })
  15. return red
  16. } else {
  17. WIKI.logger.error('Invalid Redis configuration!')
  18. process.exit(1)
  19. }
  20. },
  21. subscribe() {
  22. let red = this.init()
  23. red.on('message', (channel, msg) => {
  24. WIKI.events.emit(channel, msg)
  25. })
  26. red.subscribe('localization', (err, count) => {
  27. if (err) {
  28. WIKI.logger.error(err)
  29. process.exit(1)
  30. }
  31. WIKI.logger.info('Redis Subscriber connection: [ OK ]')
  32. })
  33. return red
  34. }
  35. }