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.coindInstanceRegistry = {};
shepherd.coindStdout = {};
shepherd.guiLog = {};
shepherd.rpcConf = {};
shepherd.appRuntimeLog = [];

31
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 {

31
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);
});

20
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

Loading…
Cancel
Save