From 9bba37404ac454a4843a79cfc521723d6dfef71a Mon Sep 17 00:00:00 2001 From: timbrinded <79199034+timbrinded@users.noreply.github.com> Date: Wed, 1 Nov 2023 18:35:17 +0000 Subject: [PATCH] switch nodes in tail zombie logs --- .changeset/sweet-bats-punch.md | 1 + packages/cli/src/cmds/runNetwork.ts | 190 ++++++++++++------ .../cli/src/internal/cmdFunctions/tempLogs.ts | 4 +- packages/cli/src/lib/globalContext.ts | 7 +- 4 files changed, 132 insertions(+), 70 deletions(-) diff --git a/.changeset/sweet-bats-punch.md b/.changeset/sweet-bats-punch.md index fe80fdbd..2383fea8 100644 --- a/.changeset/sweet-bats-punch.md +++ b/.changeset/sweet-bats-punch.md @@ -4,3 +4,4 @@ Fixes for tanssi - [#295](https://github.com/Moonsong-Labs/moonwall/issues/295) +- [#297](https://github.com/Moonsong-Labs/moonwall/issues/297) \ No newline at end of file diff --git a/packages/cli/src/cmds/runNetwork.ts b/packages/cli/src/cmds/runNetwork.ts index a57a202a..0e14a5ae 100644 --- a/packages/cli/src/cmds/runNetwork.ts +++ b/packages/cli/src/cmds/runNetwork.ts @@ -356,94 +356,154 @@ const resolveTestChoice = async (env: Environment, silent: boolean = false) => { const resolveTailChoice = async (env: Environment) => { let tailing: boolean = true; + let zombieNodePointer: number = 0; + let bottomBarContents = ""; + let switchNode: boolean; + let zombieNodes: string[] | undefined; + const resumePauseProse = [ - `, ${chalk.bgWhite.black("[p]")} - pause tail\n`, - `, ${chalk.bgWhite.black("[r]")} - resume tail\n`, + `, ${chalk.bgWhite.black("[p]")} - pause tail`, + `, ${chalk.bgWhite.black("[r]")} - resume tail`, ]; - const bottomBarContents = `📜 Tailing Logs, commands: ${chalk.bgWhite.black( + + const bottomBarBase = `📜 Tailing Logs, commands: ${chalk.bgWhite.black( "[q]" )} - quit, ${chalk.bgWhite.black("[t]")} - test, ${chalk.bgWhite.black("[g]")} - grep test`; + bottomBarContents = bottomBarBase + resumePauseProse[0]; const ui = new inquirer.ui.BottomBar({ - bottomBar: bottomBarContents + resumePauseProse[0], + bottomBar: bottomBarContents + "\n", }); - await new Promise(async (resolve) => { - const onData = (chunk: any) => ui.log.write(chunk.toString()); - const logFilePath = process.env.MOON_MONITORED_NODE - ? process.env.MOON_MONITORED_NODE - : process.env.MOON_LOG_LOCATION; + for (;;) { + if (process.env.MOON_ZOMBIE_NODES) { + zombieNodes = process.env.MOON_ZOMBIE_NODES + ? process.env.MOON_ZOMBIE_NODES.split("|") + : undefined; - // eslint-disable-next-line prefer-const - let currentReadPosition = 0; + bottomBarContents = + bottomBarBase + + resumePauseProse[0] + + `, ${chalk.bgWhite.black("[,]")} Next Log, ${chalk.bgWhite.black( + "[.]" + )} Previous Log | CurrentLog: ${`${zombieNodes[zombieNodePointer]} (${ + zombieNodePointer + 1 + }/${zombieNodes.length})`}`; + ui.updateBottomBar(bottomBarContents + "\n"); + } - const printLogs = (newReadPosition: number, currentReadPosition: number) => { - const stream = fs.createReadStream(logFilePath, { - start: currentReadPosition, - end: newReadPosition, - }); - stream.on("data", onData); - stream.on("end", () => { - currentReadPosition = newReadPosition; - }); - }; + switchNode = false; + await new Promise(async (resolve) => { + const onData = (chunk: any) => ui.log.write(chunk.toString()); + const logFilePath = process.env.MOON_MONITORED_NODE + ? process.env.MOON_MONITORED_NODE + : process.env.MOON_LOG_LOCATION; + + // eslint-disable-next-line prefer-const + let currentReadPosition = 0; + + const printLogs = (newReadPosition: number, currentReadPosition: number) => { + const stream = fs.createReadStream(logFilePath, { + start: currentReadPosition, + end: newReadPosition, + }); + stream.on("data", onData); + stream.on("end", () => { + currentReadPosition = newReadPosition; + }); + }; + + const readLog = () => { + const stats = fs.statSync(logFilePath); + const newReadPosition = stats.size; + + if (newReadPosition > currentReadPosition && tailing) { + printLogs(newReadPosition, currentReadPosition); + } + }; - const readLog = () => { - const stats = fs.statSync(logFilePath); - const newReadPosition = stats.size; + const incrPtr = () => { + zombieNodePointer = (zombieNodePointer + 1) % zombieNodes.length; + }; - if (newReadPosition > currentReadPosition && tailing) { - printLogs(newReadPosition, currentReadPosition); - } - }; + const decrPtr = () => { + zombieNodePointer = (zombieNodePointer - 1) % zombieNodes.length; + }; - printLogs(fs.statSync(logFilePath).size, 0); + printLogs(fs.statSync(logFilePath).size, 0); - const handleInputData = async (key: any) => { - ui.rl.input.pause(); - const char = key.toString().trim(); + const renderBottomBar = (...parts: any[]) => { + ui.updateBottomBar(bottomBarBase + " " + parts?.join(" ") + "\n"); + }; - if (char === "p") { - tailing = false; - ui.updateBottomBar(bottomBarContents + resumePauseProse[1]); - } + const handleInputData = async (key: any) => { + ui.rl.input.pause(); + const char = key.toString().trim(); - if (char === "r") { - printLogs(fs.statSync(logFilePath).size, currentReadPosition); - tailing = true; - ui.updateBottomBar(bottomBarContents + resumePauseProse[0]); - } + if (char === "p") { + tailing = false; + renderBottomBar(resumePauseProse[1]); + } - if (char === "q") { - ui.rl.input.removeListener("data", handleInputData); - ui.rl.input.pause(); - fs.unwatchFile(logFilePath); - resolve(""); - } + if (char === "r") { + printLogs(fs.statSync(logFilePath).size, currentReadPosition); + tailing = true; + renderBottomBar(resumePauseProse[0]); + } - if (char === "t") { - await resolveTestChoice(env, true); - ui.updateBottomBar(bottomBarContents + resumePauseProse[tailing ? 0 : 1]); - } + if (char === "q") { + ui.rl.input.removeListener("data", handleInputData); + ui.rl.input.pause(); + fs.unwatchFile(logFilePath); + resolve(""); + } - if (char === "g") { - ui.rl.input.pause(); - tailing = false; - await resolveGrepChoice(env, true); - ui.updateBottomBar(bottomBarContents + resumePauseProse[tailing ? 0 : 1]); - tailing = true; - ui.rl.input.resume(); - } + if (char === "t") { + await resolveTestChoice(env, true); + renderBottomBar(resumePauseProse[tailing ? 0 : 1]); + } - ui.rl.input.resume(); - }; + if (char === ",") { + ui.rl.input.removeListener("data", handleInputData); + ui.rl.input.pause(); + fs.unwatchFile(logFilePath); + switchNode = true; + incrPtr(); + resolve(""); + } - ui.rl.input.on("data", handleInputData); + if (char === ".") { + ui.rl.input.removeListener("data", handleInputData); + ui.rl.input.pause(); + fs.unwatchFile(logFilePath); + switchNode = true; + decrPtr(); + resolve(""); + } + + if (char === "g") { + ui.rl.input.pause(); + tailing = false; + await resolveGrepChoice(env, true); + renderBottomBar(resumePauseProse[tailing ? 0 : 1]); + tailing = true; + ui.rl.input.resume(); + } - fs.watchFile(logFilePath, () => { - readLog(); + ui.rl.input.resume(); + }; + + ui.rl.input.on("data", handleInputData); + + fs.watchFile(logFilePath, () => { + readLog(); + }); }); - }); + + if (!switchNode) { + break; + } + } ui.close(); }; diff --git a/packages/cli/src/internal/cmdFunctions/tempLogs.ts b/packages/cli/src/internal/cmdFunctions/tempLogs.ts index 7e8e0553..4c1e8eb1 100644 --- a/packages/cli/src/internal/cmdFunctions/tempLogs.ts +++ b/packages/cli/src/internal/cmdFunctions/tempLogs.ts @@ -22,7 +22,9 @@ export function reportLogLocation(silent: boolean = false) { let consoleMessage = ""; let filePath = ""; try { - filePath = process.env.MOON_LOG_LOCATION + filePath = process.env.MOON_ZOMBIE_DIR + ? process.env.MOON_ZOMBIE_DIR + : process.env.MOON_LOG_LOCATION ? process.env.MOON_LOG_LOCATION : path.join(dirPath, result.find((file) => path.extname(file) == ".log")!); consoleMessage = ` 🪵 Log location: ${filePath}`; diff --git a/packages/cli/src/lib/globalContext.ts b/packages/cli/src/lib/globalContext.ts index e7bbb282..9e220955 100644 --- a/packages/cli/src/lib/globalContext.ts +++ b/packages/cli/src/lib/globalContext.ts @@ -164,8 +164,6 @@ export class MoonwallContext { const network = await zombie.start("", zombieConfig, { logType: "silent" }); process.env.MOON_RELAY_WSS = network.relay[0].wsUri; process.env.MOON_PARA_WSS = Object.values(network.paras)[0].nodes[0].wsUri; - process.env.MOON_ZOMBIE_PATH = network.client.tmpDir; - if ( env.foundation.type == "zombie" && env.foundation.zombieSpec.monitoredNode && @@ -174,6 +172,7 @@ export class MoonwallContext { process.env.MOON_MONITORED_NODE = `${network.tmpDir}/${env.foundation.zombieSpec.monitoredNode}.log`; } const nodeNames = Object.keys(network.nodesByName); + process.env.MOON_ZOMBIE_DIR = `${network.tmpDir}`; process.env.MOON_ZOMBIE_NODES = nodeNames.join("|"); const processIds = Object.values((network.client as any).processMap) @@ -254,10 +253,10 @@ export class MoonwallContext { if (this.foundation == "zombie") { let readStreams: any[]; if (!isOptionSet("disableLogEavesdropping")) { - console.log(`🦻 Eavesdropping on node logs at ${process.env.MOON_ZOMBIE_PATH}`); + console.log(`🦻 Eavesdropping on node logs at ${process.env.MOON_ZOMBIE_DIR}`); const zombieNodeLogs = process.env .MOON_ZOMBIE_NODES!.split("|") - .map((nodeName) => `${process.env.MOON_ZOMBIE_PATH}/${nodeName}.log`); + .map((nodeName) => `${process.env.MOON_ZOMBIE_DIR}/${nodeName}.log`); readStreams = zombieNodeLogs.map((logPath) => { const readStream = fs.createReadStream(logPath, { encoding: "utf8" });