The most reliable way to stop a node instance

I have a very busy web application:

  • Nginx as a front acting as a reverse proxy
  • Node server running on unprivileged ports requested by Nginx

The application uses websockets (one socket is open for each client). Nginx is configured very typically:

server { listen XY255.3:443 ssl http2; server_name example.com; client_max_body_size 0; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_dhparam /etc/nginx/ssl/dhparam.pem; ssl_stapling on; ssl_stapling_verify on; location / { proxy_pass http://localhost:8082; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } 

My idea is that when I want to stop the server, I do the following: I send SIGTERM to: * I get the server to stop accepting connections * I disconnected existing sockets (including websockets) * I clear the intervals and timeouts

At this point, the server should leave itself, because the event loop should be empty (after the last DB record, etc.).

I encoded it like this:

  ... var server = http.createServer(app); process.on('SIGTERM', function(){ server.close(); console.log("TERMINATING THIS INSTANCE!"); // This will make sure hotplate modules will get dir // of intervals and lose ends process.emit( 'hotplateShutdown'); var handles = Array.prototype.slice.call(process._getActiveHandles()) console.log( "Event loop has: ", handles.length ); handles.forEach( (o ) => { console.log( "Dealing with:", o.readyState ); o.unref && o.unref(); } ); }); server.listen(app.get('port'), app.get('ipAddress'), function () { console.log("Express server listening on port " + app.get('port')); }); 

If after 10 seconds the process is still ongoing, I send SIGKILL (something is wrong at that moment). However, note that I can restart the server almost immediately, because after SIGTERM the server stops listening to the binding port. This means minimal downtime when restarting.

Web sites are killed, as it should be.

Questions:

1) Is this a smart way to do this?

2) If the DB call is half way, will it be completed?

3) Since I use an expression, I cannot just use this method easily - can I?

I noticed that the event loop contains 5 sockets. These sockets exist even when Nginx is down. I'm not sure why 5, and I'm not sure if this is a reasonable way to go ...

Comments / ideas?

0
source share
1 answer

SIGTERM and SIGKILL are thread daemons that are made specifically for this, i.e. to complete or kill processes.

Is this a smart way to do this?

If nothing happens, then yes, that's great.

If the database call is half completed, will it be completed?

Yes, I can assure you that if some db call continues, it will be completed before killing the process.

Since I use an expression, I can't just use this technique - right?

I don’t think you can, and I still think that killing the process is the best solution.

Some time ago I did something similar on AWS Elastic Beanstalk, where I have to kill a worker and restart it again. Well, there I have SDS SDK methods like

  • restartAppServer
  • killTheEnviornment

but I researched this, and I found this . Perhaps this will help you.

0
source

Source: https://habr.com/ru/post/1264820/


All Articles