Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed critical bug: process.env flattens all env-vars. #898

Merged
merged 2 commits into from
Dec 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/God/ClusterMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ module.exports = function ClusterMode(God) {
env_copy._pm2_version = pkg.version;

try {
clu = cluster.fork(env_copy);
// node.js cluster clients can not receive deep-level objects or arrays in the forked process, e.g.:
// { "args": ["foo", "bar"], "env": { "foo1": "bar1" }} will be parsed to
// { "args": "foo, bar", "env": "[object Object]"}
// So we passing a stringified JSON here.
clu = cluster.fork({pm2_env: JSON.stringify(env_copy)});
} catch(e) {
God.logAndGenerateError(e);
return cb(e);
Expand Down
2 changes: 1 addition & 1 deletion lib/God/ForkMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = function ForkMode(God) {
}

if (pm2_env.args) {
args = args.concat(eval((pm2_env.args)));
args = args.concat(pm2_env.args);
}

// piping stream o file
Expand Down
37 changes: 22 additions & 15 deletions lib/ProcessContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@ var cst = require('../constants');
var Utility = require('./Utility.js');
var axm = require('axm');

// Load all env-vars from master.
var pm2_env = JSON.parse(process.env.pm2_env);
for(var k in pm2_env){
process.env[k] = pm2_env[k];
}
delete process.env.pm2_env;

/**
* Main entrance to wrap the desired code
*/
(function ProcessContainer() {
var fs = require('fs');
var worker = require('cluster').worker;

var stdFile = process.env.pm_log_path;
var outFile = process.env.pm_out_log_path;
var errFile = process.env.pm_err_log_path;
var pmId = process.env.pm_id;
var pidFile = process.env.pm_pid_path;
var script = process.env.pm_exec_path;
var cronRestart = process.env.cron_restart;
var stdFile = pm2_env.pm_log_path;
var outFile = pm2_env.pm_out_log_path;
var errFile = pm2_env.pm_err_log_path;
var pmId = pm2_env.pm_id;
var pidFile = pm2_env.pm_pid_path;
var script = pm2_env.pm_exec_path;
var cronRestart = pm2_env.cron_restart;

/**
* Tell PM2 that error module is set
Expand All @@ -35,13 +42,13 @@ var axm = require('axm');
});

if (cst.MODIFY_REQUIRE)
require.main.filename = process.env.pm_exec_path;
require.main.filename = pm2_env.pm_exec_path;

fs.writeFileSync(pidFile, process.pid);

// Add args to process if args specified on start
if (process.env.args != null)
process.argv = process.argv.concat(eval(process.env.args));
process.argv = process.argv.concat(pm2_env.args);

// stdio, including: out, err and entire (both out and err if necessary).
var stds = {
Expand Down Expand Up @@ -102,7 +109,7 @@ function exec(script, stds) {

var moment = null;

if (process.env.log_date_format)
if (pm2_env.log_date_format)
moment = require('moment');

Utility.startLogging(stds, function (err) {
Expand All @@ -119,8 +126,8 @@ function exec(script, stds) {
process.stderr.write = (function(write) {
return function(string, encoding, fd) {
var log_data = string.toString();
if (process.env.log_date_format && moment)
log_data = moment().format(process.env.log_date_format) + ': ' + log_data;
if (pm2_env.log_date_format && moment)
log_data = moment().format(pm2_env.log_date_format) + ': ' + log_data;
stds.err.write && stds.err.write(log_data);
stds.std && stds.std.write && stds.std.write(log_data);
process.send({
Expand All @@ -134,8 +141,8 @@ function exec(script, stds) {
process.stdout.write = (function(write) {
return function(string, encoding, fd) {
var log_data = string.toString();
if (process.env.log_date_format && moment)
log_data = moment().format(process.env.log_date_format) + ': ' + log_data;
if (pm2_env.log_date_format && moment)
log_data = moment().format(pm2_env.log_date_format) + ': ' + log_data;
stds.out.write && stds.out.write(log_data);
stds.std && stds.std.write && stds.std.write(log_data);
process.send({
Expand Down Expand Up @@ -182,7 +189,7 @@ function exec(script, stds) {
});

// Change dir to fix process.cwd
process.chdir(process.env.pm_cwd || process.env.PWD || p.dirname(script));
process.chdir(pm2_env.pm_cwd || process.env.PWD || p.dirname(script));

require('module')._load(script, null, true);
});
Expand Down
2 changes: 1 addition & 1 deletion test/programmatic/god.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getConf3() {
function getConf4() {
return Common.prepareAppConf({
script : path.resolve(process.cwd(), 'test/fixtures/args.js'),
args : "['-d', '-a']",
args : ['-d', '-a'],
instances : '1',
name : 'child'
});
Expand Down