DDDDDD
DDDDDD

Reputation: 61

How to return the child process once the stdout stops emitting things

I have to create a tomcat server using node, I know, it's a bit weird but it's for learning purposes. So, basically, I have function that uses shelljs to spawn the server:

function spawnTomCat() {
  return new Promise((resolve, reject) => {
    const child = shell.exec('create server', {
      async: true
    })

    child.stdout.on('data', data => {
      // this is called several times, and I want to resolve the promise in the last
      // call.
      if (lastCall()) {
        resolve(child)
      }
    })

    child.stderr.once('data', reject)
  })
}

Basically, because it's a server, the child.on('close') won't ever be called, and if I call the resolve function on the first execution of the child.stdout.on('data') callback, the server will be starting. I could paste the things that I've tried, but none of them really worked, but I tried to:

Thank you in advance.

Upvotes: 0

Views: 356

Answers (1)

jfriend00
jfriend00

Reputation: 707238

OK, so you want to resolve when the server is up and running. There are really only two ways I can think of to know that.

1. Have the server tell you. The server writes some known data to stdout (or some other communication channel) when the server thinks that it's up and running and ready for requests so you can monitor that channel and known when it's good to go.

2. You poll the server. You send test requests to find out when it's responding properly to some simple request.

A timer watching for a pause in stdout data is a hack and is subject to all sorts of opportunities to be wrong.

Upvotes: 1

Related Questions