Skip to content

Commit

Permalink
Fix meteor reset on Windows
Browse files Browse the repository at this point in the history
We were incorrectly always assuming that Meteor is running
by not actually trying to connect to the port that the
MONGO-PORT file reports Mongo is listening to.

The problem is that that file isn't cleared when Meteor
quits or crashes.

Fixes https://github.com/meteor/windows-preview/issues/138
  • Loading branch information
avital committed Mar 27, 2015
1 parent 2b2b721 commit 120febb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
1 change: 0 additions & 1 deletion tools/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,6 @@ main.registerCommand({

// XXX detect the case where Meteor is running the app, but
// MONGO_URL was set, so we don't see a Mongo process

var findMongoPort = require('./run-mongo.js').findMongoPort;
var isRunning = !! findMongoPort(options.appDir);
if (isRunning) {
Expand Down
55 changes: 41 additions & 14 deletions tools/run-mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,6 @@ if (process.platform === 'win32') {
// See if mongo is running already. Yields. Returns the port that
// mongo is running on or null if mongo is not running.
var findMongoPort = function (appDir) {
// On Windows, finding the Mongo pid, checking it and extracting the port is
// often unreliable. There is an easier way to find the port of running Mongo:
// look it up in a METEOR-PORT file that we generate when running. This may
// result into problems where we try to connect to a mongod that is not
// running, or a wrong mongod if our current app is not running but there is a
// left-over file lying around. This still can be better than always failing
// to connect.
if (process.platform === 'win32') {
var portFile = files.pathJoin(appDir, '.meteor/local/db/METEOR-PORT');
if (files.exists(portFile)) {
return files.readFile(portFile, 'utf8').replace(/\s/g, '');
}
}

var pids = findMongoPids(appDir);

if (pids.length !== 1) {
Expand All @@ -199,6 +185,47 @@ var findMongoPort = function (appDir) {
return pids[0].port;
};

// XXX actually -- the code below is probably more correct than the code we
// have above for non-Windows platforms (since that code relies on
// `findMongoPids`). But changing this a few days before the 1.1 release
// seemed too bold. But if you're changing code around here, consider using
// the implementation below on non-Windows platforms as well.
if (process.platform === 'win32') {
// On Windows, finding the Mongo pid, checking it and extracting the port
// is often unreliable (XXX reliable in what specific way?). There is an
// easier way to find the port of running Mongo: look it up in a METEOR-
// PORT file that we generate when running. This may result into problems
// where we try to connect to a mongod that is not running, or a wrong
// mongod if our current app is not running but there is a left-over file
// lying around. This still can be better than always failing to connect.
findMongoPort = function (appDir) {
var mongoPort = null;

var portFile = files.pathJoin(appDir, '.meteor/local/db/METEOR-PORT');
if (files.exists(portFile)) {
mongoPort = files.readFile(portFile, 'utf8').replace(/\s/g, '');
}

// Now, check if there really is a Mongo server running on this port.
// (The METEOR-PORT file may point to an old Mongo server that's now
// stopped)
var net = require('net');
var mongoTestConnectFuture = new Future;
var client = net.connect({port: mongoPort}, function() {
// The server is running.
client.end();
mongoTestConnectFuture.return();
});
client.on('error', function () {
mongoPort = null;
mongoTestConnectFuture.return();
});
mongoTestConnectFuture.wait();

return mongoPort;
}
}


// Kill any mongos running on 'port'. Yields, and returns once they
// are all dead. Throws an exception on failure.
Expand Down

0 comments on commit 120febb

Please sign in to comment.