Search code examples
watchman

how to terminate server on watchman rebuild


I have a simple watch target in my Makefile

watch:
  watchman-make -p '**/*.go' -t run

target run starts a web server and binds to a port. Upon file change, run is executed, but get the error.

bind: address already in use

this is because, the web server started previously is not terminated. I see few threads related to this, but doesn't give a solution.

https://github.com/facebook/watchman/issues/246 https://github.com/facebook/watchman/issues/447

Is this possible with watchman to terminate the server and release the port, before each build.


Solution

  • The solution is not really directly controllable with watchman since it doesn't know anything about your server since it sounds like you're forking it off at the end of the build, but should be pretty simple: have your run target be responsible for tearing down the old server instance.

    For example, in your Makefile:

    run: build
          ./stop-running-server
          ./start-server
    

    How you implement stop-running-server is up to you. A couple of common techniques you could try are listed below:

    • pkill to kill processes whose name matches that of your server
    • Have your server write out its process id to a file and then you can kill $(cat pidfile) in the implementation of stop-running-server