Skip to content

Commit

Permalink
update heroku to use pm2
Browse files Browse the repository at this point in the history
  • Loading branch information
chaudhryjunaid committed Jan 16, 2016
1 parent 3d6ec5b commit d77eaab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: node app.js
web: node pm2-main.js
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"express": "^4.13.3",
"express-sequelize-session": "^0.4.0",
"express-session": "^1.12.1",
"forever": "latest",
"glob": "^6.0.1",
"grunt": "^0.4.5",
"grunt-cli": "^0.1.13",
Expand All @@ -48,6 +47,7 @@
"passport-local": "^1.0.0",
"passport-twitter": "^1.0.3",
"pg": "^4.4.3",
"pm2": "^1.0.0",
"proxyquire": "^1.7.3",
"sequelize": "^3.13.0",
"serve-favicon": "^2.3.0",
Expand Down
35 changes: 35 additions & 0 deletions pm2-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var pm2 = require('pm2');

var instances = process.env.WEB_CONCURRENCY || 1; // Set by Heroku or 1 (set -1 to scale to max cpu core - 1)
var maxMemory = process.env.WEB_MEMORY || 512; // 512 is the maximum free Dyno memory on Heroku

pm2.connect(function() {
pm2.start({
script : 'app.js',
name : 'msr', // ----> THESE ATTRIBUTES ARE OPTIONAL:
exec_mode : 'cluster', // set to 'cluster' for cluster execution
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"
}
}, function(err) {
if (err) return console.error('Error while launching applications', err.stack || err);
console.log('PM2 and application has been successfully 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 d77eaab

Please sign in to comment.