Fix meteor reset on Windows

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
This commit is contained in:
Avital Oliver
2015-03-26 17:09:42 -07:00
parent 2b2b721ff4
commit 120febbf8a
2 changed files with 41 additions and 15 deletions

View File

@@ -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) {

View File

@@ -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) {
@@ -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.