Skip to content

Commit

Permalink
1. Make sure directories of logs and pods exist before logging. 2…
Browse files Browse the repository at this point in the history
…. Refactor: put `startLogging` into Common.
  • Loading branch information
Tjatse committed Nov 21, 2014
1 parent 6a2be4c commit bf32318
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 100 deletions.
56 changes: 56 additions & 0 deletions lib/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var util = require('util');
var cronJob = require('cron').CronJob;
var isBinary = require('isbinaryfile');

var async = require('async');
var UX = require('./CliUx.js');
var cst = require('../constants.js');
var extItps = require('./interpreter.json');
Expand All @@ -23,6 +24,61 @@ var InteractorDaemonizer = require('./Interactor/InteractorDaemonizer.js');

var Common = module.exports;

/**
* Start log outgoing messages
* @method startLogging
* @param {} callback
* @return
*/
Common.startLogging = function(stds, callback) {
// Make sure directories of `logs` and `pids` exist.
try {
['logs', 'pids'].forEach(function(n){
(function(_path){
!fs.existsSync(_path) && fs.mkdirSync(_path, '0755');
})(path.resolve(cst.PM2_ROOT_PATH, n))
});
}catch(err){
return callback(new Error('can not create directories (logs/pids):' + err.message));
}

// waterfall.
var flows = [];
// types of stdio, should be sorted as `std(entire log)`, `out`, `err`.
var types = Object.keys(stds).sort(function(x, y){
return -x.charCodeAt(0) + y.charCodeAt(0);
});

// Create write streams.
(function createWS(io){
if(io.length != 1){
return;
}
io = io[0];

// If `std` is a Stream type, try next `std`.
// compatible with `pm2 reloadLogs`
if(typeof stds[io] == 'object' && !isNaN(stds[io].fd)){
return createWS(types.splice(0, 1));
}

flows.push(function(next){
var file = stds[io];
stds[io] = fs.createWriteStream(file, {flags: 'a'})
.on('error', function(err){
next(err);
})
.on('open', function(){
next();
});
stds[io]._file = file;
});
createWS(types.splice(0, 1));
})(types.splice(0, 1));

async.waterfall(flows, callback);
}

/**
* Resolve app paths and replace missing values with defaults.
* @method resolveAppPaths
Expand Down
54 changes: 2 additions & 52 deletions lib/God/ForkMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

var log = require('debug')('pm2:god');
var fs = require('fs');
var async = require('async');
var cst = require('../../constants.js');
var moment = require('moment');
var Common = require('../Common');
Expand Down Expand Up @@ -74,56 +73,7 @@ module.exports = function ForkMode(God) {
stds.std = pm2_env.pm_log_path;
}

/**
* Description
* @method startLogging
* @param {} cb
* @return
*/
function startLogging(cb) {
// waterfall.
var flows = [];
// types of stdio, should be sorted as `std(entire log)`, `out`, `err`.
var types = Object.keys(stds).sort(function(x, y){
return -x.charCodeAt(0) + y.charCodeAt(0);
});

// Create write streams.
(function createWS(io){
if(io.length != 1){
return;
}
io = io[0];

// If `std` is a Stream type, try next `std`.
// compatible with `pm2 reloadLogs`
if(typeof stds[io] == 'object' && !isNaN(stds[io].fd)){
return createWS(types.splice(0, 1));
}

flows.push(function(next){
var file = stds[io];
stds[io] = fs.createWriteStream(file, {flags: 'a'})
.on('error', function(e){
next(e);
})
.on('open', function(){
next();
});
stds[io]._file = file;
});
createWS(types.splice(0, 1));
})(types.splice(0, 1));

async.waterfall(flows, function(err, result){
if (err){
God.logAndGenerateError(err);
}
cb(err);
});
}

startLogging(function(err) {
Common.startLogging(stds, function(err, result) {
if (err) {
God.logAndGenerateError(err);
return cb(err);
Expand Down Expand Up @@ -219,7 +169,7 @@ module.exports = function ForkMode(God) {
stds[k] = stds[k]._file;
}
cspr.removeAllListeners();
startLogging(cb);
Common.startLogging(stds, cb);
};

cspr.unref();
Expand Down
56 changes: 8 additions & 48 deletions lib/ProcessContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ if (process.env.name != null)

var fs = require('fs');
var p = require('path');
var async = require('async');
var cst = require('../constants');
var Common = require('./Common');
var axm = require('axm');

/**
Expand Down Expand Up @@ -88,8 +88,12 @@ function exec(script, stds) {
stds[k].close();
stds[k] = stds[k]._file;
}
startLogging(function () {
console.log('Reloading log...');
Common.startLogging(stds, function (err) {
if(err){
console.error('Failed to reload logs:', err.stack);
}else {
console.log('Reloading log...');
}
});
}
});
Expand All @@ -99,51 +103,7 @@ function exec(script, stds) {
if (process.env.log_date_format)
moment = require('moment');


/**
* Description
* @method startLogging
* @param {} callback
* @return
*/
function startLogging(callback) {
// waterfall.
var flows = [];
// types of stdio, should be sorted as `std(entire log)`, `out`, `err`.
var types = Object.keys(stds).sort(function(x, y){
return -x.charCodeAt(0) + y.charCodeAt(0);
});

// Create write streams.
(function createWS(io){
if(io.length != 1){
return;
}
io = io[0];

// If `std` is a Stream type, try next `std`.
// compatible with `pm2 reloadLogs`
if(typeof stds[io] == 'object' && !isNaN(stds[io].fd)){
return createWS(types.splice(0, 1));
}

flows.push(function(next){
var file = stds[io];
stds[io] = fs.createWriteStream(file, {flags: 'a'})
.on('error', function(e){
next(e);
}).on('open', function(){
next();
});
stds[io]._file = file;
});
createWS(types.splice(0, 1));
})(types.splice(0, 1));

async.waterfall(flows, callback);
}

startLogging(function (err) {
Common.startLogging(stds, function (err) {
if (err) {
process.send({
type : 'process:exception',
Expand Down

0 comments on commit bf32318

Please sign in to comment.