Skip to content

Commit

Permalink
make production future ready
Browse files Browse the repository at this point in the history
This commit does the following:

Production start should use the following command `npm run
start-producion` this first runs bower install and builds the front end
app(react). Then it will use the `pm2Start` script. This script will set
up the pm2 daemons to run loopback in cluster mode. This script also use
`production-start` script instead of the regular `server` script.

The reasons are two fold: to ensure `server` is run in es7 mode, and to
wait for handshake from DB or kill itself if no DB can be found within a
certain amount of time.
  • Loading branch information
Berkeley Martinez authored and Berkeley Martinez committed Jul 25, 2015
1 parent 00b81f4 commit c0eda90
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
},
"scripts": {
"start": "babel-node server/server.js",
"prestart-production": "bower cache clean && bower install && gulp build",
"start-production": "node pm2Start",
"lint": "eslint --ext=.js,.jsx .",
"test": "mocha",
"postinstall": "bower cache clean && bower install && gulp build"
"test": "mocha"
},
"license": "BSD-3-Clause",
"contributors": [
Expand Down
2 changes: 1 addition & 1 deletion pm2Start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var pm2 = require('pm2');
pm2.connect(function() {
pm2.start({
name: 'server',
script: 'server/server.js',
script: 'server/production-start.js',
'exec_mode': 'cluster',
instances: '2',
'max_memory_restart': '900M'
Expand Down
3 changes: 2 additions & 1 deletion server/debug-entry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// use this file with runners like node-debug
// or mocha.
require('babel/register');
var app = require('./server');

app.start();
31 changes: 31 additions & 0 deletions server/production-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// this ensures node understands the future
require('babel/register');

var startTime = Date.now();
var timeoutHandler;
// this is where server starts booting up
var app = require('./server');
console.log('waiting for db to connect');


var onConnect = function() {
console.log('db connected in %s ms', Date.now() - startTime);
if (timeoutHandler) {
clearTimeout(timeoutHandler);
}
app.start();
};

var timeoutHandler = setTimeout(function() {
var message =
'db did not after ' +
(Date.now() - startTime) +
' ms connect crashing hard';

console.log(message);
// purposely shutdown server
// pm2 should restart this in production
throw new Error(message);
}, 5000);

app.dataSources.db.on('connected', onConnect);
42 changes: 8 additions & 34 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ require('dotenv').load();
var pmx = require('pmx');
pmx.init();

var R = require('ramda'),
assign = require('lodash').assign,
var assign = require('lodash').assign,
loopback = require('loopback'),
boot = require('loopback-boot'),
accepts = require('accepts'),
Expand Down Expand Up @@ -199,7 +198,6 @@ app.use(
})
);

var startTime = Date.now();
boot(app, {
appRootDir: __dirname,
dev: process.env.NODE_ENV
Expand Down Expand Up @@ -249,7 +247,7 @@ var passportOptions = {
}
};

R.keys(passportProviders).map(function(strategy) {
Object.keys(passportProviders).map(function(strategy) {
var config = passportProviders[strategy];
config.session = config.session !== false;
passportConfigurator.configureProvider(
Expand Down Expand Up @@ -310,8 +308,6 @@ if (process.env.NODE_ENV === 'development') {
});
}

module.exports = app;

app.start = function() {
app.listen(app.get('port'), function() {
app.emit('started');
Expand All @@ -323,34 +319,12 @@ app.start = function() {
});
};

module.exports = app;

// start the server if `$ node server.js`
// in production use `$npm start-production`
// or `$node server/production` to start the server
// and wait for DB handshake
if (require.main === module) {
if (process.env.NODE_ENV === 'production') {
var timeoutHandler;
console.log('waiting for db to connect');

var onConnect = function() {
console.log('db connected in %s ms', Date.now() - startTime);
if (timeoutHandler) {
clearTimeout(timeoutHandler);
}
app.start();
};

var timeoutHandler = setTimeout(function() {
var message =
'db did not after ' +
(Date.now() - startTime) +
' ms connect crashing hard';

console.log(message);
// purposely shutdown server
// pm2 should restart this in production
throw new Error(message);
}, 5000);

app.dataSources.db.on('connected', onConnect);
} else {
app.start();
}
app.start();
}

0 comments on commit c0eda90

Please sign in to comment.