From 867620d95b82d9ba293f515efd9769ea101b9cbc Mon Sep 17 00:00:00 2001 From: gyroflaw <83718263+gyroflaw@users.noreply.github.com> Date: Thu, 22 Jun 2023 06:18:59 +0000 Subject: [PATCH 1/2] fix: exit parent process on daemon --- package.json | 1 + src/commands/node.ts | 38 +++++++++++++++++++++----------------- yarn.lock | 2 +- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index c1a9fc0..ef065bf 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@oclif/core": "^2", "@oclif/plugin-help": "^5", "@oclif/plugin-plugins": "^2.4.7", + "chokidar": "^3.5.3", "envsub": "^4.1.0", "kleur": "^4.1.5", "node-fetch": "^2.6.11", diff --git a/src/commands/node.ts b/src/commands/node.ts index 1c1bf3e..0c82bbd 100644 --- a/src/commands/node.ts +++ b/src/commands/node.ts @@ -1,6 +1,7 @@ import * as fs from "node:fs"; import * as path from "node:path"; import { spawn, execFile } from "node:child_process"; +import chokidar from "chokidar"; import { Args, Command, Flags, ux } from "@oclif/core"; import kleur from "kleur"; @@ -84,9 +85,17 @@ export default class Node extends Command { // start the casper-node process const casperNode = spawn(binaryPath, ["validator", configPath], { detached: flags.daemon, - // stdio: flags.daemon ? "ignore" : undefined, + stdio: [ + "ignore", + fs.openSync(workDir + "/stdout.log", "w"), + fs.openSync(workDir + "/stderr.log", "w"), + ], }); + if (flags.daemon) { + casperNode.unref(); + } + // log the error & close to console casperNode.on("error", (error) => { console.error(kleur.red(`[Casper node]: ${error.message}`)); @@ -94,21 +103,20 @@ export default class Node extends Command { casperNode.on("close", (code) => { console.error(kleur.red(`[Casper node]: Exited ${code}`)); - }); - - // setup the log files to write to - const stdoutFile = fs.createWriteStream(workDir + "/stdout.log", { - flags: "a", - }); - const stderrFile = fs.createWriteStream(workDir + "/stderr.log", { - flags: "a", + process.exit(code ?? undefined); }); let rpcStarted = false; let restStarted = false; let eventStreamStarted = false; - casperNode.stdout?.setEncoding("utf8"); - casperNode.stdout?.on("data", function (data) { + + const watcher = chokidar.watch(workDir + "/stdout.log", { + persistent: true, + }); + + watcher.on("change", (path) => { + const data = fs.readFileSync(path, "utf8"); + // started JSON RPC server; address=0.0.0.0:7777 // started REST server; address=0.0.0.0:8888 // started event stream server; address=0.0.0.0:9999 @@ -138,11 +146,8 @@ export default class Node extends Command { } }); - // log casper-node to temp dir for local use - casperNode.stdout?.pipe(stdoutFile); - casperNode.stderr?.pipe(stderrFile); - - if (casperNode.pid) { + // log pid for further stop + if (flags.daemon && casperNode.pid) { fs.writeFileSync(path.resolve(workDir, "../.pid"), `${casperNode.pid}`); } @@ -165,7 +170,6 @@ export default class Node extends Command { let timer: NodeJS.Timer; const exitNode = () => { if (rpcStarted && restStarted && eventStreamStarted) { - casperNode.unref(); clearTimeout(timer); process.exit(0); } diff --git a/yarn.lock b/yarn.lock index b823fcc..6e29d0b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1613,7 +1613,7 @@ check-error@^1.0.2: resolved "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz" integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII= -chokidar@3.5.3: +chokidar@3.5.3, chokidar@^3.5.3: version "3.5.3" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd" integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw== From a84cc877ca2187d082d7ff3d7d999d78e8a68956 Mon Sep 17 00:00:00 2001 From: gyroflaw <83718263+gyroflaw@users.noreply.github.com> Date: Thu, 22 Jun 2023 06:25:31 +0000 Subject: [PATCH 2/2] chore: sort import order --- src/commands/node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/node.ts b/src/commands/node.ts index 0c82bbd..639d5db 100644 --- a/src/commands/node.ts +++ b/src/commands/node.ts @@ -1,9 +1,9 @@ import * as fs from "node:fs"; import * as path from "node:path"; import { spawn, execFile } from "node:child_process"; -import chokidar from "chokidar"; import { Args, Command, Flags, ux } from "@oclif/core"; +import chokidar from "chokidar"; import kleur from "kleur"; import {