Browse Source

write/read daemon stdout

v0.25
pbca26 7 years ago
parent
commit
1b5dbc7c18
  1. 1
      routes/shepherd.js
  2. 31
      routes/shepherd/daemonControl.js
  3. 31
      routes/shepherd/debugLog.js
  4. 20
      routes/shepherd/rpc.js

1
routes/shepherd.js

@ -31,6 +31,7 @@ shepherd.assetChainPorts = require('./ports.js');
shepherd._appConfig = require('./appConfig.js'); shepherd._appConfig = require('./appConfig.js');
shepherd.coindInstanceRegistry = {}; shepherd.coindInstanceRegistry = {};
shepherd.coindStdout = {};
shepherd.guiLog = {}; shepherd.guiLog = {};
shepherd.rpcConf = {}; shepherd.rpcConf = {};
shepherd.appRuntimeLog = []; shepherd.appRuntimeLog = [];

31
routes/shepherd/daemonControl.js

@ -1,4 +1,5 @@
const spawn = require('child_process').spawn; const spawn = require('child_process').spawn;
const fs = require('fs-extra');
module.exports = (shepherd) => { module.exports = (shepherd) => {
const getConf = (flock, coind) => { const getConf = (flock, coind) => {
@ -173,14 +174,30 @@ module.exports = (shepherd) => {
let _arg = `${coindACParam}${data.ac_options.join(' ')}${_customParam}`; let _arg = `${coindACParam}${data.ac_options.join(' ')}${_customParam}`;
_arg = _arg.trim().split(' '); _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) { if (!shepherd.appConfig.stopNativeDaemonsOnQuit) {
let spawnOut = fs.openSync(_daemonLogName, 'a');
let spawnErr = fs.openSync(_daemonLogName, 'a');
spawn(shepherd.komododBin, _arg, { spawn(shepherd.komododBin, _arg, {
stdio: 'ignore', // piping all stdio to /dev/null stdio: ['ignore', spawnOut, spawnErr],
detached: true, detached: true,
}).unref(); }).unref();
} else { } else {
shepherd.execFile(`${shepherd.komododBin}`, _arg, { let logStream = fs.createWriteStream(_daemonLogName, { flags: 'a' });
maxBuffer: 1024 * 1000000 // 1000 mb
let _daemonChildProc = shepherd.execFile(`${shepherd.komododBin}`, _arg, {
maxBuffer: 1024 * 1000000, // 1000 mb
}, (error, stdout, stderr) => { }, (error, stdout, stderr) => {
shepherd.writeLog(`stdout: ${stdout}`); shepherd.writeLog(`stdout: ${stdout}`);
shepherd.writeLog(`stderr: ${stderr}`); 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 { } else {

31
routes/shepherd/debugLog.js

@ -1,6 +1,6 @@
module.exports = (shepherd) => { module.exports = (shepherd) => {
/* /*
* type: GET * type: POST
* params: herd, lastLines * params: herd, lastLines
*/ */
shepherd.post('/debuglog', (req, res) => { 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) => { shepherd.readDebugLog = (fileLocation, lastNLines) => {
return new shepherd.Promise( return new shepherd.Promise(
@ -72,7 +93,13 @@ module.exports = (shepherd) => {
} }
const lines = data.trim().split('\n'); 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); resolve(lastLine);
}); });

20
routes/shepherd/rpc.js

@ -97,8 +97,8 @@ module.exports = (shepherd) => {
shepherd.readDebugLog(coindDebugLogLocation, 1) shepherd.readDebugLog(coindDebugLogLocation, 1)
.then((result) => { .then((result) => {
const _obj = { const _obj = {
'msg': 'success', msg: 'success',
'result': result, result: result,
}; };
// shepherd.log('bitcoinrpc debug ====>'); // shepherd.log('bitcoinrpc debug ====>');
@ -127,15 +127,15 @@ module.exports = (shepherd) => {
} }
let _body = { let _body = {
'agent': 'bitcoinrpc', agent: 'bitcoinrpc',
'method': _cmd, method: _cmd,
}; };
if (req.body.payload.params) { if (req.body.payload.params) {
_body = { _body = {
'agent': 'bitcoinrpc', agent: 'bitcoinrpc',
'method': _cmd, method: _cmd,
'params': req.body.payload.params === ' ' ? [''] : req.body.payload.params, 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}`, url: `http://localhost:${shepherd.rpcConf[req.body.payload.chain].port}`,
method: 'POST', method: 'POST',
auth: { auth: {
'user': shepherd.rpcConf[req.body.payload.chain].user, user: shepherd.rpcConf[req.body.payload.chain].user,
'pass': shepherd.rpcConf[req.body.payload.chain].pass pass: shepherd.rpcConf[req.body.payload.chain].pass,
}, },
body: JSON.stringify(_body) body: JSON.stringify(_body),
}; };
// send back body on both success and error // send back body on both success and error

Loading…
Cancel
Save