diff --git a/inspect-emitters.js b/inspect-emitters.js deleted file mode 100644 index cd57bffd..00000000 --- a/inspect-emitters.js +++ /dev/null @@ -1,54 +0,0 @@ -(function (global, undefined) { - "use strict"; - - var path = require('path'); - - console.log("nodejs module path: ", require.paths); - console.log(""); - - //get the forever module - var forever = require("./lib/forever"), - emitter = path.join(__dirname, 'examples', 'server.js'), - child; - - console.log(""); - console.log("get the list:"); - console.log("forever.list(true)", forever.list(true)); - console.log("forever.list(false).length", (forever.list(false) || []).length); - - console.log(""); - console.log("Start emitter.js"); - - child = new (forever.Forever)(emitter, { options: ['-p', '8080'] }); - - //extend - child.__old__emit = child.emit; - child.emit = function () { - var eventname = arguments[0], - list; - - if (eventname != "newListener") { - list = forever.list(false) || []; - console.log(":: emiting: " + arguments[0]); - console.log(":: processors: ", list.length); - } - - //console.dir(new Error().stack.split('\n')); - return this.__old__emit.apply(this, arguments); - }; - - child.on("start", function () { - console.log("> emitter.js emits start"); - console.log("> > forever.list(true)", forever.list(true)); - console.log("> > forever.list(false).length", (forever.list(false) || []).length); - console.log("> stop emmiter.js"); - child.stop(); - }); - child.on("stop", function () { - console.log("> emitter.js emits stop"); - console.log("> > forever.list(true)", forever.list(true)); - console.log("> > forever.list(false).length", (forever.list(false) || []).length); - }); - child.start(); - -})(global); \ No newline at end of file diff --git a/lib/forever.js b/lib/forever.js index 244b3719..515af7bc 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -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 () { @@ -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; }; // @@ -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) { } }); @@ -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'); @@ -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)); } }); }; @@ -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); } diff --git a/lib/forever/cli.js b/lib/forever/cli.js index 11376489..2da79dba 100644 --- a/lib/forever/cli.js +++ b/lib/forever/cli.js @@ -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); } diff --git a/lib/forever/monitor.js b/lib/forever/monitor.js index f2146498..4fab274b 100644 --- a/lib/forever/monitor.js +++ b/lib/forever/monitor.js @@ -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); @@ -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) {