Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PM2 and cluster mode in Node.js/TypeScript #5790

Open
robokonk opened this issue Mar 19, 2024 · 5 comments
Open

PM2 and cluster mode in Node.js/TypeScript #5790

robokonk opened this issue Mar 19, 2024 · 5 comments

Comments

@robokonk
Copy link

I have a problem with running PM2 in cluster mode.
I am running it in Docker. How can I fix it?

root@docker-desktop /s/a/v1# node -v
v20.11.1
  • my example script:
import axios from 'axios';

async function checkIP() {
    const response = await axios.get('https://api.ipify.org?format=json');
    if (response.data.ip) {
        console.log(response.data.ip);
    } 
}

checkIP();
  • ecosystem.config.cjs
module.exports = {
  apps: [{
    script: 'ts-node ./src/index.ts', // Compiled JS file path from your TypeScript file
    instances: 'max', // Use 'max' for as many instances as CPU cores or specify a number
    exec_mode: 'cluster', // Enables cluster mode
    watch: true, // Watch for file changes in development
    env: {
      NODE_ENV: 'development',
    },
    env_production: {
      NODE_ENV: 'production',
    }
  }]
};
  • tsconfig.json
{
  "compilerOptions": {
  "moduleResolution": "node",
         "esModuleInterop": true,
         "target": "es6",
         "module": "commonjs"
  }
}
  • package.json
{
  "name": "v1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^1.6.8",
    "got": "^14.2.1"
  }
}
@ultimate-tester
Copy link

You posted a lot of source code snippets but did not actually describe the issue you are having.

@robokonk
Copy link
Author

@ultimate-tester
I am having a problem running it in cluster mode. While the mode is working, I do not have any error logs, only information indicating that there is an error with the instance running in cluster mode.

@ultimate-tester
Copy link

Is that message appearing when you do pm2 logs? Or else, where do you see it?
Could it be that this happens because your script is completing/terminating? It doesn't seem to have any functionality that needs to be clustered.

@robokonk
Copy link
Author

@ultimate-tester

root@docker-desktop /s/a/v1# pm2 flush && pm2 start ecosystem.config.cjs
[PM2] Flushing /root/.pm2/pm2.log
[PM2] Logs flushed
[PM2][WARN] Applications index not running, starting...
[PM2] App [index] launched (8 instances)
┌────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ index    │ default     │ N/A     │ cluster │ 6167     │ 0s     │ 1    │ online    │ 0%       │ 46.9mb   │ root     │ enabled  │
│ 1  │ index    │ default     │ N/A     │ cluster │ 6178     │ 0s     │ 1    │ online    │ 0%       │ 49.6mb   │ root     │ enabled  │
│ 2  │ index    │ default     │ N/A     │ cluster │ 6196     │ 0s     │ 1    │ online    │ 0%       │ 0b       │ root     │ enabled  │
│ 3  │ index    │ default     │ N/A     │ cluster │ 6132     │ 0      │ 1    │ stopped   │ 0%       │ 0b       │ root     │ enabled  │
│ 4  │ index    │ default     │ N/A     │ cluster │ 6143     │ 0s     │ 0    │ online    │ 0%       │ 0b       │ root     │ enabled  │
│ 5  │ index    │ default     │ N/A     │ cluster │ 6150     │ 0s     │ 0    │ online    │ 0%       │ 51.9mb   │ root     │ enabled  │
│ 6  │ index    │ default     │ N/A     │ cluster │ 6161     │ 0s     │ 0    │ online    │ 0%       │ 48.9mb   │ root     │ enabled  │
│ 7  │ index    │ default     │ N/A     │ cluster │ 6190     │ 0s     │ 0    │ online    │ 0%       │ 45.6mb   │ root     │ enabled  │
└────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
root@docker-desktop /s/a/v1# pm2 list
┌────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ index    │ default     │ N/A     │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ root     │ enabled  │
│ 1  │ index    │ default     │ N/A     │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ root     │ enabled  │
│ 2  │ index    │ default     │ N/A     │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ root     │ enabled  │
│ 3  │ index    │ default     │ N/A     │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ root     │ enabled  │
│ 4  │ index    │ default     │ N/A     │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ root     │ enabled  │
│ 5  │ index    │ default     │ N/A     │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ root     │ enabled  │
│ 6  │ index    │ default     │ N/A     │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ root     │ enabled  │
│ 7  │ index    │ default     │ N/A     │ cluster │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ root     │ enabled  │
└────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
root@docker-desktop /s/a/v1# pm2 logs
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/root/.pm2/pm2.log last 15 lines:
PM2        | 2024-03-21T18:18:56: PM2 log: App name:index id:4 disconnected
PM2        | 2024-03-21T18:18:56: PM2 log: App [index:4] exited with code [0] via signal [SIGINT]
PM2        | 2024-03-21T18:18:56: PM2 log: Script /usr/bin/bash had too many unstable restarts (16). Stopped. "errored"
PM2        | 2024-03-21T18:18:56: PM2 log: App name:index id:3 disconnected
PM2        | 2024-03-21T18:18:56: PM2 log: App [index:3] exited with code [0] via signal [SIGINT]
PM2        | 2024-03-21T18:18:56: PM2 log: Script /usr/bin/bash had too many unstable restarts (16). Stopped. "errored"
PM2        | 2024-03-21T18:18:56: PM2 log: App name:index id:6 disconnected
PM2        | 2024-03-21T18:18:56: PM2 log: App [index:6] exited with code [0] via signal [SIGINT]
PM2        | 2024-03-21T18:18:56: PM2 log: Script /usr/bin/bash had too many unstable restarts (16). Stopped. "errored"
PM2        | 2024-03-21T18:18:56: PM2 log: App name:index id:5 disconnected
PM2        | 2024-03-21T18:18:56: PM2 log: App [index:5] exited with code [0] via signal [SIGINT]
PM2        | 2024-03-21T18:18:56: PM2 log: Script /usr/bin/bash had too many unstable restarts (16). Stopped. "errored"
PM2        | 2024-03-21T18:18:56: PM2 log: App name:index id:7 disconnected
PM2        | 2024-03-21T18:18:56: PM2 log: App [index:7] exited with code [0] via signal [SIGINT]
PM2        | 2024-03-21T18:18:56: PM2 log: Script /usr/bin/bash had too many unstable restarts (16). Stopped. "errored"

/root/.pm2/logs/index-out-0.log last 15 lines:
/root/.pm2/logs/index-error-0.log last 15 lines:
/root/.pm2/logs/index-out-1.log last 15 lines:
/root/.pm2/logs/index-error-1.log last 15 lines:
/root/.pm2/logs/index-out-2.log last 15 lines:
/root/.pm2/logs/index-error-2.log last 15 lines:
/root/.pm2/logs/index-out-3.log last 15 lines:
/root/.pm2/logs/index-error-3.log last 15 lines:
/root/.pm2/logs/index-out-4.log last 15 lines:
/root/.pm2/logs/index-error-4.log last 15 lines:
/root/.pm2/logs/index-out-5.log last 15 lines:
/root/.pm2/logs/index-error-5.log last 15 lines:
/root/.pm2/logs/index-out-6.log last 15 lines:
/root/.pm2/logs/index-error-6.log last 15 lines:
/root/.pm2/logs/index-out-7.log last 15 lines:
/root/.pm2/logs/index-error-7.log last 15 lines:

^C⏎                                                                                                                                                                 
root@docker-desktop /s/a/v1 [SIGINT]# pm2 logs --err
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/root/.pm2/logs/index-error-0.log last 15 lines:
/root/.pm2/logs/index-error-1.log last 15 lines:
/root/.pm2/logs/index-error-2.log last 15 lines:
/root/.pm2/logs/index-error-3.log last 15 lines:
/root/.pm2/logs/index-error-4.log last 15 lines:
/root/.pm2/logs/index-error-5.log last 15 lines:
/root/.pm2/logs/index-error-6.log last 15 lines:
/root/.pm2/logs/index-error-7.log last 15 lines:

Or another way, do you have any JS script that works with cluster mode? Can you share the repository with me, including the package.json file?

@ultimate-tester
Copy link

Code 0 is a normal successful program end, which I'd expect from the code you provided.

The cluster mode is meant to automatically share a listening port between all nodes in the cluster. For example if you setup an express server and want to load balance between multiple nodes. So perhaps the sample code of express might be a good test. Another example would be if you have built a (queue) worker. In any case scripts that keep running because they are listening on a port or waiting for an event to happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants