diff --git a/lib/mongobox.js b/lib/mongobox.js index 85c82b5..da1ca3e 100644 --- a/lib/mongobox.js +++ b/lib/mongobox.js @@ -12,7 +12,7 @@ var DEFAULT_ARGS = [ // don't flood stdout, we're not reading it "--quiet", // save the port - "--nohttpinterface", + //"--nohttpinterface", // disable unused. "--nounixsocket", // use a smaller default file size @@ -24,7 +24,7 @@ var DEFAULT_ARGS = [ var START_CHECK_ATTEMPTS = 200; -var MongoBox = function(_options) { +var MongoBox = function (_options) { var options = _options || {}; this.options = options; @@ -39,13 +39,13 @@ var MongoBox = function(_options) { } var self = this; - process.on('exit', function(code) { + process.on('exit', function (code) { self.stop(); }); } -MongoBox.prototype.start = function(callback) { +MongoBox.prototype.start = function (callback) { if (this.options.databasePath) { if (!fs.existsSync(this.options.databasePath)) { fs.mkdirSync(this.options.databasePath); @@ -57,24 +57,24 @@ MongoBox.prototype.start = function(callback) { var self = this; if (!this.options.port) { - freeport(function(err, port) { + freeport(function (err, port) { if (err) return callback(err); self.options.port = port; doStart.bind(self)(); }); } else { - doStart(); + doStart.bind(self)(); } - var doStart = function() { + function doStart() { var args = _.clone(DEFAULT_ARGS); var op - args = args.concat(['--dbpath', this.options.databasePath]); - args = args.concat(['--port', this.options.port]); + args = args.concat([ '--dbpath', this.options.databasePath ]); + args = args.concat([ '--port', this.options.port ]); if (this.options.logPath) - args = args.concat(['--logpath', this.options.logPath]); + args = args.concat([ '--logpath', this.options.logPath ]); if (this.options.auth) args.push("--auth"); @@ -90,7 +90,7 @@ MongoBox.prototype.start = function(callback) { // console.log("%s %s", this.options.mongodBinary, args.join(" ")); var self = this; - this.process.on('close', function(code) { + this.process.on('close', function (code) { self.process = undefined; utils.rmdirSync(self.options.databasePath); if (self._temporary) { @@ -101,7 +101,7 @@ MongoBox.prototype.start = function(callback) { if (self._onStopped) self._onStopped(null, code); }); - this.process.on('error', function(e) { + this.process.on('error', function (e) { console.error(e); self.stop(); }); @@ -110,34 +110,34 @@ MongoBox.prototype.start = function(callback) { } } -MongoBox.prototype.stop = function(callback) { +MongoBox.prototype.stop = function (callback) { if (!this.process) return callback ? callback() : _.noop(); this._onStopped = callback; this.process.kill(); } -MongoBox.prototype.isRunning = function() { +MongoBox.prototype.isRunning = function () { return this.process !== undefined; } -MongoBox.prototype.getPort = function() { +MongoBox.prototype.getPort = function () { return this.options.port; } -MongoBox.prototype._waitTillStarted = function(callback) { +MongoBox.prototype._waitTillStarted = function (callback) { var attempts = 0; var self = this; function tryConnect() { - var socket = net.connect({port: self.options.port}, function() { + var socket = net.connect({ port: self.options.port }, function () { callback(); }); - socket.on('error', function() { + socket.on('error', function () { attempts += 1; if (self.process && attempts < START_CHECK_ATTEMPTS) setTimeout(tryConnect, 250); - else callback(new Error('MongoDB did not start.')) + else callback(new Error('MongoDB did not start...['+attempts+']')) }); }