PM2 reloads now wait until new LINE bot workers are listening
2 hours ago
This release of the Cofacts LINE bot carries one fix, for anyone who runs it under PM2, which is how its Docker image starts the server. When PM2 reloads the bot, it now waits until the new workers are listening before it stops the old ones. That closes a short window during each reload where LINE's webhook calls were refused.
Ask about this presentation
Chapters
Show transcriptHide transcript
This release of the Cofacts LINE bot carries one fix, for anyone who runs it under PM2, which is how its Docker image starts the server. When PM2 reloads the bot, it now waits until the new workers are listening before it stops the old ones. That closes a short window during each reload where LINE's webhook calls were refused.
Here's one reload, just after quarter past midnight UTC on October 5th. The new workers came online at the two-second mark, but startup takes about seven seconds. PM2 waited its default three seconds, then stopped old workers zero and one at the five-second mark, and worker two at 8.4 seconds. The new workers started listening at nine seconds. In between, cloudflared logged two refused connections, and the LINE Developers console showed two 502 errors.
PM2 reloads the bot on a cron every three hours, and each reload could open that same gap. Over a month of cloudflared logs, from September 6th to October 5th, a refused connection during a reload or deploy showed up 13 times, more than any other reload-time failure. The connection resets that the October 1st release targeted showed up 5 times.
The fix is a handshake across two files. In ecosystem.config.js, setting wait_ready to true makes PM2 wait for a ready message, and listen_timeout gives it fifteen seconds instead of the default three. In src/index.js, the listen callback sends ready once the server accepts connections. It only sends when process.send exists, so running the app outside PM2 works as before.
Here's the same reload with the fix. Startup still takes about seven seconds. PM2 now keeps the old workers running until the new ones report ready, so a worker is listening the whole way through. The new fifteen-second limit is about twice today's startup time, which leaves room for a slow start.
Both settings ship in the repository's own ecosystem.config.js, and the Docker image copies that file in. So the upgrade is to rebuild the image with docker build, and deploy it. If you start the bot with your own PM2 config instead, add the same two lines, wait_ready true and listen_timeout fifteen thousand. The app already sends the ready message.
This release changes only how a reload brings up the new workers. Together with the October 1st release, which gives old workers fifteen seconds to shut down cleanly, both ends of a reload are now covered. So with this release, PM2 reloads wait until the new LINE bot workers are listening.






