Example #1
0
    var pingServer = function() {
      // Else we need to start checking if the server is up
      server = new Server({
        host: host,
        port: port,
        connectionTimeout: 2000,
        socketTimeout: 2000,
        size: 1,
        reconnect: false
      });

      // On connect let's go
      server.on('connect', function(_server) {
        ismaster = server.lastIsMaster();
        _server.destroy();

        try {
          /*eslint no-sync:0*/
          pid = fs.readFileSync(pidfilepath, 'ascii').trim();
        } catch (err) {
          return setTimeout(pingServer, 1000);
        }

        // Finish up
        if (callback) {
          var _callback = callback;
          callback = null;
          _callback(null, server);
        }
      });

      var errHandler = function(err) {
        if (err.code === 10185) {
          try {
            pid = fs.readFileSync(pidfilepath, 'ascii').trim();
          } catch (err) {
            return setTimeout(pingServer, 1000);
          }

          var _callback = callback;
          callback = null;
          return _callback(null, server);
        }

        setTimeout(pingServer, 1000);
      };

      // Error or close handling
      server.once('error', errHandler);
      server.once('close', errHandler);
      server.once('timeout', errHandler);
      server.once('parseError', errHandler);

      // Attempt connect
      server.connect();
    };
Example #2
0
  this.connect = function(callback) {
    if (server.isConnected()) {
      return callback(null, server);
    }

    // Else we need to start checking if the server is up
    var s = new Server({
      host: host,
      port: port,
      connectionTimeout: 2000,
      socketTimeout: 2000,
      size: 1,
      reconnect: false,
      emitError: true
    });

    // On connect let's go
    s.on('connect', function(_server) {
      server = _server;

      ['error', 'close', 'timeout', 'parseError'].forEach(function(e) {
        server.removeAllListeners(e);
      });
      // If we have credentials apply them
      if (credentials) {
        return _server.auth(credentials.provider, credentials.db, credentials.user, credentials.password, function(err) {
          if (err) {
            return callback(err);
          }
          callback(null, _server);
        });
      }

      callback(null, _server);
    });

    // Error
    var e = function(err) {
      callback(err, null);
    };

    // Error or close handling
    s.once('error', e);
    s.once('close', e);
    s.once('timeout', e);

    // Attempt connect
    s.connect();
  };
Example #3
0
    var connectHandler = function() {
      // Clear out all the current handlers left over
      ["timeout", "error", "close"].forEach(function(e) {
        server.removeAllListeners(e);
      });

      // Set up listeners
      server.once('timeout',  errorHandler('timeout'));
      server.once('error',  errorHandler('error'));
      server.once('close', errorHandler('close'));

      // Emit open event
      self.emit('open', null, self);      

      // Return correctly
      try {
        callback(null, self);
      } catch(err) { 
        process.nextTick(function() { throw err; })
      }      
    }
Example #4
0
  this.connect = function(callback) {
    if (server.isConnected()) {
      debug('already connected')
      return callback(null, server);
    }

    var opt = {
      host: host,
      port: port,
      connectionTimeout: 2000,
      socketTimeout: 2000,
      size: 1,
      reconnect: false,
      emitError: true
    };

    // Set the key
    if (keys.indexOf('sslOnNormalPorts') !== -1) {
      opt.ssl = true;
    }
    if (keys.indexOf('ssl') !== -1) {
      opt.ssl = serverOptions.ssl;
    }
    if (keys.indexOf('ca') !== -1) {
      opt.ca = serverOptions.ca;
    }
    if (keys.indexOf('cert') !== -1) {
      opt.cert = serverOptions.cert;
    }
    if (keys.indexOf('rejectUnauthorized') !== -1) {
      opt.rejectUnauthorized = serverOptions.rejectUnauthorized;
    }
    if (keys.indexOf('key') !== -1) {
      opt.key = serverOptions.key;
    }
    if (keys.indexOf('passphrase') !== -1) {
      opt.passphrase = serverOptions.passphrase;
    }

    // Else we need to start checking if the server is up
    var s = new Server(opt);

    // On connect let's go
    s.on('connect', function(_server) {
      server = _server;

      ['error', 'close', 'timeout', 'parseError'].forEach(function(e) {
        server.removeAllListeners(e);
      });

      // If we have credentials apply them
      if (credentials) {
        return _server.auth(credentials.provider, credentials.db, credentials.user, credentials.password, function(err) {
          if (err) {
            return callback(err);
          }
          callback(null, _server);
        });
      }

      callback(null, _server);
    });

    // Error
    var e = function(err) {
      callback(err, null);
    };

    // Error or close handling
    s.once('error', e);
    s.once('close', e);
    s.once('timeout', e);
    debug('attempting to connect', JSON.stringify(opt, null, 2));
    s.connect();
  };
Example #5
0
    var pingServer = function() {
      if (server) {
        server.destroy();
      }
      var opt = {
        host: host,
        port: port,
        connectionTimeout: 2000,
        socketTimeout: 2000,
        size: 1,
        reconnect: false,
        emitError: typeof serverOptions.emitError === 'boolean' ? serverOptions.emitError : false
      };
      // Set the key
      if (keys.indexOf('sslOnNormalPorts') !== -1) {
        opt.ssl = true;
      }
      if (keys.indexOf('ssl') !== -1) {
        opt.ssl = serverOptions.ssl;
      }
      if (keys.indexOf('ca') !== -1) {
        opt.ca = serverOptions.ca;
      }
      if (keys.indexOf('cert') !== -1) {
        opt.cert = serverOptions.cert;
      }
      if (keys.indexOf('rejectUnauthorized') !== -1) {
        opt.rejectUnauthorized = serverOptions.rejectUnauthorized;
      }
      if (keys.indexOf('key') !== -1) {
        opt.key = serverOptions.key;
      }
      if (keys.indexOf('passphrase') !== -1) {
        opt.passphrase = serverOptions.passphrase;
      }

      // Else we need to start checking if the server is up
      server = new Server(opt);
      // On connect let's go
      server.on('connect', function(_server) {
        ismaster = server.lastIsMaster();
        _server.destroy();
        // Heap storage engine, no lock file available
        if (storageEngine === null) {
          try {
            // Read the pidfile
            /*eslint no-sync:0*/
            fs.readFileSync(path.join(dbpath, 'mongod.lock'), 'ascii').trim();
          } catch (err) {
            return setTimeout(pingServer, 1000);
          }
        }

        // Finish up
        if (callback) {
          var _callback = callback;
          callback = null;
          _callback(null, null);
        }
      });

      var errHandler = function() {
        setTimeout(pingServer, 1000);
      };

      // Error or close handling
      server.on('error', errHandler);
      server.on('close', errHandler);
      server.on('timeout', errHandler);
      server.once('parseError', errHandler);
      // Attempt connect
      server.connect();
    };
Example #6
0
  this.connect = function(db, _options, callback) {
    if('function' === typeof _options) callback = _options, _options = {};
    if(_options == null) _options = {};
    if(!('function' === typeof callback)) callback = null;
    options = _options;

    // Update bufferMaxEntries
    storeOptions.bufferMaxEntries = db.bufferMaxEntries;

    // Error handler
    var connectErrorHandler = function(event) {
      return function(err) {
        // Remove all event handlers
        var events = ['timeout', 'error', 'close'];
        events.forEach(function(e) {
          self.removeListener(e, connectErrorHandler);
        });

        server.removeListener('connect', connectErrorHandler);

        // Try to callback
        try {
          callback(err);
        } catch(err) { 
          process.nextTick(function() { throw err; })
        }
      }
    }

    // Actual handler
    var errorHandler = function(event) {
      return function(err) {
        if(event != 'error') {
          self.emit(event, err);
        }
      }
    }

    // Error handler
    var reconnectHandler = function(err) {
      self.emit('reconnect', self);
      store.execute();
    }

    // Connect handler
    var connectHandler = function() {
      // Clear out all the current handlers left over
      ["timeout", "error", "close"].forEach(function(e) {
        server.removeAllListeners(e);
      });

      // Set up listeners
      server.once('timeout',  errorHandler('timeout'));
      server.once('error',  errorHandler('error'));
      server.once('close', errorHandler('close'));

      // Emit open event
      self.emit('open', null, self);      

      // Return correctly
      try {
        callback(null, self);
      } catch(err) { 
        process.nextTick(function() { throw err; })
      }      
    }

    // Set up listeners
    server.once('timeout',  connectErrorHandler('timeout'));
    server.once('error',  connectErrorHandler('error'));
    server.once('close', connectErrorHandler('close'));
    server.once('connect', connectHandler);
    // Reconnect server
    server.on('reconnect', reconnectHandler);

    // Start connection
    server.connect(_options);
  }