Skip to content

Commit e427d7c

Browse files
committed
Merge branch 'feature/pm2-integration' into dev
2 parents ca4db0d + 879fe46 commit e427d7c

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

configs/templates/Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: npm start
1+
web: npm run pm2

gulpfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ gulp.task('deploy', function(cb) {
215215
'rm -rf ./configs',
216216
'mkdir configs',
217217
'cp -r ../configs/project ./configs/',
218+
'cp ../configs/templates/Procfile ./',
218219
'cp ../package.json ./',
219220
'cp -r ../build ./',
220221
], './.deploy', cbSeries);

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build": "gulp build",
1111
"mocha": "mocha specs/index.js --timeout 10000",
1212
"start": "node build/server/server.js",
13+
"pm2": "node ./build/server/pm2Entry.js",
1314
"android": "npm run native-run-android && npm run native-watch-css",
1415
"native-run-android": "node ./node_modules/react-native/local-cli/cli.js run-android",
1516
"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",
@@ -107,6 +108,7 @@
107108
"object-assign": "^4.1.0",
108109
"passport": "^0.3.2",
109110
"passport-jwt": "^2.0.0",
111+
"pm2": "^2.0.18",
110112
"react": "^15.3.2",
111113
"react-bootstrap": "^0.30.5",
112114
"react-dom": "^15.0.2",

src/server/pm2Entry.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ref: <http://pm2.keymetrics.io/docs/usage/use-pm2-with-cloud-providers/>
2+
var pm2 = require('pm2');
3+
4+
var instances = process.env.WEB_CONCURRENCY || -1; // Set by Heroku or -1 to scale to max cpu core -1
5+
var maxMemory = process.env.WEB_MEMORY || 512; // " " "
6+
7+
pm2.connect(function() {
8+
pm2.start({
9+
script: './build/server/server.js',
10+
name: 'production-app', // ----> THESE ATTRIBUTES ARE OPTIONAL:
11+
exec_mode: 'cluster', // ----> https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#schema
12+
instances: instances,
13+
max_memory_restart: maxMemory + 'M', // Auto restart if process taking more than XXmo
14+
env: { // If needed declare some environment variables
15+
NODE_ENV: 'production',
16+
AWESOME_SERVICE_API_TOKEN: 'xxx',
17+
},
18+
}, function(err) {
19+
if (err) {
20+
return console.error(
21+
'Error while launching applications',
22+
err.stack || err
23+
);
24+
}
25+
console.log('PM2 and application has been succesfully started');
26+
27+
// Display logs in standard output
28+
pm2.launchBus(function(err, bus) {
29+
console.log('[PM2] Log streaming started');
30+
31+
bus.on('log:out', function(packet) {
32+
console.log('[App:%s] %s', packet.process.name, packet.data);
33+
});
34+
35+
bus.on('log:err', function(packet) {
36+
console.error('[App:%s][Err] %s', packet.process.name, packet.data);
37+
});
38+
});
39+
});
40+
});

0 commit comments

Comments
 (0)