Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.35k stars 2.61k forks source link

NextJS 13/14 app router fails with cluster mode with SIGINT #5771

Open prunusnira opened 7 months ago

prunusnira commented 7 months ago

What's going wrong?

PM2 starts but immediately shows 'errored' status for all clusters. I saw from community that I need to run by using 'next start' directly, but as I need to use .env files, I used env-cmd. of course, I can run single instance by executing script manually from the command line

ecosystem.config.js

module.exports = {
    apps: [
        {
            name: 'mall-front',
            script: 'env-cmd -f .env.prd next start',
            instances: 3,
            exec_mode: 'cluster',
            merge_logs: true,
            autorestart: true,
            watch: false,
            // max_memory_restart: '1G',
            env: {
                NODE_ENV: 'dev',
            },
            env_prd: {
                NODE_ENV: 'prd',
            },
        },
    ],
};

result

PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:1] exited with code [0] via signal [SIGINT]
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:1] starting in -cluster mode-
PM2        | 2024-02-20T19:44:37: PM2 log: App name:mall-front id:2 disconnected
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:2] exited with code [0] via signal [SIGINT]
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:2] starting in -cluster mode-
PM2        | 2024-02-20T19:44:37: PM2 log: App name:mall-front id:3 disconnected
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:3] exited with code [0] via signal [SIGINT]
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:3] starting in -cluster mode-
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:1] online
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:2] online
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:3] online
PM2        | 2024-02-20T19:44:37: PM2 log: App name:mall-front id:1 disconnected
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:1] exited with code [0] via signal [SIGINT]
PM2        | 2024-02-20T19:44:37: PM2 log: Script /bin/bash had too many unstable restarts (16). Stopped. "errored"
PM2        | 2024-02-20T19:44:37: PM2 log: App name:mall-front id:2 disconnected
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:2] exited with code [0] via signal [SIGINT]
PM2        | 2024-02-20T19:44:37: PM2 log: Script /bin/bash had too many unstable restarts (16). Stopped. "errored"
PM2        | 2024-02-20T19:44:37: PM2 log: App name:mall-front id:3 disconnected
PM2        | 2024-02-20T19:44:37: PM2 log: App [mall-front:3] exited with code [0] via signal [SIGINT]
PM2        | 2024-02-20T19:44:37: PM2 log: Script /bin/bash had too many unstable restarts (16). Stopped. "errored"

I can run with fork mode though, it causes lots of instances needed to run and maintain our service.

How could we reproduce this issue?

just run the script above

Supporting information

NextJS 14.1.0 NodeJS 18.18.2 PM2 5.3.0

rafaeljunio commented 6 months ago

same issue