Skip to content

Commit

Permalink
Merge branch 'feature/pm2-integration' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
gocreating committed Oct 14, 2016
2 parents ca4db0d + 879fe46 commit e427d7c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion configs/templates/Procfile
@@ -1 +1 @@
web: npm start
web: npm run pm2
1 change: 1 addition & 0 deletions gulpfile.js
Expand Up @@ -215,6 +215,7 @@ gulp.task('deploy', function(cb) {
'rm -rf ./configs',
'mkdir configs',
'cp -r ../configs/project ./configs/',
'cp ../configs/templates/Procfile ./',
'cp ../package.json ./',
'cp -r ../build ./',
], './.deploy', cbSeries);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -10,6 +10,7 @@
"build": "gulp build",
"mocha": "mocha specs/index.js --timeout 10000",
"start": "node build/server/server.js",
"pm2": "node ./build/server/pm2Entry.js",
"android": "npm run native-run-android && npm run native-watch-css",
"native-run-android": "node ./node_modules/react-native/local-cli/cli.js run-android",
"native-watch-css": "./node_modules/react-native-css/bin/react-native-css -i ./src/native/styles/index.scss -o ./src/native/styles/index.js --watch",
Expand Down Expand Up @@ -107,6 +108,7 @@
"object-assign": "^4.1.0",
"passport": "^0.3.2",
"passport-jwt": "^2.0.0",
"pm2": "^2.0.18",
"react": "^15.3.2",
"react-bootstrap": "^0.30.5",
"react-dom": "^15.0.2",
Expand Down
40 changes: 40 additions & 0 deletions src/server/pm2Entry.js
@@ -0,0 +1,40 @@
// ref: <http://pm2.keymetrics.io/docs/usage/use-pm2-with-cloud-providers/>
var pm2 = require('pm2');

var instances = process.env.WEB_CONCURRENCY || -1; // Set by Heroku or -1 to scale to max cpu core -1
var maxMemory = process.env.WEB_MEMORY || 512; // " " "

pm2.connect(function() {
pm2.start({
script: './build/server/server.js',
name: 'production-app', // ----> THESE ATTRIBUTES ARE OPTIONAL:
exec_mode: 'cluster', // ----> https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#schema
instances: instances,
max_memory_restart: maxMemory + 'M', // Auto restart if process taking more than XXmo
env: { // If needed declare some environment variables
NODE_ENV: 'production',
AWESOME_SERVICE_API_TOKEN: 'xxx',
},
}, function(err) {
if (err) {
return console.error(
'Error while launching applications',
err.stack || err
);
}
console.log('PM2 and application has been succesfully started');

// Display logs in standard output
pm2.launchBus(function(err, bus) {
console.log('[PM2] Log streaming started');

bus.on('log:out', function(packet) {
console.log('[App:%s] %s', packet.process.name, packet.data);
});

bus.on('log:err', function(packet) {
console.error('[App:%s][Err] %s', packet.process.name, packet.data);
});
});
});
});

0 comments on commit e427d7c

Please sign in to comment.