Skip to content

Commit

Permalink
Merge pull request #16 from gyroflaw/fix/daemon-issue
Browse files Browse the repository at this point in the history
Fix daemon issue
  • Loading branch information
Ryo Kanazawa authored Jun 27, 2023
2 parents 4d4db9b + a84cc87 commit e2e4963
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 21 additions & 17 deletions src/commands/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from "node:path";
import { spawn, execFile } from "node:child_process";

import { Args, Command, Flags, ux } from "@oclif/core";
import chokidar from "chokidar";
import kleur from "kleur";

import {
Expand Down Expand Up @@ -84,31 +85,38 @@ 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}`));
});

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
Expand Down Expand Up @@ -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}`);
}

Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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=

[email protected]:
[email protected], chokidar@^3.5.3:
version "3.5.3"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd"
integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==
Expand Down

0 comments on commit e2e4963

Please sign in to comment.