howmanyleft.co.uk runs its node.js workers behind supervisord. To avoid
dropping requests with 502s when restarting workers, I hook into the
SIGTERM signal and call close() on the HTTP server. This stops the server
from listening for new connections, and once all the open connections are
complete, the worker exits.
Here’s a quick example:
Since I’m using redis on howmanyleft, I need to close my redis connection
gracefully too. The close event on the HTTP server fires when all
connections have closed, so close my redis connection there. node_redis
flushes all active redis commands when you call quit, so I won’t lose any
data.