-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from ric03uec/master
adding logs route for workers
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
var self = getLogs; | ||
module.exports = self; | ||
|
||
var async = require('async'); | ||
var fs = require('fs'); | ||
var readline = require('readline'); | ||
var path = require('path'); | ||
|
||
function getLogs(req, res) { | ||
var bag = { | ||
reqQuery: req.query, | ||
resBody: [], | ||
component: 'workers', | ||
skipRead: false | ||
}; | ||
|
||
bag.who = util.format('workers|%s', self.name); | ||
logger.info(bag.who, 'Starting'); | ||
|
||
async.series([ | ||
_checkInputParams.bind(null, bag), | ||
_checkFile.bind(null, bag), | ||
_get.bind(null, bag) | ||
], | ||
function (err) { | ||
logger.info(bag.who, 'Completed'); | ||
if (err) | ||
return respondWithError(res, err); | ||
|
||
sendJSONResponse(res, bag.resBody); | ||
} | ||
); | ||
} | ||
|
||
function _checkInputParams(bag, next) { | ||
var who = bag.who + '|' + _checkInputParams.name; | ||
logger.verbose(who, 'Inside'); | ||
|
||
bag.logsFile = path.join( | ||
global.config.runtimeDir, '/logs/', bag.component + '.log'); | ||
|
||
return next(); | ||
} | ||
|
||
function _checkFile(bag, next) { | ||
var who = bag.who + '|' + _checkFile.name; | ||
logger.verbose(who, 'Inside'); | ||
|
||
fs.stat(bag.logsFile, | ||
function (err) { | ||
if (err && err.code === 'ENOENT') | ||
bag.skipRead = true; | ||
else if (err) | ||
return next( | ||
new ActErr(who, ActErr.OperationFailed, | ||
'Failed to get logs for ' + bag.component + | ||
' with error: ' + util.inspect(err)) | ||
); | ||
|
||
return next(); | ||
} | ||
); | ||
} | ||
|
||
function _get(bag, next) { | ||
if (bag.skipRead) return next(); | ||
|
||
var who = bag.who + '|' + _get.name; | ||
logger.verbose(who, 'Inside'); | ||
|
||
var filereader = readline.createInterface({ | ||
input: fs.createReadStream(bag.logsFile), | ||
console: false | ||
}); | ||
|
||
filereader.on('line', | ||
function (line) { | ||
bag.resBody.push(line); | ||
} | ||
); | ||
|
||
filereader.on('close', | ||
function (err) { | ||
if (err) | ||
return next( | ||
new ActErr(who, ActErr.OperationFailed, | ||
'Failed to get logs for ' + bag.component + | ||
' with error: ' + util.inspect(err)) | ||
); | ||
|
||
return next(null); | ||
} | ||
); | ||
} |