ericclemmons / grunt-express-server

Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fallback implementation question

nemosx opened this issue · comments

I am trying to learn more about the implementation of the fallback function. In the source, I see a comment that suggests that it can be used to "Prevent EADDRINUSE from breaking Grunt." When I look at the rest of the code, I do not see any additional references to this property - what am I missing? Thanks so much in advance for helping steer me in the right direction. I apologize if this isn't the right place for this type of question - I am still a GitHub noob.

Hi @nemosx, I don't think you're missing anything. In some versions of node sending a SIGINT to the child would not cause it to exit, causing EADDRINUSE on respawn. I believe the comment you reference was a TODO, but we ended up merging an option called hardStop that accomplishes the same thing.

Thank you so much for the quick reply @jlsutherland ! I thought the fallback function could be used to attempt a server restart when the configured port was already in use. Is there a standard way to determine an available port during the plugin configuration? I would also need this port for downstream task configuration as well.

Some implementations seem to start the server with the configured port and increment the port if it is already being used. https://github.com/karma-runner/karma/blob/master/lib/server.js

  webServer.on('error', function (e) {
    if (e.code === 'EADDRINUSE') {
      self.log.warn('Port %d in use', config.port)
      config.port++
      webServer.listen(config.port)
    } else {
      throw e
    }
  })

Something like that could work. You would want to ensure that the final port used is exposed to other grunt modules. For example, a minimal config will look like the following:

  grunt.initConfig({
    express: {
      options: {
        port: process.env.PORT || 9000
      },
    open: {
      server: {
        url: 'http://localhost:<%= express.options.port %>'
      }
    }, ...
});

The open module will need to access the port via the fallback. Have you come up with anything?

What you described is exactly what I am looking for. I think I could put something together, but I may need a little guidance/coaching. Do you think this would be a valuable feature for the grunt-express-server module? If so, I'd like to take a stab at it.

Actually, I think I found another module already that handles this cleanly. grunt-port-pick can be used to inject an available port into both multiple grunt configs. Thank you again so much for your help!

Of course!