Skip to content

Commit

Permalink
[fix] Batch the cleaning of *.fvr and *.pid files to avoid file descr…
Browse files Browse the repository at this point in the history
…iptor overload. Fixes #53
  • Loading branch information
indexzero committed May 13, 2011
1 parent 828cd48 commit e18a256
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,34 +362,47 @@ forever.list = function (format, procs) {
//
forever.cleanUp = function (cleanLogs) {
var emitter = new events.EventEmitter(),
processes = getAllProcesses(true);
processes = getAllProcesses(true),
pidPath = forever.config.get('pidPath');

if (cleanLogs) forever.cleanLogsSync(processes);

if (processes && processes.length > 0) {
var checked = 0;
processes.forEach(function (proc) {
function cleanProcess (proc, next) {
checkProcess(proc.pid, function (child) {
checkProcess(proc.foreverPid, function (manager) {
if (!child && !manager || proc.dead) {
fs.unlink(path.join(forever.config.get('pidPath'), proc.uid + '.fvr'), function () {
fs.unlink(path.join(forever.config.get('pidPath'), proc.uid + '.pid'), function () {
fs.unlink(path.join(pidPath, proc.uid + '.fvr'), function () {
fs.unlink(path.join(pidPath, proc.uid + '.pid'), function () {
//
// Ignore errors
//
if (cleanLogs && proc.logFile) {
return fs.unlink(proc.logFile, function () {
next();
});
}

next();
});
});

if (cleanLogs && proc.logFile) {
fs.unlink(proc.logFile, function () { /* Ignore Errors */ });
}
return;
}

checked++;
if (checked === processes.length) {
emitter.emit('cleanUp');
}
next();
});
});
});
}

(function cleanBatch (batch) {
async.forEach(batch, cleanProcess, function () {
return processes.length > 0
? cleanBatch(processes.splice(0, 10))
: emitter.emit('cleanUp');
});
})(processes.splice(0, 10));

}
else {
process.nextTick(function () {
Expand Down

0 comments on commit e18a256

Please sign in to comment.