Skip to content

Commit

Permalink
[fix] Update forever cleanlogs for 0.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jul 11, 2011
1 parent a39fee1 commit 581a132
Showing 1 changed file with 69 additions and 74 deletions.
143 changes: 69 additions & 74 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,82 +488,83 @@ forever.format = function (format, procs) {
//
forever.cleanUp = function (cleanLogs, allowManager) {
var emitter = new events.EventEmitter(),
processes = getAllProcesses(true),
pidPath = forever.config.get('pidPath');

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

if (processes && processes.length > 0) {
function unlinkProcess (proc, done) {
fs.unlink(path.join(pidPath, proc.uid + '.pid'), function () {
//
// Ignore errors (in case the file doesnt exist).
//
getAllProcesses(function (processes) {
if (cleanLogs) {
forever.cleanLogsSync(processes);
}

if (cleanLogs && proc.logFile) {
if (processes && processes.length > 0) {
function unlinkProcess (proc, done) {
fs.unlink(path.join(pidPath, proc.uid + '.pid'), function () {
//
// If we are cleaning logs then do so if the process
// has a logfile.
// Ignore errors (in case the file doesnt exist).
//
return fs.unlink(proc.logFile, function () {
done();
});
}

done();
});
}
if (cleanLogs && proc.logFile) {
//
// If we are cleaning logs then do so if the process
// has a logfile.
//
return fs.unlink(proc.logFile, function () {
done();
});
}

function cleanProcess (proc, done) {
if (proc.child && proc.manager) {
return done();
}
else if (!proc.child && !proc.manager
|| (!proc.child && proc.manager && allowManager)
|| proc.dead) {
return unlinkProcess(proc, done);
done();
});
}

//
// If we have a manager but no child, wait a moment
// in-case the child is currently restarting, but **only**
// if we have not already waited for this process
//
if (!proc.waited) {
proc.waited = true;
return setTimeout(function () {
checkProcess(proc, done);
}, 500);
function cleanProcess (proc, done) {
if (proc.child && proc.manager) {
return done();
}
else if (!proc.child && !proc.manager
|| (!proc.child && proc.manager && allowManager)
|| proc.dead) {
return unlinkProcess(proc, done);
}

//
// If we have a manager but no child, wait a moment
// in-case the child is currently restarting, but **only**
// if we have not already waited for this process
//
if (!proc.waited) {
proc.waited = true;
return setTimeout(function () {
checkProcess(proc, done);
}, 500);
}

done();
}

done();
}
function checkProcess (proc, next) {
forever.checkProcess(proc.pid, function (child) {
proc.child = child;
forever.checkProcess(proc.foreverPid, function (manager) {
proc.manager = manager;
cleanProcess(proc, next);
});
});
}

function checkProcess (proc, next) {
forever.checkProcess(proc.pid, function (child) {
proc.child = child;
forever.checkProcess(proc.foreverPid, function (manager) {
proc.manager = manager;
cleanProcess(proc, next);
(function cleanBatch (batch) {
async.forEach(batch, checkProcess, function () {
return processes.length > 0
? cleanBatch(processes.splice(0, 10))
: emitter.emit('cleanUp');
});
});
})(processes.splice(0, 10));
}

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

return emitter;
};
Expand Down Expand Up @@ -704,7 +705,6 @@ forever.columns = {
}
};


//
// ### function getAllProcess (callback)
// #### @callback {function} Continuation to respond to when complete.
Expand All @@ -718,7 +718,7 @@ function getAllProcesses (callback) {
if (sockets.length === 0) {
return callback();
}

function getProcess (name, next) {
var fullPath = path.join(sockPath, name),
socket = new net.Socket({ type: 'unix' });
Expand Down Expand Up @@ -753,15 +753,10 @@ function getAllProcesses (callback) {
// e.x. [{ pid: 12345, foreverPid: 12346 }, ...]
//
function getAllPids (processes) {
processes = processes || getAllProcesses();
if (processes) {
return processes.map(function (proc) {
return {
pid: proc.pid,
foreverPid: proc.foreverPid
}
});
}

return null;
return !processes ? null : processes.map(function (proc) {
return {
pid: proc.pid,
foreverPid: proc.foreverPid
}
});
};

0 comments on commit 581a132

Please sign in to comment.