Skip to content

Commit

Permalink
[minor] Small update to how forever works with pid files
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Dec 24, 2010
1 parent 57850e9 commit b7f792b
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ forever.cleanUp = function (cleanLogs) {
processes = getAllProcesses(true);

if (cleanLogs) forever.cleanLogsSync();
if (processes) {

if (processes && processes.length > 0) {
var checked = 0;
processes.forEach(function (proc) {
checkProcess(proc.pid, function (child) {
Expand Down Expand Up @@ -291,37 +291,40 @@ function formatProcess (proc, index, padding) {
// [findDead]: Optional parameter that indicates to return dead procs
//
function getAllProcesses (findDead) {
var processes = [];
try {
var files = fs.readdirSync(config.pidPath);
if (files.length === 0) return null;

var pidFiles = files.filter(function(file) { return /\.pid$/.test(file) });
pidFiles.forEach(function (file) {
var pidFile = path.join(config.pidPath, file),
pid = parseInt(fs.readFileSync(pidFile).toString());

try {
var child = JSON.parse(fs.readFileSync(path.join(config.pidPath, pid + '.fvr')));
processes.push(child);
}
catch (ex) {
if (findDead) {
processes.push({
pidFile: pidFile,
dead: true,
foreverPid: pid
});
}
var results = [], processes = {},
files = fs.readdirSync(config.pidPath);

if (files.length === 0) return null;

files.forEach(function (file) {
try {
var fullPath = path.join(config.pidPath, file),
data = fs.readFileSync(fullPath).toString();

switch (file.match(/\.(\w{3})$/)[1]) {
case 'pid':
var pid = parseInt(data);
if (!processes[pid]) processes[pid] = { foreverPid: pid };
break;

case 'fvr':
var child = JSON.parse(data);
processes[child.foreverPid] = child;
break;
}
});

return processes;
}
catch (ex) {
// Ignore errors
return null;
}
}
catch (ex) {
// Ignore errors
}
});

Object.keys(processes).forEach(function (key) {
if (!processes[key].pid && !findDead) return;
else if (!processes[key].pid) processes[key].dead = true;
results.push(processes[key]);
});

return results;
};

//
Expand Down

0 comments on commit b7f792b

Please sign in to comment.