0

Consider the following cases when nodejs runtime crashes or is forcefully taken down, I want to do some cleanup, for instance unregistering with consul which is a service registry and discovery utility.

["exit", "SIGINT", "SIGHUP", "SIGTERM", "SIGQUIT", "SIGUSR1", "SIGUSR2", "uncaughtException"].forEach(function(value){
    process.on(value, function(){
        consul.agent.check.deregister(env.CONSUL_ID, function(error) {
            if (error) throw error;
            console.log("DeRegistered Consul.");
            process.exit(1);
        });
    })
});
  1. The signals are not firing when I take down the container (docker-compose down), however, they do fire when I run the nodejs instance in a terminal and I press Ctrl-C to force exit.

  2. Please also check on the signal cases, for instance I might not need an uncaught exception, de-registration should only happen when the whole nodejs instance goes down. Any other unnecessary signals in here or signals that I might be missing?

Edit

How do I start the container. (using docker-compose)

  dept:
    image: node:10
    container_name: dept
    working_dir: /service
    command: bash -c "npm install && npm install nodemon -g && nodemon --exec npm start"
    ports:
      - 3003:3000
      - 9232:9229
    volumes:
      - "./dept:/dept"
AppDeveloper
  • 1,816
  • 7
  • 24
  • 49
  • see this https://stackoverflow.com/questions/58131968/how-to-stop-supervisord-when-a-specific-program-stopped-normally/58132042#58132042 you can setup your command depend on the service gowes down – LinPy Oct 09 '19 at 06:31
  • i'm not using supervisorid, not sure what this is either – AppDeveloper Oct 09 '19 at 06:40
  • yes I know that you not using it, but using it will help you manage the stop/start of your services see http://supervisord.org/introduction.html – LinPy Oct 09 '19 at 06:42
  • Is Node.js the main process inside the container? How do you start it? – b0gusb Oct 09 '19 at 07:50
  • @b0gusb see my edit please – AppDeveloper Oct 09 '19 at 08:08
  • 1
    From [Building Graceful Node Applications in Docker](https://medium.com/@becintec/building-graceful-node-applications-in-docker-4d2cd4d5d392): *The solution to this signal-passing problem is disappointingly simple: run `node server.js` directly from the Dockerfile instead of `npm start`*. The same is also mentioned here: [Top 4 Tactics To Keep Node.js Rockin’ in Docker](https://www.docker.com/blog/keep-nodejs-rockin-in-docker/) – tgogos Oct 09 '19 at 09:35
  • I see, i'm using nodemon for dev environment, shall I create a separate docker-compose for dev and production? – AppDeveloper Oct 09 '19 at 11:28

0 Answers0