wiki/server/core/db.js
/**
   * Subscribe to database LISTEN / NOTIFY for multi-instances events
   */
Subscribe to database LISTEN / NOTIFY for multi-instances events
Subscribe to database LISTEN / NOTIFY for multi - instances events
async subscribeToNotifications () {
    const useHA = (WIKI.config.ha === true || WIKI.config.ha === 'true' || WIKI.config.ha === 1 || WIKI.config.ha === '1')
    if (!useHA) {
      return
    } else if (WIKI.config.db.type !== 'postgres') {
      WIKI.logger.warn(`Database engine doesn't support pub/sub. Will not handle concurrent instances: [ DISABLED ]`)
      return
    }
    const PGPubSub = require('pg-pubsub')
    this.listener = new PGPubSub(this.knex.client.connectionSettings, {
      log (ev) {
        WIKI.logger.debug(ev)
      }
    })
    this.listener.addChannel('wiki', payload => {
      if (_.has(payload, 'event') && payload.source !== WIKI.INSTANCE_ID) {
        WIKI.logger.info(`Received event ${payload.event} from instance ${payload.source}: [ OK ]`)
        WIKI.events.inbound.emit(payload.event, payload.value)
      }
    })
    WIKI.events.outbound.onAny(this.notifyViaDB)
    WIKI.auth.subscribeToEvents()
    WIKI.configSvc.subscribeToEvents()
    WIKI.models.pages.subscribeToEvents()
    WIKI.logger.info(`High-Availability Listener initialized successfully: [ OK ]`)
  }
async subscribeToNotifications ( ) { const useHA = ( WIKI . config . ha === true || WIKI . config . ha === 'true' || WIKI . config . ha === 1 || WIKI . config . ha === '1' ) if ( ! useHA ) { return } else if ( WIKI . config . db . type !== 'postgres' ) { WIKI . logger . warn ( ` ` ) return } const PGPubSub = require ( 'pg-pubsub' ) this . listener = new PGPubSub ( this . knex . client . connectionSettings , { log ( ev ) { WIKI . logger . debug ( ev ) } } ) this . listener . addChannel ( 'wiki' , payload => { if ( _ . has ( payload , 'event' ) && payload . source !== WIKI . INSTANCE_ID ) { WIKI . logger . info ( ` ${ payload . event } ${ payload . source } ` ) WIKI . events . inbound . emit ( payload . event , payload . value ) } } ) WIKI . events . outbound . onAny ( this . notifyViaDB ) WIKI . auth . subscribeToEvents ( ) WIKI . configSvc . subscribeToEvents ( ) WIKI . models . pages . subscribeToEvents ( ) WIKI . logger . info ( ` ` ) }
Subscribe to database LISTEN / NOTIFY for multi-instances events
Subscribe to database LISTEN / NOTIFY for multi - instances events
// -> Outbound events handling // -> Listen to inbound events