TransWikia.com

pm2 restart loop in AWS Elastic Beansalk (Node environment)

Stack Overflow Asked by Tom Schreck on December 5, 2021

I have a NodeJs environment built in AWS Elastic Beanstalk. I’m using pm2 to monitor 2 different NodeJS apps uploaded to environment. The kicker is I need to make sure the local app is started before the gateway app starts. I’m using npm-run-all synchronously to start apps in a specific order.

Here’s my package.json:

"start": "npm-run-all -s start:local start:gateway",
"start:local": "pm2 start ./ecosystem.config.js --only local-service --env production",
"start:gateway": "pm2 start ./ecosystem.config.js --only gateway-service --env production",

Here’s my ecosystem.config file:

module.exports =
{
  apps:
    [
      {
        name: 'local-service',
        script: './dist/services/local.js',
        watch: false,
        interpreter: 'node',
        interpreter_args: '--require ts-node/register --require tsconfig-paths/register',
        autorestart: false
      },
      {
        name: 'gateway-service',
        script: './dist/server.js',
        watch: false,
        interpreter: 'node',
        interpreter_args: '--require ts-node/register --require tsconfig-paths/register',
        wait_ready: true,
        listen_timeout: 5000,
        autorestart: false
      }
    ]
};

The elastic beanstalk log is indicating start in package.json is continuously being called. I’ve configured each app to not restart, but there seems to be something else causing the continuous restart to happen. I’m getting 502 Bad Gateway error when trying to access the gateway-service itself.

One Answer

Please try to use just pm2 ecosystem without npm-run-all, i.e. just pm2 start ecosystem.config.js and allow any (re)start sequences, because in real life your gateway & local apps can be restarted in different orders.

So instead of using start scheduling logic - please consider to move that logic to your local app - to monitor & re-connect on connection failure with gateway app.

In case you still want to use start scheduling logic, please take a look at pm2 process actions and add to gateway app code to trigger starting your local app.

Answered by Denisix on December 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP