Rogach
Rogach

Reputation: 27260

How can I delay "online" event from cluster processes under pm2?

My nodejs app takes a while before it is able to accept requests (resources are compiled, etc).

When I run it under pm2 in cluster mode and trigger reload, pm2 starts reloading instances one-by-one - but it does not wait until application is actually able to accept requests, and goes on to restart other instances - which results in all instances being down for some time.

Looking through the pm2 source, it seems that it waits for "online" event from worker process, and that event happens too soon.

Is there a way to delay this online event to achieve normal reload?

Here's the test case:

var http = require("http");

setTimeout(() => {
  var server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader("Content-Type", "text/plain");
    res.end("hello\n");
  });

  server.listen(7000, "127.0.0.1", () => {
    console.log("server ready");
  });
}, 10000);

Start with pm2 start app -i 2, then try reloading with pm2 reload app. On my machine, there is about 4s window when app does not respond to requests at all:

curl: (7) Failed to connect to localhost port 7000: Connection refused

(you can conveniently monitor if app is online with watch curl -sS localhost:7000)

Upvotes: 1

Views: 1650

Answers (2)

DAC
DAC

Reputation: 366

Sometimes you might need to wait for your application to have established connections with your DBs/caches/workers/whatever. PM2 needs to wait, before considering your application as online. To do this, you need to provide --wait-ready to the CLI or provide wait_ready: true in a process file. This will make PM2 listen for that event. In your application you will need to add process.send('ready') when you want your application to be considered as ready.

https://pm2.keymetrics.io/docs/usage/signals-clean-restart/

Upvotes: 0

Rogach
Rogach

Reputation: 27260

Increasing GRACEFUL_LISTEN_TIMEOUT value fixes the issue - by default, it is set to 3 seconds, which means that pm2 gives up too quickly and goes on to next instance.

You can change the value like this:

PM2_GRACEFUL_LISTEN_TIMEOUT=15000 pm2 update

Upvotes: 1

Related Questions