Skip to content

Commit

Permalink
[api] Expose forever.columns and update forever.format to generat…
Browse files Browse the repository at this point in the history
…e results dynamically
  • Loading branch information
indexzero committed Jul 11, 2011
1 parent bc8153a commit 381ecaf
Showing 1 changed file with 87 additions and 45 deletions.
132 changes: 87 additions & 45 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ forever.load = function (options) {
//
// Setup the incoming options with default options.
//
options = options || {};
options.root = options.root || forever.root;
options.pidPath = options.pidPath || path.join(options.root, 'pids');
options = options || {};
options.root = options.root || forever.root;
options.pidPath = options.pidPath || path.join(options.root, 'pids');
options.sockPath = options.sockPath || path.join(options.root, 'sock');
options.columns = options.columns || [
'uid', 'command', 'script', 'forever', 'pid', 'logfile', 'uptime'
];

//
// If forever is initalized and the config directories are identical
Expand All @@ -83,6 +86,7 @@ forever.load = function (options) {
forever.config.set('root', options.root);
forever.config.set('pidPath', options.pidPath);
forever.config.set('sockPath', options.sockPath);
forever.config.set('columns', options.columns);

//
// Attempt to see if `forever` has been configured to
Expand Down Expand Up @@ -222,30 +226,16 @@ forever.startDaemon = function (script, options) {
forever.startServer = function () {
var monitors = Array.prototype.slice.call(arguments),
callback = typeof monitors[monitors.length - 1] == 'function' && monitors.pop(),
current = getData(),
socket = path.join(forever.config.get('sockPath'), 'forever.sock'),
server;

function getData () {
return monitors.map(function (m) { return m.data });
}

function formatProc (proc) {
var info = [
proc.command,
proc.file,
':',
proc.uid
].join(' ');

return '[' + info + ']'
}

server = net.createServer(function (socket) {
//
// Write the specified data and close the socket
//
socket.end(JSON.stringify({ monitors: getData() }));
socket.end(JSON.stringify({
monitors: monitors.map(function (m) { return m.data })
}));
});

portfinder.getSocket({ path: socket }, function (err, socket) {
Expand Down Expand Up @@ -446,45 +436,46 @@ forever.list = function (format, callback) {
});
};

//
// ### function format (format, procs)
// #### @format {Boolean} Value indicating if processes should be formatted
// #### @procs {Array} Processes to format
// Returns a formatted version of the `procs` supplied based on the column
// configuration in `forever.config`.
//
forever.format = function (format, procs) {
if (!procs || procs.length === 0) {
return null;
}

if (format) {
var index = 0, rows = [
[' ', 'uid', 'command ', 'script', 'forever ', 'pid', 'logfile', 'uptime']
];

var index = 0,
columns = forever.config.get('columns'),
rows = [[' '].concat(columns)];

function mapColumns (prefix, mapFn) {
return [prefix].concat(columns.map(mapFn));
}

//
// Iterate over the procs to see which has the
// longest options string
//
procs.forEach(function (proc) {
rows.push([
'[' + index + ']',
proc.uid,
(proc.command || 'node').grey,
[proc.file].concat(proc.options).join(' ').grey,
proc.foreverPid,
proc.pid,
proc.logFile ? proc.logFile.magenta : '',
timespan.fromDates(new Date(proc.ctime), new Date()).toString().yellow
]);

rows.push(mapColumns('[' + index + ']', function (column) {
return forever.columns[column]
? forever.columns[column].get(proc)
: 'MISSING';
}));

index++;
});

formatted = cliff.stringifyRows(rows, [
'white',
'grey',
'grey',
'grey',
'white',
'white',
'magenta',
'yellow'
]);
formatted = cliff.stringifyRows(rows, mapColumns('white', function (column) {
return forever.columns[column]
? forever.columns[column].color
: 'white';
}));
}

return format ? formatted : procs;
Expand Down Expand Up @@ -663,6 +654,57 @@ forever.checkProcess = function (pid, callback) {
});
};

//
// ### @columns {Object}
// Property descriptors for accessing forever column information
// through `forever list` and `forever.list()`
//
forever.columns = {
uid: {
color: 'white',
get: function (proc) {
return proc.uid;
}
},
command: {
color: 'grey',
get: function (proc) {
return (proc.command || 'node').grey;
}
},
script: {
color: 'grey',
get: function (proc) {
return [proc.file].concat(proc.options).join(' ').grey;
}
},
forever: {
color: 'white',
get: function (proc) {
return proc.foreverPid;
}
},
pid: {
color: 'white',
get: function (proc) {
return proc.pid;
}
},
logfile: {
color: 'magenta',
get: function (proc) {
return proc.logFile ? proc.logFile.magenta : '';
}
},
uptime: {
color: 'yellow',
get: function (proc) {
return timespan.fromDates(new Date(proc.ctime), new Date()).toString().yellow;
}
}
};


//
// ### function getAllProcess (callback)
// #### @callback {function} Continuation to respond to when complete.
Expand Down

0 comments on commit 381ecaf

Please sign in to comment.