From 24e70fda2175175e89451f0e8937a02005f7e929 Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Fri, 30 Nov 2012 15:10:31 -0800 Subject: [PATCH] Find mongo and kill it dead on --once exit --- app/lib/mongo_runner.js | 1 + app/meteor/run.js | 59 ++++++++++++++++----------- packages/meteor/server_environment.js | 11 ++++- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/app/lib/mongo_runner.js b/app/lib/mongo_runner.js index df3f8f14ced..d5fae2a5dbc 100644 --- a/app/lib/mongo_runner.js +++ b/app/lib/mongo_runner.js @@ -181,3 +181,4 @@ exports.launch_mongo = function (app_dir, port, launch_callback, on_exit_callbac }; +exports._find_mongo_and_kill_it_dead = find_mongo_and_kill_it_dead; diff --git a/app/meteor/run.js b/app/meteor/run.js index 1f98616d1a5..e8fac46501c 100644 --- a/app/meteor/run.js +++ b/app/meteor/run.js @@ -14,28 +14,55 @@ var mongo_runner = require(path.join(__dirname, '..', 'lib', 'mongo_runner.js')) var _ = require(path.join(__dirname, '..', 'lib', 'third', 'underscore.js')); ////////// Globals ////////// +//XXX: Refactor to not have globals anymore? // list of log objects from the child process. var server_log = []; -var RestartingStatus = { +// port that mongo is running on +var mongo_port; + +var Status = { running: false, // is server running now? crashing: false, // does server crash whenever we start it? listening: false, // do we expect the server to be listening now. counter: 0, // how many crashes in rapid succession code: 0, + shouldRestart: true, + shuttingDown: false, + justCrash : function () { + var self = this; + log_to_clients({'exit': "Your application is exiting."}); + process.stdout.write("Attempting to kill mongo on " + mongo_port + "\n"); + self.shuttingDown = true; + _.defer(function() {mongo_runner._find_mongo_and_kill_it_dead(mongo_port, function (err) { + if (err) + process.stdout.write(err.reason + "\n"); + process.exit(self.code); + });}); + }, reset: function () { this.crashing = false; this.counter = 0; }, hard_crashed: function () { + var self = this; + if (!self.shouldRestart) { + self.justCrash(); + return; + } log_to_clients({'exit': "Your application is crashing. Waiting for file change."}); this.crashing = true; }, soft_crashed: function () { + var self = this; + if (!self.shouldRestart) { + self.justCrash(); + return; + } if (this.counter === 0) setTimeout(function () { this.counter = 0; @@ -49,28 +76,8 @@ var RestartingStatus = { } }; -var _justCrash = function () { - log_to_clients({'exit': "Your application is crashing."}); - process.exit(this.code); -}; - -var OnceStatus = { - running: false, // is server running now? - crashing: false, // does server crash whenever we start it? - listening: false, // do we expect the server to be listening now. - counter: 0, // how many crashes in rapid succession - reset: function () { - log_to_clients({'exit': "Meteor is exiting instead of restarting."}); - process.exit(0); - }, - hard_crashed: _justCrash, - - soft_crashed: _justCrash -}; - -var Status = RestartingStatus; // List of queued requests. Each item in the list is a function to run // when the inner app is ready to receive connections. @@ -212,7 +219,7 @@ var start_server = function (options) { }, options); if (options.runOnce) { - Status = OnceStatus; + Status.shouldRestart = false; } var env = {}; @@ -368,7 +375,7 @@ _.extend(DependencyWatcher.prototype, { return false; try { - var stats = fs.lstatSync(filepath) + var stats = fs.lstatSync(filepath); } catch (e) { // doesn't exist -- leave stats undefined } @@ -516,7 +523,7 @@ exports.DebugStatus = { exports.run = function (app_dir, bundle_opts, port, once, settings, dbg) { var outer_port = port || 3000; var inner_port = outer_port + 1; - var mongo_port = outer_port + 2; + mongo_port = outer_port + 2; var test_port = outer_port + 3; var bundle_path = path.join(app_dir, '.meteor', 'local', 'build'); var test_bundle_path = path.join(app_dir, '.meteor', 'local', 'build_test'); @@ -526,6 +533,7 @@ exports.run = function (app_dir, bundle_opts, port, once, settings, dbg) { var test_mongo_url = "mongodb://127.0.0.1:" + mongo_port + "/meteor_test"; var test_bundle_opts; + if (files.is_app_dir(app_dir)) { // If we're an app, make separate test_bundle_opts to trigger a // separate runner. @@ -684,6 +692,9 @@ exports.run = function (app_dir, bundle_opts, port, once, settings, dbg) { restart_server(); }, function (code, signal) { // On Mongo dead + if (Status.shuttingDown) { + return; + } console.log("Unexpected mongo exit code " + code + ". Restarting."); // if mongo dies 3 times with less than 5 seconds between each, diff --git a/packages/meteor/server_environment.js b/packages/meteor/server_environment.js index 6379c5f90fd..29192838323 100644 --- a/packages/meteor/server_environment.js +++ b/packages/meteor/server_environment.js @@ -1,6 +1,13 @@ Meteor = { isClient: false, - isServer: true, - settings: JSON.parse(process.env.METEOR_SETTINGS) + isServer: true }; +try { + Meteor.settings = JSON.parse(process.env.METEOR_SETTINGS); +} catch (e) { + // If the settings aren't JSON, just treat them as a string, or + // undefined, or whatever they are. + Meteor.settings = process.env.METEOR_SETTINGS; +} +