wclr / ts-node-dev

Compiles your TS app and restarts when files are modified.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not loading app.ts

ghostlexly opened this issue · comments

Issue description

The app is not loaded when i require it in my main entry script.

I have 2 files:

  • server.ts - for running workers in cluster mode
  • app.ts - the file that serve the application data

Context

OS version (is it docker or host?), ts-node-dev version: Docker

Did you try to run with ts-node? : yes, it's working with ts-node

Did you try to run with --files option enabled? Yes

Did you try to run with --debug option enabled? Yes

Do you have a repro example (git repo) with simple steps to reproduce your problem?

const cluster = require("cluster");
const numCPUs = require("os").cpus().length;

/** Initiate a Cluster */
if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  // Fork worker processes
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on("online", (worker) => {
    console.log(`Worker ${worker.process.pid} is running`);
  });

  // Handle worker process exit and create a new one
  cluster.on("exit", (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died`);
    cluster.fork();
  });
} else {
  require("./app"); // this is not loaded
}

My cli command:

ts-node-dev --respawn --transpile-only -r tsconfig-paths/register src/server.ts

Debug log:

gistique-backend-1  | [INFO] 10:33:58 Restarting: /usr/src/app/src/server.ts has been modified
logistique-backend-1  | [DEBUG] 10:33:58 /usr/src/app/src/server.ts compiled in 12 ms
logistique-backend-1  | [DEBUG] 10:33:58 Removing all watchers from files
logistique-backend-1  | [DEBUG] 10:33:58 Child is already stopped, probably due to a previous error
logistique-backend-1  | [DEBUG] 10:33:58 Starting child process -r /tmp/ts-node-dev-hook-7154198465957726.js -r tsconfig-paths/register /usr/src/app/node_modules/ts-node-dev/lib/wrap.js src/server.ts
logistique-backend-1  | [DEBUG] 10:33:58 /usr/src/app/src/server.ts added to watcher
logistique-backend-1  | [DEBUG] 10:31:51 /usr/src/app/src/server.ts added to watcher
logistique-backend-1  | [DEBUG] 10:31:51 /usr/src/app/src/app.ts added to watcher

maybe this is a ts-node-dev's bug。

cluster.fork()’s process has trapped in a endless loop。detail:child-require-hook.ts#L62

ts-node-dev process struct:

  • Main process(ts-node-dev )
  • child process (detail: ts-node-dev fork)
    • your server.ts running in this process
  • cluster.fork()’s process(your server.ts fork)
    • when this process excute this code, Main process code can't recevie this signal,because this child process is ts-node-dev forked process,not your your server.ts forked process,so app.ts not compiled.