lib/mongodb/mongodb-core/lib/sdam/monitoring.js
function monitorServer(server) {
  // executes a single check of a server
  const checkServer = callback => {
    let start = process.hrtime();

    // emit a signal indicating we have started the heartbeat
    server.emit('serverHeartbeatStarted', new ServerHeartbeatStartedEvent(server.name));

    server.command(
      'admin.$cmd',
      { ismaster: true },
      {
        monitoring: true,
        socketTimeout: server.s.options.connectionTimeout || 2000
      },
      function(err, result) {
        let duration = calculateDurationInMs(start);

        if (err) {
          server.emit(
            'serverHeartbeatFailed',
            new ServerHeartbeatFailedEvent(duration, err, server.name)
          );

          return callback(err, null);
        }

        const isMaster = result.result;
        server.emit(
          'serverHeartbeatSucceded',
          new ServerHeartbeatSucceededEvent(duration, isMaster, server.name)
        );

        return callback(null, isMaster);
      }
    );
  };

  const successHandler = isMaster => {
    server.s.monitoring = false;

    // emit an event indicating that our description has changed
    server.emit('descriptionReceived', new ServerDescription(server.description.address, isMaster));

    // schedule the next monitoring process
    server.s.monitorId = setTimeout(
      () => monitorServer(server),
      server.s.options.heartbeatFrequencyMS
    );
  };

  // run the actual monitoring loop
  server.s.monitoring = true;
  checkServer((err, isMaster) => {
    if (!err) {
      successHandler(isMaster);
      return;
    }

    // According to the SDAM specification's "Network error during server check" section, if
    // an ismaster call fails we reset the server's pool. If a server was once connected,
    // change its type to `Unknown` only after retrying once.

    // TODO: we need to reset the pool here

    return checkServer((err, isMaster) => {
      if (err) {
        server.s.monitoring = false;

        // revert to `Unknown` by emitting a default description with no isMaster
        server.emit('descriptionReceived', new ServerDescription(server.description.address));

        // do not reschedule monitoring in this case
        return;
      }

      successHandler(isMaster);
    });
  });
}
function monitorServer(server) {
  // executes a single check of a server
  const checkServer = callback => {
    let start = process.hrtime();

    // emit a signal indicating we have started the heartbeat
    server.emit('serverHeartbeatStarted', new ServerHeartbeatStartedEvent(server.name));

    server.command(
      'admin.$cmd',
      { ismaster: true },
      {
        monitoring: true,
        socketTimeout: server.s.options.connectionTimeout || 2000
      },
      function(err, result) {
        let duration = calculateDurationInMs(start);

        if (err) {
          server.emit(
            'serverHeartbeatFailed',
            new ServerHeartbeatFailedEvent(duration, err, server.name)
          );

          return callback(err, null);
        }

        const isMaster = result.result;
        server.emit(
          'serverHeartbeatSucceded',
          new ServerHeartbeatSucceededEvent(duration, isMaster, server.name)
        );

        return callback(null, isMaster);
      }
    );
  };

  const successHandler = isMaster => {
    server.s.monitoring = false;

    // emit an event indicating that our description has changed
    server.emit('descriptionReceived', new ServerDescription(server.description.address, isMaster));

    // schedule the next monitoring process
    server.s.monitorId = setTimeout(
      () => monitorServer(server),
      server.s.options.heartbeatFrequencyMS
    );
  };

  // run the actual monitoring loop
  server.s.monitoring = true;
  checkServer((err, isMaster) => {
    if (!err) {
      successHandler(isMaster);
      return;
    }

    // According to the SDAM specification's "Network error during server check" section, if
    // an ismaster call fails we reset the server's pool. If a server was once connected,
    // change its type to `Unknown` only after retrying once.

    // TODO: we need to reset the pool here

    return checkServer((err, isMaster) => {
      if (err) {
        server.s.monitoring = false;

        // revert to `Unknown` by emitting a default description with no isMaster
        server.emit('descriptionReceived', new ServerDescription(server.description.address));

        // do not reschedule monitoring in this case
        return;
      }

      successHandler(isMaster);
    });
  });
}
function monitorServer ( server ) { // executes a single check of a server const checkServer = callback => { let start = process . hrtime ( ) ; // emit a signal indicating we have started the heartbeat server . emit ( 'serverHeartbeatStarted' , new ServerHeartbeatStartedEvent ( server . name ) ) ; server . command ( 'admin.$cmd' , { ismaster : true } , { monitoring : true , socketTimeout : server . s . options . connectionTimeout || 2000 } , function ( err , result ) { let duration = calculateDurationInMs ( start ) ; if ( err ) { server . emit ( 'serverHeartbeatFailed' , new ServerHeartbeatFailedEvent ( duration , err , server . name ) ) ; return callback ( err , null ) ; } const isMaster = result . result ; server . emit ( 'serverHeartbeatSucceded' , new ServerHeartbeatSucceededEvent ( duration , isMaster , server . name ) ) ; return callback ( null , isMaster ) ; } ) ; } ; const successHandler = isMaster => { server . s . monitoring = false ; // emit an event indicating that our description has changed server . emit ( 'descriptionReceived' , new ServerDescription ( server . description . address , isMaster ) ) ; // schedule the next monitoring process server . s . monitorId = setTimeout ( ( ) => monitorServer ( server ) , server . s . options . heartbeatFrequencyMS ) ; } ; // run the actual monitoring loop server . s . monitoring = true ; checkServer ( ( err , isMaster ) => { if ( ! err ) { successHandler ( isMaster ) ; return ; } // According to the SDAM specification's "Network error during server check" section, if // an ismaster call fails we reset the server's pool. If a server was once connected, // change its type to `Unknown` only after retrying once. // TODO: we need to reset the pool here return checkServer ( ( err , isMaster ) => { if ( err ) { server . s . monitoring = false ; // revert to `Unknown` by emitting a default description with no isMaster server . emit ( 'descriptionReceived' , new ServerDescription ( server . description . address ) ) ; // do not reschedule monitoring in this case return ; } successHandler ( isMaster ) ; } ) ; } ) ; }
Performs a server check as described by the SDAM spec.

NOTE: This method automatically reschedules itself, so that there is always an active
monitoring process

@param {Server} server The server to monitor
Performs a server check as described by the SDAM spec .