Skip to content

Commit

Permalink
[minor] Always try to parse the response before calling next()
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Dec 2, 2011
1 parent dcbfc70 commit d6c7590
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions lib/forever.js
Expand Up @@ -96,15 +96,41 @@ function getAllProcesses(callback) {
function getProcess(name, next) {
var fullPath = path.join(sockPath, name),
socket = new net.Socket({ type: 'unix' }),
parsed = false,
data = '';

function tryParse() {
if (!parsed) {
parsed = true;

var monitors;
try {
monitors = JSON.parse(data);
}
catch (ex) {
//
// Ignore errors
//
}

//
// Be a little lazier about loading results
//
if (monitors && monitors.monitors) {
results = results.concat(monitors.monitors);
}

next();
}
}

socket.on('error', function (err) {
if (err.code === 'ECONNREFUSED') {
try {
fs.unlinkSync(fullPath);
}
catch (ex) { }
return next();
return tryParse();
}
else if (err.code === 'EACCES') {
forever.log.warn('Error contacting: ' + fullPath.magenta);
Expand All @@ -113,33 +139,14 @@ function getAllProcesses(callback) {
forever.log.error('Unknown error (' + err.code + ') when contacting: ' + fullPath.magenta);
}

next();
tryParse();
});

socket.on('data', function (msg) {
data += msg;
});

socket.on('close', function () {
var monitors;
try {
monitors = JSON.parse(data);
}
catch (ex) {
//
// Ignore errors
//
}

//
// Be a little lazier about loading results
//
if (monitors && monitors.monitors) {
results = results.concat(monitors.monitors);
}

next();
});
socket.on('close', tryParse);

socket.connect(fullPath);
}
Expand Down

0 comments on commit d6c7590

Please sign in to comment.