Skip to content

Commit

Permalink
switch nodes in tail zombie logs
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Nov 1, 2023
1 parent 52b1d64 commit 9bba374
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 70 deletions.
1 change: 1 addition & 0 deletions .changeset/sweet-bats-punch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
190 changes: 125 additions & 65 deletions packages/cli/src/cmds/runNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
4 changes: 3 additions & 1 deletion packages/cli/src/internal/cmdFunctions/tempLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/src/lib/globalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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)
Expand Down Expand Up @@ -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" });
Expand Down

0 comments on commit 9bba374

Please sign in to comment.