src/MasterElector.js
function electMaster (advertiseId) {
    _advertiseId = advertiseId || null
    if (_electingMaster) return _electingMaster
    _electingMaster = _requestVotes()
                      .then(nodes => {
                        _electingMaster = false
                        _advertiseId = null
                        const masterNodes = nodes.filter(
                          ({isMaster}) => isMaster)
                        nodes = masterNodes.length ? masterNodes : nodes
                        const electedMaster = sortBy(nodes, ({id}) => id)[0]
                        if (!electedMaster) throw new Error('could not elect a master')

                        debug(`elected master: ${electedMaster.name} ${JSON.stringify(electedMaster.endpoints)}`)
                        _broadcastMessage('masterElected', electedMaster)
                        return electedMaster
                      })

    _electingMaster.catch(() => {
      _electingMaster = false
      _advertiseId = null
    })
    return _electingMaster
  }
function electMaster (advertiseId) {
    _advertiseId = advertiseId || null
    if (_electingMaster) return _electingMaster
    _electingMaster = _requestVotes()
                      .then(nodes => {
                        _electingMaster = false
                        _advertiseId = null
                        const masterNodes = nodes.filter(
                          ({isMaster}) => isMaster)
                        nodes = masterNodes.length ? masterNodes : nodes
                        const electedMaster = sortBy(nodes, ({id}) => id)[0]
                        if (!electedMaster) throw new Error('could not elect a master')

                        debug(`elected master: ${electedMaster.name} ${JSON.stringify(electedMaster.endpoints)}`)
                        _broadcastMessage('masterElected', electedMaster)
                        return electedMaster
                      })

    _electingMaster.catch(() => {
      _electingMaster = false
      _advertiseId = null
    })
    return _electingMaster
  }
function electMaster ( advertiseId ) { _advertiseId = advertiseId || null if ( _electingMaster ) return _electingMaster _electingMaster = _requestVotes ( ) . then ( nodes => { _electingMaster = false _advertiseId = null const masterNodes = nodes . filter ( ( { isMaster } ) => isMaster ) nodes = masterNodes . length ? masterNodes : nodes const electedMaster = sortBy ( nodes , ( { id } ) => id ) [ 0 ] if ( ! electedMaster ) throw new Error ( 'could not elect a master' ) debug ( ` ${ electedMaster . name } ${ JSON . stringify ( electedMaster . endpoints ) } ` ) _broadcastMessage ( 'masterElected' , electedMaster ) return electedMaster } ) _electingMaster . catch ( ( ) => { _electingMaster = false _advertiseId = null } ) return _electingMaster }
Triggers a master election
@alias resolve
@param  {string} [advertiseId] - A fake id to use for this node during the election
@return {promise} An object containing the name and the pub/sub endpoints of the master node
Triggers a master election