Skip to content

Commit

Permalink
[api fix] When executing stopall, dont kill the current process. Refa…
Browse files Browse the repository at this point in the history
…ctor flow-control of forever.cleanUp()
  • Loading branch information
indexzero committed Jun 15, 2011
1 parent d681cb7 commit 3c8e6eb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 94 deletions.
54 changes: 0 additions & 54 deletions inspect-emitters.js

This file was deleted.

111 changes: 75 additions & 36 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,13 @@ forever.stop = function (target, format, restart) {
var procs = /(\d+)/.test(target)
? forever.findByIndex(target, processes)
: forever.findByScript(target, processes);

if (procs && procs.length > 0) {
procs.forEach(function (proc) {
try {
process.kill(proc.foreverPid);
process.kill(proc.pid);
}
catch (ex) { }
[proc.foreverPid, proc.pid].forEach(function (pid) {
try { process.kill(pid) }
catch (ex) { }
});
});

process.nextTick(function () {
Expand Down Expand Up @@ -278,7 +277,8 @@ forever.restart = function (target, format) {
// Finds the process with the specified index.
//
forever.findByIndex = function (index, processes) {
return processes && [processes[parseInt(index)]];
var proc = processes && processes[parseInt(index)];
return proc ? [proc] : null;
};

//
Expand Down Expand Up @@ -313,7 +313,9 @@ forever.stopAll = function (format) {

fPids.concat(cPids).forEach(function (pid) {
try {
process.kill(pid);
if (pid !== process.pid) {
process.kill(pid);
}
}
catch (ex) { }
});
Expand Down Expand Up @@ -374,38 +376,74 @@ forever.cleanUp = function (cleanLogs, allowManager) {
processes = getAllProcesses(true),
pidPath = forever.config.get('pidPath');

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

if (processes && processes.length > 0) {
function cleanProcess (proc, next) {
checkProcess(proc.pid, function (child) {
checkProcess(proc.foreverPid, function (manager) {
if (!child && !manager || (!child && manager && allowManager) || proc.dead) {
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();
});
});

return;
}

next();
function tryUnlink (file, next) {
fs.unlink(file, function () {
//
// Ignore errors (in case the file doesnt exist).
//
next();
});
}

function unlinkProcess (proc, done) {
var files = [
path.join(pidPath, proc.uid + '.fvr'),
path.join(pidPath, proc.uid + '.pid')
];

async.forEach(files, tryUnlink, function () {
if (cleanLogs && proc.logFile) {
return fs.unlink(proc.logFile, function () {
done();
});
}

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);
}

//
// 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();
}

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, cleanProcess, function () {
async.forEach(batch, checkProcess, function () {
return processes.length > 0
? cleanBatch(processes.splice(0, 10))
: emitter.emit('cleanUp');
Expand All @@ -429,13 +467,14 @@ forever.cleanUp = function (cleanLogs, allowManager) {
// that do not belong to current running forever processes.
//
forever.cleanLogsSync = function (processes) {
var files = fs.readdirSync(forever.config.get('root')),
var root = forever.config.get('root'),
files = fs.readdirSync(root),
running = processes && processes.filter(function (p) { return p && p.logFile }),
runningLogs = running && running.map(function (p) { return p.logFile.split('/').pop() });

files.forEach(function (file) {
if (/\.log$/.test(file) && (!runningLogs || runningLogs.indexOf(file) === -1)) {
fs.unlinkSync(path.join(forever.config.get('root'), file));
fs.unlinkSync(path.join(root, file));
}
});
};
Expand Down Expand Up @@ -493,7 +532,7 @@ forever.pidFilePath = function(pidFile) {
// #### @callback {function} Continuation to pass control backto.
// Utility function to check to see if a pid is running
//
function checkProcess (pid, callback) {
forever.checkProcess = function (pid, callback) {
if (!pid) {
return callback(false);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cli.exec = function (action, file, options) {
tidy.on('cleanUp', function () {
winston.silly(forever.config.get('root') + ' tidied.');

if (file && action !== 'set' && action !== 'clear') {
if (file && action === 'start') {
winston.info('Forever processing file: ' + file.grey);
}

Expand Down
15 changes: 12 additions & 3 deletions lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,19 @@ Monitor.prototype.start = function (restart) {
function letChildDie() {
self.running = false;

//
// If had to write to an stdout file, close it
if (self.stdout) self.stdout.end();
//
if (self.stdout) {
self.stdout.end();
}

//
// If had to write to an stderr file, close it
if (self.stderr) self.stderr.end();
//
if (self.stderr) {
self.stderr.end();
}

fs.unlink(self.fvrFile, function () {
self.emit('exit', self, spinning);
Expand All @@ -167,7 +176,7 @@ Monitor.prototype.start = function (restart) {
if (self.forceStop) {
letChildDie();
}
else if(spinning && typeof self.spinSleepTime !== 'number') {
else if (spinning && typeof self.spinSleepTime !== 'number') {
letChildDie();
}
else if (!self.forever && self.times >= self.max) {
Expand Down

0 comments on commit 3c8e6eb

Please sign in to comment.