#PM2 wait_ready for Nest.js app in cluster mode

5 messages · Page 1 of 1 (latest)

humble sail
#

Anyone was able to configure pm2 with nest.js and wait_ready signal? Idea is that when I run pm2 reload I would hope app being accessible during the whole reload if I run in it in cluster mode.

This is config I am using for pm2:

module.exports = {
    apps: [
        {
            name: "pm2test",
            script: "dist/pm2ReloadTest.js",
            instances: "3",
            exec_mode: "cluster",
            autorestart: true,
            time: true,
            wait_ready: true,
           
        },
    ],
};

and this is what I add to app.module

export class AppModule implements OnApplicationBootstrap {
  async onApplicationBootstrap() {
    console.log('All modules initialized');
    process.send('ready');
  }
}

Instead what happen is that the user might experience 502 error coming from nginx (acting as reverse proxy for nest.js app)

lapis shuttle
#

onApplicationBootstrap is fired before the http listeners start listening. You need to send the ready signal after app.listen in your bootstrap function

craggy quiver
#

@lapis shuttle I am facing the same issue as the user above. I've added the process.send() call after the app. listen but i get the following error

process.send('ready');
1|api | ^
1|api | TypeError: process.send is not a function
1|api | at bootstrap

Do you have any ideas?

#
 await app.listen(configService.get<number>('SERVER_PORT'));
 process.send('ready');
craggy quiver
#

pm2 start dist/src/main.js --name along-api -i 2 --wait-ready

This has resolved the issue for me, the key is to start the compiled main.js file