Skip to content

Commit

Permalink
Parallel tool execution improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpauk committed Jun 27, 2022
1 parent 7a56fe5 commit f347e1c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
62 changes: 40 additions & 22 deletions server/core/CliWorker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const fs = require('fs').promises;
const fs = require('fs-extra');
const path = require('path');
const _ = require('lodash');
const readline = require('readline');

const ayncExit = new (require('./AsyncExit'))();//singleton
const jembaConnManager = new (require('../db/JembaConnManager'))();//singleton
const utils = require('./utils');

const JembaRunner = require('./JembaRunner');
Expand All @@ -13,7 +12,9 @@ class CliWorker {
constructor(config) {
this.config = config;
this.jembaRunner = new JembaRunner(config);
this.configDb = jembaConnManager.db['config'];

this.cmdHistoryPath = `${config.dataDir}/cmdHistory.json`;
this.cmdHistory = [];
}

async run() {
Expand Down Expand Up @@ -65,21 +66,42 @@ class CliWorker {
return true;
}

async loadCmdHistory() {
let loadedHistory = [];
if (await fs.pathExists(this.cmdHistoryPath)) {
loadedHistory = await fs.readFile(this.cmdHistoryPath, 'utf8');
loadedHistory = JSON.parse(loadedHistory);
}

const joinedHistory = this.cmdHistory.concat(loadedHistory);
const uniq = new Set();
const result = [];
for (const rec of joinedHistory) {
if (!uniq.has(rec.time)) {
result.push(rec);
uniq.add(rec.time);
}
}

result.sort((a, b) => b.time - a.time);

this.cmdHistory = result;
}

async saveCmdHistory() {
await this.loadCmdHistory();

await fs.writeFile(this.cmdHistoryPath, JSON.stringify(this.cmdHistory, null, 2));
}

async runTTY() {
return new Promise((resolve, reject) => { (async() => {
const db = this.configDb;
const table = 'cli';
await db.open({table});
let cmdHistory = [];
const rows = await db.select({table, where: '@@id(1)'});
if (rows.length)
cmdHistory = rows[0].data;
await this.loadCmdHistory();

readline.emitKeypressEvents(process.stdin);

process.stdin.setRawMode(true);


let cmd = '';
let multiCmd = [];
let multiOn = false;
Expand All @@ -97,7 +119,7 @@ class CliWorker {
let prevKey = '';
const filterHistory = () => {
fhIndex = 0;
const result = cmdHistory.filter((item) => (item.indexOf(cmd) == 0));
const result = this.cmdHistory.filter((rec) => (rec.cmd.indexOf(cmd) == 0)).map((rec) => rec.cmd);
if (result.length) {
if (result[0] !== cmd) {
result.unshift(cmd);
Expand Down Expand Up @@ -170,16 +192,12 @@ class CliWorker {
try {
writeln();
if (cmd.trim() != '') {
if (!cmdHistory.length || cmdHistory[0] !== cmd) {
cmdHistory.unshift(cmd);
while (cmdHistory.length > 1000)
cmdHistory.pop();

await db.insert({
table,
replace: true,
rows: [{id: 1, data: cmdHistory}]
});
if (!this.cmdHistory.length || this.cmdHistory[0].cmd !== cmd) {
this.cmdHistory.unshift({time: Date.now(), cmd});
while (this.cmdHistory.length > 1000)
this.cmdHistory.pop();

await this.saveCmdHistory();
}

if (!this.processConsoleCommand(cmd)) {
Expand Down
6 changes: 0 additions & 6 deletions server/db/jembaMigrations/config/001-create.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
module.exports = {
up: [
['create', {
table: 'cli'
}],
['create', {
table: 'webui'
}],
],
down: [
['drop', {
table: 'cli'
}],
['drop', {
table: 'webui'
}],
Expand Down
7 changes: 3 additions & 4 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ async function init() {
log('Initializing');

await fs.ensureDir(config.dataDir);

//connections
const jembaConnManager = new (require('./db/JembaConnManager'))();//singleton
await jembaConnManager.init(config, argv['auto-repair']);
}

function showHelp() {
Expand All @@ -47,6 +43,9 @@ Options:
}

async function mainWebUI() {
//connections
const jembaConnManager = new (require('./db/JembaConnManager'))();//singleton
await jembaConnManager.init(config, argv['auto-repair']);
}

async function mainCli() {
Expand Down

0 comments on commit f347e1c

Please sign in to comment.