From 1b5dbc7c1862f0796c2d40339577f007d089b20a Mon Sep 17 00:00:00 2001 From: pbca26 Date: Wed, 15 Nov 2017 22:30:14 +0300 Subject: [PATCH] write/read daemon stdout --- routes/shepherd.js | 1 + routes/shepherd/daemonControl.js | 31 ++++++++++++++++++++++++++++--- routes/shepherd/debugLog.js | 31 +++++++++++++++++++++++++++++-- routes/shepherd/rpc.js | 20 ++++++++++---------- 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/routes/shepherd.js b/routes/shepherd.js index 222b6cf..6e3563c 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -31,6 +31,7 @@ shepherd.assetChainPorts = require('./ports.js'); shepherd._appConfig = require('./appConfig.js'); shepherd.coindInstanceRegistry = {}; +shepherd.coindStdout = {}; shepherd.guiLog = {}; shepherd.rpcConf = {}; shepherd.appRuntimeLog = []; diff --git a/routes/shepherd/daemonControl.js b/routes/shepherd/daemonControl.js index 3f35e9d..23ec48b 100644 --- a/routes/shepherd/daemonControl.js +++ b/routes/shepherd/daemonControl.js @@ -1,4 +1,5 @@ const spawn = require('child_process').spawn; +const fs = require('fs-extra'); module.exports = (shepherd) => { const getConf = (flock, coind) => { @@ -173,14 +174,30 @@ module.exports = (shepherd) => { let _arg = `${coindACParam}${data.ac_options.join(' ')}${_customParam}`; _arg = _arg.trim().split(' '); + const _daemonName = data.ac_name !== 'komodod' ? data.ac_name : 'komodod'; + const _daemonLogName = `${shepherd.agamaDir}/${_daemonName}.log`; + + try { + fs.accessSync(_daemonLogName, shepherd.fs.R_OK | shepherd.fs.W_OK); + shepherd.log(`created ${_daemonLogName}`); + fs.unlinkSync(_daemonLogName); + } catch (e) { + shepherd.log(`error accessing ${_daemonLogName}, doesnt exist?`); + } + if (!shepherd.appConfig.stopNativeDaemonsOnQuit) { + let spawnOut = fs.openSync(_daemonLogName, 'a'); + let spawnErr = fs.openSync(_daemonLogName, 'a'); + spawn(shepherd.komododBin, _arg, { - stdio: 'ignore', // piping all stdio to /dev/null + stdio: ['ignore', spawnOut, spawnErr], detached: true, }).unref(); } else { - shepherd.execFile(`${shepherd.komododBin}`, _arg, { - maxBuffer: 1024 * 1000000 // 1000 mb + let logStream = fs.createWriteStream(_daemonLogName, { flags: 'a' }); + + let _daemonChildProc = shepherd.execFile(`${shepherd.komododBin}`, _arg, { + maxBuffer: 1024 * 1000000, // 1000 mb }, (error, stdout, stderr) => { shepherd.writeLog(`stdout: ${stdout}`); shepherd.writeLog(`stderr: ${stderr}`); @@ -198,6 +215,14 @@ module.exports = (shepherd) => { } } }); + + _daemonChildProc.stdout.on('data', (data) => { + // shepherd.log(`${_daemonName} stdout: \n${data}`); + }).pipe(logStream); + + _daemonChildProc.stderr.on('data', (data) => { + // shepherd.error(`${_daemonName} stderr:\n${data}`); + }).pipe(logStream); } } } else { diff --git a/routes/shepherd/debugLog.js b/routes/shepherd/debugLog.js index 74c41c1..5fd0c88 100644 --- a/routes/shepherd/debugLog.js +++ b/routes/shepherd/debugLog.js @@ -1,6 +1,6 @@ module.exports = (shepherd) => { /* - * type: GET + * type: POST * params: herd, lastLines */ shepherd.post('/debuglog', (req, res) => { @@ -52,6 +52,27 @@ module.exports = (shepherd) => { }); }); + shepherd.get('/coind/stdout', (req, res) => { + const _daemonName = req.query.chain !== 'komodod' ? req.query.chain : 'komodod'; + const _daemonLogName = `${shepherd.agamaDir}/${_daemonName}.log`; + + shepherd.readDebugLog(_daemonLogName, 'all') + .then((result) => { + const _obj = { + msg: 'success', + result: result, + }; + + res.end(JSON.stringify(_obj)); + }, (result) => { + const _obj = { + msg: 'error', + result: result, + }; + + res.end(JSON.stringify(_obj)); + }); + }); shepherd.readDebugLog = (fileLocation, lastNLines) => { return new shepherd.Promise( @@ -72,7 +93,13 @@ module.exports = (shepherd) => { } const lines = data.trim().split('\n'); - const lastLine = lines.slice(lines.length - lastNLines, lines.length).join('\n'); + let lastLine; + + if (lastNLines === 'all') { + lastLine = data.trim(); + } else { + lastLine = lines.slice(lines.length - lastNLines, lines.length).join('\n'); + } resolve(lastLine); }); diff --git a/routes/shepherd/rpc.js b/routes/shepherd/rpc.js index 6373bb2..40d55e4 100644 --- a/routes/shepherd/rpc.js +++ b/routes/shepherd/rpc.js @@ -97,8 +97,8 @@ module.exports = (shepherd) => { shepherd.readDebugLog(coindDebugLogLocation, 1) .then((result) => { const _obj = { - 'msg': 'success', - 'result': result, + msg: 'success', + result: result, }; // shepherd.log('bitcoinrpc debug ====>'); @@ -127,15 +127,15 @@ module.exports = (shepherd) => { } let _body = { - 'agent': 'bitcoinrpc', - 'method': _cmd, + agent: 'bitcoinrpc', + method: _cmd, }; if (req.body.payload.params) { _body = { - 'agent': 'bitcoinrpc', - 'method': _cmd, - 'params': req.body.payload.params === ' ' ? [''] : req.body.payload.params, + agent: 'bitcoinrpc', + method: _cmd, + params: req.body.payload.params === ' ' ? [''] : req.body.payload.params, }; } @@ -144,10 +144,10 @@ module.exports = (shepherd) => { url: `http://localhost:${shepherd.rpcConf[req.body.payload.chain].port}`, method: 'POST', auth: { - 'user': shepherd.rpcConf[req.body.payload.chain].user, - 'pass': shepherd.rpcConf[req.body.payload.chain].pass + user: shepherd.rpcConf[req.body.payload.chain].user, + pass: shepherd.rpcConf[req.body.payload.chain].pass, }, - body: JSON.stringify(_body) + body: JSON.stringify(_body), }; // send back body on both success and error