diff --git a/lib/forever.js b/lib/forever.js index f4798e09..1e776c8a 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -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) { @@ -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; }; //