Skip to content

Commit e731d93

Browse files
committed
Unitech#2541 fix setting node.js version and restart via configuration file
1 parent ad733d6 commit e731d93

File tree

6 files changed

+98
-39
lines changed

6 files changed

+98
-39
lines changed

examples/custom-nodejs-version/ecosystem.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
{
99
name : "API",
1010
script : "http.js",
11-
interpreter : "node@4.1.2"
11+
interpreter : "node@6.9.0"
1212
}
1313
]
1414
}

examples/custom-nodejs-version/http.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
var http = require('http');
33

4+
console.log(process.version);
5+
46
var server = http.createServer(function(req, res) {
57
res.writeHead(200);
68
res.end('hey');

lib/Common.js

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,44 @@ Common.sink.determineExecMode = function(app) {
310310
}
311311
};
312312

313+
var resolveNodeInterpreter = function(app) {
314+
if (app.exec_mode.indexOf('cluster') > -1) {
315+
Common.printError(cst.PREFIX_MSG_WARNING + chalk.bold.yellow('Choosing the Node.js version in cluster mode is not supported'));
316+
return false;
317+
}
318+
if (!process.env.NVM_DIR) {
319+
Common.printError(cst.PREFIX_MSG_ERR + chalk.red('NVM is not available in PATH'));
320+
Common.printError(cst.PREFIX_MSG_ERR + chalk.red('Fallback to node in PATH'));
321+
Common.printOut(cst.PREFIX_MSG_ERR + chalk.bold('Install NVM:\n$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash'));
322+
}
323+
else {
324+
var node_version = app.exec_interpreter.split('@')[1];
325+
var nvm_node_path;
326+
327+
if (semver.satisfies(node_version, '>= 0.12.0'))
328+
nvm_node_path = path.join(process.env.NVM_DIR, '/versions/node/v' + node_version + '/bin/node');
329+
else
330+
nvm_node_path = path.join(process.env.NVM_DIR, '/v' + node_version + '/bin/node');
331+
332+
try {
333+
fs.accessSync(nvm_node_path);
334+
} catch(e) {
335+
Common.printOut(cst.PREFIX_MSG + 'Installing Node v%s', node_version);
336+
var nvm_bin = path.join(process.env.NVM_DIR, 'nvm.sh');
337+
var nvm_cmd = '. ' + nvm_bin + ' ; nvm install ' + node_version;
338+
339+
Common.printOut(cst.PREFIX_MSG + 'Executing: %s', nvm_cmd);
340+
shelljs.exec(nvm_cmd);
341+
}
342+
343+
Common.printOut(cst.PREFIX_MSG + chalk.green.bold('Setting Node to v%s (path=%s)'),
344+
node_version,
345+
nvm_node_path);
346+
347+
app.exec_interpreter = nvm_node_path;
348+
}
349+
};
350+
313351
/**
314352
* Resolve interpreter
315353
*/
@@ -318,49 +356,14 @@ Common.sink.resolveInterpreter = function(app) {
318356
extName = path.extname(app.pm_exec_path),
319357
betterInterpreter = extItps[extName];
320358

321-
var thereIsNVMInstalled = false;
322-
323359
// No interpreter defined and correspondance in schema hashmap
324360
if (noInterpreter && betterInterpreter)
325361
app.exec_interpreter = betterInterpreter;
326362
// Else if no Interpreter detect if process is binary
327363
else if (noInterpreter)
328364
app.exec_interpreter = isBinary(app.pm_exec_path) ? 'none' : 'node';
329-
else if (app.exec_interpreter.indexOf('node@') > -1 ||
330-
app.node_version && thereIsNVMInstalled) {
331-
332-
if (!process.env.NVM_DIR) {
333-
Common.printError(cst.PREFIX_MSG_ERR + chalk.red('NVM is not available in PATH'));
334-
Common.printError(cst.PREFIX_MSG_ERR + chalk.red('Fallback to node in PATH'));
335-
Common.printOut(cst.PREFIX_MSG_ERR + chalk.bold('Install NVM:\n$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash'));
336-
}
337-
else {
338-
var node_version = app.exec_interpreter.split('@')[1];
339-
var nvm_node_path;
340-
341-
if (semver.satisfies(node_version, '>= 0.12.0'))
342-
nvm_node_path = path.join(process.env.NVM_DIR, '/versions/node/v' + node_version + '/bin/node');
343-
else
344-
nvm_node_path = path.join(process.env.NVM_DIR, '/v' + node_version + '/bin/node');
345-
346-
try {
347-
fs.accessSync(nvm_node_path);
348-
} catch(e) {
349-
Common.printOut(cst.PREFIX_MSG + 'Installing Node v%s', node_version);
350-
var nvm_bin = path.join(process.env.NVM_DIR, 'nvm.sh');
351-
var nvm_cmd = '. ' + nvm_bin + ' ; nvm install ' + node_version;
352-
353-
Common.printOut(cst.PREFIX_MSG + 'Executing: %s', nvm_cmd);
354-
shelljs.exec(nvm_cmd);
355-
}
356-
357-
Common.printOut(cst.PREFIX_MSG + chalk.green.bold('Setting Node to v%s (path=%s)'),
358-
node_version,
359-
nvm_node_path);
360-
361-
app.exec_interpreter = nvm_node_path;
362-
}
363-
}
365+
else if (app.exec_interpreter.indexOf('node@') > -1)
366+
resolveNodeInterpreter(app);
364367

365368
/**
366369
* Specific installed JS transpilers
@@ -496,6 +499,10 @@ Common.mergeEnvironmentVariables = function(app_env, env_name, deploy_conf) {
496499
delete current_conf.env;
497500
app.env.current_conf = current_conf;
498501

502+
// #2541 force resolution of node interpreter
503+
if (app.env.current_conf.exec_interpreter.indexOf('@') > -1)
504+
resolveNodeInterpreter(app.env.current_conf);
505+
499506
return app.env;
500507
}
501508

lib/God/ForkMode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = function ForkMode(God) {
5656
args = args.concat(process.env.PM2_NODE_OPTIONS.split(' '));
5757
}
5858

59-
if (interpreter === 'node') {
59+
if (interpreter === 'node' || interpreter.indexOf('node') > -1) {
6060
args.push(path.resolve(path.dirname(module.filename), '..', 'ProcessContainerFork.js'));
6161
}
6262
else

test/bash/nvm-node-version.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@ $pm2 start ecosystem.json
1616
sleep 1
1717

1818
OCC=$($pm2 prettylist | grep "exec_interpreter" | grep 'v4.6.0\|v6.7.0' | wc -l)
19+
[ $OCC -eq 2 ] || fail "Errors in setting interpreters"
20+
success "Success"
21+
22+
$pm2 restart ecosystem.json
23+
24+
should 'should have 2 apps online' 'online' 2
25+
OCC=$($pm2 prettylist | grep "exec_interpreter" | grep 'v4.6.0\|v6.7.0' | wc -l)
26+
[ $OCC -eq 2 ] || fail "Errors in setting interpreters"
27+
success "Success"
28+
29+
$pm2 restart all
30+
sleep 0.5
31+
should 'should have 2 apps online' 'online' 2
32+
OCC=$($pm2 prettylist | grep "exec_interpreter" | grep 'v4.6.0\|v6.7.0' | wc -l)
33+
[ $OCC -eq 2 ] || fail "Errors in setting interpreters"
34+
success "Success"
35+
36+
# Update node.js version
37+
$pm2 restart ecosystem-change.json
38+
OCC=$($pm2 prettylist | grep "exec_interpreter" | grep 'v4.5.0\|v6.7.0' | wc -l)
39+
[ $OCC -eq 2 ] || fail "Errors in setting interpreters"
40+
success "Success"
1941

42+
$pm2 restart all
43+
sleep 0.5
44+
should 'should have 2 apps online' 'online' 2
45+
OCC=$($pm2 prettylist | grep "exec_interpreter" | grep 'v4.5.0\|v6.7.0' | wc -l)
2046
[ $OCC -eq 2 ] || fail "Errors in setting interpreters"
2147
success "Success"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
/**
3+
* Application configuration section
4+
* http://pm2.keymetrics.io/docs/usage/application-declaration/
5+
*/
6+
apps : [
7+
8+
// First application
9+
{
10+
name : "http-4.6",
11+
script : "http.js",
12+
interpreter : "[email protected]"
13+
},
14+
{
15+
name : "http-6.7",
16+
script : "http.js",
17+
force : true,
18+
env : {
19+
PORT : 8002
20+
},
21+
interpreter : "[email protected]"
22+
},
23+
]
24+
}

0 commit comments

Comments
 (0)