Skip to content

Commit

Permalink
[fix] handle exit and error events of tail process as well as add retry
Browse files Browse the repository at this point in the history
functionality
  • Loading branch information
jcrugzz committed Sep 10, 2013
1 parent 0bf8160 commit ffb2629
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions lib/solenoid.js
Expand Up @@ -390,24 +390,45 @@ var startApp = exports.startApp = function startApp(options, callback) {
fs.writeFile(options.pidFile, pid, next.bind(null, null));
},
function tailLog(next) {
var allLog = '',
var maxRetries = 3,
retries = 0,
json,
err;

//
// TODO: make this cleaner
//
function retry(e) {
if (++retries < maxRetries) {
return tailLog(next);
}
next(e);
}

logger.info('Tailing forza log: ' + startLog);
tail = spawn('tail', ['-f', startLog]);
tail.on('error', retry);
tail.on('exit', function (code) {
return code != 0
? retry(new Error('tailing start log exited poorly with code ' + code))
: next()
});
tail.stdout.pipe(concat(function (data) {

try { json = JSON.parse(allLog) }
catch (ex) { err = new Error('No start log data') }
try { json = JSON.parse(data) }
catch (ex) { err = new Error('No parseable data') }

// Use console.log to ensure it is JSON parseable
console.log(JSON.stringify(json));

try { tail.kill() }
catch (ex) { }

next(err);
//
// Remark: Do we want to handle this possible error case?
//
return err
? retry(err)
: next();
}));
}
], done);
Expand Down

0 comments on commit ffb2629

Please sign in to comment.