refactor(transaction-logger): replace log file write by append

## Problem
Doing read, then write each a trasaction or call exectues could get
heavy, especially with regular txs on
This was especially true in the tests, which led to deactivate the
tx logger in the tests
## Solution
Instead of reading the whole file, adding the JSON line a writing,
we instead just append some pseudo JSON to it that later gets read
and parsed correctly back to JSON
This commit is contained in:
Jonathan Rainville 2020-01-07 12:08:21 -05:00 committed by Iuri Matias
parent 6db8d8750a
commit 22f1f72897
1 changed files with 18 additions and 10 deletions

View File

@ -28,7 +28,7 @@ export default class TransactionLogger {
this.contractsConfig = embark.config.contractsConfig;
this.contractsDeployed = false;
this.outputDone = false;
this.logFile = dappPath(".embark", "contractLogs.json");
this.logFile = dappPath(".embark", "contractLogs.json.txt");
this.transactions = {};
this._listenForLogRequests();
@ -48,16 +48,14 @@ export default class TransactionLogger {
});
this.writeLogFile = async.cargo((tasks, callback) => {
// TODO change this to only read once then use memory, because it slows things down a lot to read on each TX
const data = this._readLogs();
let appendThis = '';
tasks.forEach(task => {
data[new Date().getTime()] = task;
appendThis += `"${new Date().getTime()}":${JSON.stringify(task)},\n`;
});
this.fs.writeJson(this.logFile, data, err => {
this.fs.appendFile(this.logFile, appendThis, (err) => {
if (err) {
console.error(err);
this.logger.error('Error writing to the log file', err.message);
this.logger.trace(err);
}
callback();
});
@ -266,8 +264,18 @@ export default class TransactionLogger {
_readLogs() {
this.fs.ensureFileSync(this.logFile);
try {
return JSON.parse(this.fs.readFileSync(this.logFile));
} catch (_error) {
let data = this.fs.readFileSync(this.logFile).toString();
if (!data) {
return {};
}
// remove last comma and add braces around
data = `{${data.substring(0, data.length - 2)}}`;
return JSON.parse(data);
} catch (error) {
this.logger.error('Error reading contract log file', error.message);
this.logger.trace(error);
return {};
}
}