pm2/lib/Common.js
Loïc Mahieu cbb5acacb9 Add ability to specify env variables in JSON
By not overwriting the env object in the JSON, it allow us to specify
env variables directly in the JSON.
2014-01-02 18:53:01 +01:00

114 lines
2.9 KiB
JavaScript

/**
* Module dependencies
*/
var fs = require('fs');
var path = p = require('path');
var util = require('util');
var cronJob = require('cron').CronJob;
var cst = require('../constants.js');
/**
* Common methods (used by CLI and God)
*/
var Common = module.exports;
/**
* Resolve app paths and replace missing values with defaults.
* @param app {Object}
* @param [cwd] {string} Optional cwd for app. Defaults to `process.cwd()`.
* @param [outputter] {function} Optional function which will receive non-error log messages.
* @returns {Object}
*/
Common.resolveAppPaths = function(app, cwd, outputter) {
var err = Common.validateApp(app, outputter);
if (err)
return err;
cwd = cwd || process.cwd();
app.env = app.env || {};
app.env.pm_cwd = cwd;
if (!('exec_mode' in app)) app['exec_mode'] = 'cluster_mode';
app["pm_exec_path"] = path.resolve(cwd, app.script);
delete app.script;
if (!app["name"]) {
app["name"] = p.basename(app["pm_exec_path"]);
}
if (fs.existsSync(app.pm_exec_path) == false) {
return new Error('script not found : ' + app.pm_exec_path);
}
if (app.out_file)
app["pm_out_log_path"] = path.resolve(cwd, app.out_file);
else {
if (!app.name) {
return new Error('You havent specified log path, please specify at least a "name" field in the JSON');
}
app["pm_out_log_path"] = path.resolve(cst.DEFAULT_LOG_PATH, [app.name, '-out.log'].join(''));
app.out_file = app["pm_out_log_path"];
}
delete app.out_file;
if (app.error_file)
app["pm_err_log_path"] = path.resolve(cwd, app.error_file);
else {
app["pm_err_log_path"] = path.resolve(cst.DEFAULT_LOG_PATH, [app.name, '-err.log'].join(''));
app.error_file = app["pm_err_log_path"];
}
delete app.error_file;
if (app.pid_file)
app["pm_pid_path"] = path.resolve(cwd, app.pid_file);
else {
app["pm_pid_path"] = path.resolve(cst.DEFAULT_PID_PATH, [app.name, '.pid'].join(''));
app.pid_file = app["pm_pid_path"];
}
delete app.pid_file;
// Set current env
util._extend(app.env, process.env);
//set port env variable
if (app.port) {
app.env.PORT = app.port;
}
return app;
};
Common.validateApp = function(appConf, outputter) {
var instances = appConf['instances'];
var script = appConf['script'];
var cron_pattern = appConf['cron_restart'];
if (instances && isNaN(parseInt(instances)) && instances != 'max') {
return new Error('Instance option must be an integer or the "max" string');
}
if (cron_pattern) {
try {
if (outputter)
outputter(cron_pattern);
var cron_test = new cronJob(cron_pattern, function() {
if (outputter)
outputter(cst.PREFIX_MSG + 'cron pattern for auto restart detected and valid');
delete cron_test;
});
} catch(ex) {
return new Error('Cron pattern is not valid !');
}
}
return null;
};