Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lib/mongobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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) {
Expand All @@ -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();
});
Expand All @@ -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+']'))
});
}

Expand Down