1

I've been poking around the node environment and realized I couldn't script a setTimeout function that successfully calls process.exit(). After a bit of trial and error, I understand the cause to be the node process was started with a command line argument of --watch-path which I presume creates an unhandled promise.

Here's the node command starting the process:

"node --env-file=.env --loader ts-node/esm --watch-path=./src index.ts"

And this is my code block to end the process:

if (process.argv[1] === import.meta.filename) { //if this file is first executed file on node process
  fastify.listen({port: Number(process.env.PORT), host: process.env.HOST}, (err, address) => {
    if (err) {
      fastify.log.error(err)
      process.exit(1)
    }
    console.log(`App listening address: ${address} , on port ${process.env.PORT}`);
    console.log("node environment: ", process.env.NODE_ENV);
        (() => {
            console.log("closing server...");
            setTimeout(function () {
                fastify.close();
                process.exit(0);
            }, 3500)
        })();

  });
}

I've already run an A/B comparison to verify that my database and dev server connections are all closing correctly. The only difference is whether the --watch flag is set when starting the process. Is there a way to script a workaround that resolves this promise from the file being watched?

I tried calling process.exit() expecting the process to end... instead the process kept running in idle.

1 Answer 1

0

well, the issue is when we use the --watch-path option, the node js creates a watcher on that very path, and that's why the process does not exit even after using process.exit() or setTimeout for closing the server. As the watcher keeps process in active state and keep on waiting for file changes and prevents the process from fully existing because event loop always have something and never empty.

for this issue, you can use a different watcher tool like Nodemon or ts-node-dev

nodemon --exec "node --env-file=.env --loader ts-node/esm index.ts" --watch ./src

or you can use process.exit() with a signal

if (process.argv[1] === import.meta.filename) { //if this file is first executed file on node process
    fastify.listen({port: Number(process.env.PORT), host: process.env.HOST}, (err, address) => {
      if (err) {
        fastify.log.error(err)
        process.exit(1)
      }
      console.log(`App listening address: ${address} , on port ${process.env.PORT}`);
      console.log("node environment: ", process.env.NODE_ENV);
          (() => {
              console.log("closing server...");
              setTimeout(function () {
                fastify.close(() => {
                    console.log("Server is closed");
                    process.kill(process.pid, 'SIGTERM'); 
                  });
              }, 3500)
          })();
  
    });
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.