Nestjs app not working on cpanel node-selector - node.js

I am trying to run my nest.js app by
function log(s: any) {
fs.appendFileSync('./serverStart.log', JSON.stringify(s) + "\n");
}
async function bootstrap() {
try {
const app = await NestFactory.create(AppModule);
log("app created")
app.setGlobalPrefix('api');
log("app set prefix")
await app.listen(0);
log("started")
} catch (e) {
log(e);
}
}
bootstrap();
that works nicely locally and used to work nicely with cpanel node-selector at my hoster until two days ago when I suspect my hoster changed sth, though they deny it. However now I get the follwowing error in my log file for the await app.listen(0) command
{"errno":-17,"code":"EEXIST","syscall":"uv_pipe_open"}
I am not sure what to make of it and a search did not result in anything meaningfull? Any ideas someone?
Could it be sth is already listening on the port (wild guess) and if so how can fix that?
Simple erxpress app works like the one below work at my hoster
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var response = "All good";
res.end(response);
});
server.listen();
So it seems to be some interworking issue between cpanel and nest
UpdatE: I investigated further and found
calling $ node /home/dasdding/nestbasic/dist/main.js the app is nicely started on my hosters environment
"starting "
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [NestFactory] Starting Nest application...
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [InstanceLoader] AppModule dependencies initialized +33ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [InstanceLoader] HttpModule dependencies initialized +4ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [InstanceLoader] MetricsModule dependencies initialized +0ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [InstanceLoader] TerminusModule dependencies initialized +1ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [InstanceLoader] HealthModule dependencies initialized +0ms
"create app "
"prefix done"
"init done"
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [RoutesResolver] HealthController {/nestbasic/health}: +5ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [RouterExplorer] Mapped {/nestbasic/health, GET} route +2ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [RouterExplorer] Mapped {/nestbasic/health/storage, GET} route +1ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [RouterExplorer] Mapped {/nestbasic/health/memory, GET} route +0ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [RouterExplorer] Mapped {/nestbasic/health/main4shops, GET} route +1ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [RoutesResolver] MetricsController {/nestbasic/metrics}: +0ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [RouterExplorer] Mapped {/nestbasic/metrics, GET} route +0ms
[Nest] 879990 - 11/25/2022, 5:21:05 PM LOG [NestApplication] Nest application successfully started +3ms
"now listening "
"http://[::1]:45611"
Buit when starting it via capnel i still get the error. According to https://www.quora.com/What-is-EINVAL-in-Linux this EINVAL means invalid argument passed.
So I check what arguments are being passed and found it to be ["/opt/alt/alt-nodejs16/root/usr/bin/node","/usr/local/lsws/fcgi-bin/lsnode.js"]
Comparing that to the call when I just run node ..../main.js I see a difference. Running node .../main.js as expected I get
["/opt/alt/alt-nodejs16/root/usr/bin/node","/home/dasdding/nestbasic/dist/main.js"]
Doing a cat /usr/local/lsws/fcgi-bin/lsnode.js I see that the script executed by cpanel is not directly my script but sth that appears to be some sort of a wrapper that mentions " Copyright 2002-2018 Lite Speed Technologies Inc, All Rights Reserved. * LITE SPEED PROPRIETARY/CONFIDENTIAL" I.e. from an app developers perspective that knows his app but not cpanel wrapping cpanel is not calling my script but doing sth different that results in an invalid parameter exeception.
Since previously nest.js and cpanel worked together and now they dont I conclude that this is a incompatibility between cpanel and nest.js that must have recently been introduced, since it worked before (config change or code - not sure).
Since in my deployment of my app I use the defined versions in the package-lock of packages and install via npm ci (thus using the exact versions from the package-lock and tried deployment with a commit if package-lock where it worked I conclude that it is most likey a recent chagne in cpanel/node-selector config or code. Or do I misunderstand npm ci and package-lock,json working together?
Is there anybody from cpanel here to comment? Will also post in cpanel forum...
Update: Has been fixed by my hoster. Was an issue with the litespeed servers and a reinstall solved the issue.
Cheers
Tom

Related

NodeJS app crashes when requests MSSQL DB

Good day everyone!
I have an old app from dev that now isn't working in our company. I need to start this app but don't have enough experience in NodeJS (I don't have it at all, TBH).
The problem is: I can build a docker image, start it, and use the app, but when I make something that requires to make a request to MQSQL server, the app crashes.
But there are no issues with requests to Postgres DB.
This is my docker build output
docker build -t fixver .
Sending build context to Docker daemon 2.427MB
Step 1/21 : FROM node:16-alpine as base
---> c4ee3c9d7bc1
Step 2/21 : ARG NODE_ENV=production
---> Using cache
---> ba79cfac2e2c
Step 3/21 : ENV NODE_ENV=${NODE_ENV} NODE_OPTIONS="--max_old_space_size=8192"
---> Using cache
---> 1a344f8791d8
Step 4/21 : WORKDIR /usr/src/app
---> Using cache
---> 1c591a772bcd
Step 5/21 : FROM base as clientBuilder
---> 1c591a772bcd
Step 6/21 : COPY ./client/package.json ./client/yarn.lock ./
---> Using cache
---> 7382b944fcc0
Step 7/21 : RUN yarn install --production=false --frozen-lockfile
---> Using cache
---> 431700be035b
Step 8/21 : COPY ./client .
---> Using cache
---> 87790e7c061a
Step 9/21 : RUN yarn build
---> Using cache
---> 9ba70dd8301c
Step 10/21 : FROM base as serverBuilder
---> 1c591a772bcd
Step 11/21 : COPY ./server/package.json ./server/yarn.lock ./
---> Using cache
---> bf5dc70ee2eb
Step 12/21 : RUN yarn install --production=false --frozen-lockfile
---> Using cache
---> c26b02f2af5c
Step 13/21 : COPY ./server .
---> 16fdc772d650
Step 14/21 : RUN yarn build
---> Running in 676753d20a77
yarn run v1.22.19
$ node_modules/.bin/rimraf dist
$ node_modules/.bin/nest build
Done in 5.75s.
Removing intermediate container 676753d20a77
---> 66a53b6af7cd
Step 15/21 : FROM base as production
---> 1c591a772bcd
Step 16/21 : COPY ./server/package.json ./server/.env ./
---> dbbbe7295cfd
Step 17/21 : RUN yarn install --pure-lockfile
---> Running in f3b965d1ec0d
yarn install v1.22.19
info No lockfile found.
[1/4] Resolving packages...
warning Resolution field "uuid#8.3.2" is incompatible with requested version "uuid#^3.1.0"
warning jest > jest-cli > jest-config > jest-environment-jsdom > jsdom > w3c-hr-time#1.0.2: Use your platform's native performance.now() and performance.timeOrigin.
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning " > ts-loader#9.4.2" has unmet peer dependency "webpack#^5.0.0".
[4/4] Building fresh packages...
Done in 39.04s.
Removing intermediate container f3b965d1ec0d
---> 8d804071a8d2
Step 18/21 : COPY --from=serverBuilder /usr/src/app/dist ./
---> f760fda4b1a1
Step 19/21 : COPY --from=clientBuilder /usr/src/app/build ./public
---> 4f697bab8bc6
Step 20/21 : EXPOSE 80
---> Running in 95fae0df9266
Removing intermediate container 95fae0df9266
---> 4bebff7957bc
Step 21/21 : CMD ["node", "./main"]
---> Running in 19b4df36b37e
Removing intermediate container 19b4df36b37e
---> 362fbadf0b4a
Successfully built 362fbadf0b4a
Successfully tagged fixver:latest
Starting the app
docker run -p 7007:80 fixver
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [NestFactory] Starting Nest application...
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] AppModule dependencies initialized +60ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] PassportModule dependencies initialized +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ServeStaticModule dependencies initialized +0ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] AuthModule dependencies initialized +8ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] PagesModule dependencies initialized +0ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ScriptsModule dependencies initialized +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RoutesResolver] ScriptsController {/api/scripts}: +8ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts, GET} route +4ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id, GET} route +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id/check, POST} route +0ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id/apply, POST} route +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RoutesResolver] PagesController {/api/pages}: +1ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/pages/:id, GET} route +0ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG [NestApplication] Nest application successfully started +5ms
[Nest] 1 - 12/01/2022, 1:49:54 PM LOG Server started! Port: 80
And error right after making request to MSSQL
node:events:491
throw er; // Unhandled 'error' event
^
Error: No event 'featureExtAck' in state 'SentLogin7WithNTLMLogin'
at Connection.dispatchEvent (/usr/src/app/node_modules/tedious/lib/connection.js:1663:26)
at Parser.<anonymous> (/usr/src/app/node_modules/tedious/lib/connection.js:1224:12)
at Parser.emit (node:events:513:28)
at Readable.<anonymous> (/usr/src/app/node_modules/tedious/lib/token/token-stream-parser.js:27:14)
at Readable.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Readable.push (node:internal/streams/readable:228:10)
at next (node:internal/streams/from:98:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Readable instance at:
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
I really don't know what to do with it, how to debug it and find the problem.
Any help and advise is appreciated.
Problem was solved by changing this code in Dockerfile
-COPY ./server/package.json ./server/.env ./
-RUN yarn install --pure-lockfile
+COPY ./server/package.json ./server/yarn.lock ./server/.env ./
+RUN yarn install --production=true --frozen-lockfile

NestJS project starting when i running migration generate

When i running npm run typeorm migration:generate -- -n CreateUserTable migration successfully generates, but at the same time my project is starting. When i running migration:create all works fine and project doesn't starts.
I tried
change ts-node to node.
run npx typeorm instead of npm run typeorm
changing dist folder to src
create mig:gen scripts
Here's error log
> ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli "migration:generate" "-n" "CreateUserTable"
[Nest] 6648 - 2022-01-27 19:23:24 LOG [NestFactory] Starting Nest application...
[Nest] 6648 - 2022-01-27 19:23:24 ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it
now has an active connection session.
at AlreadyHasActiveConnectionError.TypeORMError [as constructor] (C:\Users\whitered932\Projects\wr-todo\src\error\TypeORMError.ts:7
:9)
at new AlreadyHasActiveConnectionError (C:\Users\whitered932\Projects\wr-todo\src\error\AlreadyHasActiveConnectionError.ts:8:9)
at ConnectionManager.create (C:\Users\whitered932\Projects\wr-todo\src\connection\ConnectionManager.ts:57:23)
at C:\Users\whitered932\Projects\wr-todo\src\globals.ts:77:35
at step (C:\Users\whitered932\Projects\wr-todo\node_modules\tslib\tslib.js:143:27)
at Object.next (C:\Users\whitered932\Projects\wr-todo\node_modules\tslib\tslib.js:124:57)
at fulfilled (C:\Users\whitered932\Projects\wr-todo\node_modules\tslib\tslib.js:114:62)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
[Nest] 6648 - 2022-01-27 19:23:24 LOG [InstanceLoader] AppModule dependencies initialized +7ms
[Nest] 6648 - 2022-01-27 19:23:24 LOG [InstanceLoader] TypeOrmModule dependencies initialized +2ms
Migration C:\Users\whitered932\Projects\wr-todo/src/migrations/1643293402567-CreateUserTable.ts has been generated successfully.
[Nest] 6648 - 2022-01-27 19:23:27 LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +3152ms
[Nest] 6648 - 2022-01-27 19:23:27 LOG [InstanceLoader] TypeOrmModule dependencies initialized +2ms
[Nest] 6648 - 2022-01-27 19:23:27 LOG [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 6648 - 2022-01-27 19:23:27 LOG [InstanceLoader] UserModule dependencies initialized +3ms
[Nest] 6648 - 2022-01-27 19:23:27 LOG [InstanceLoader] TaskModule dependencies initialized +1ms
[Nest] 6648 - 2022-01-27 19:23:28 LOG [RoutesResolver] TaskController {/task}: +344ms
[Nest] 6648 - 2022-01-27 19:23:28 LOG [RouterExplorer] Mapped {/task, GET} route +5ms
[Nest] 6648 - 2022-01-27 19:23:28 LOG [RouterExplorer] Mapped {/task/:id, GET} route +3ms
[Nest] 6648 - 2022-01-27 19:23:28 LOG [RouterExplorer] Mapped {/task, POST} route +2ms
[Nest] 6648 - 2022-01-27 19:23:28 LOG [RouterExplorer] Mapped {/task/:id, PATCH} route +3ms
[Nest] 6648 - 2022-01-27 19:23:28 LOG [RouterExplorer] Mapped {/task/:id, DELETE} route +3ms
[Nest] 6648 - 2022-01-27 19:23:28 LOG [RoutesResolver] UserController {/user}: +3ms
[Nest] 6648 - 2022-01-27 19:23:28 LOG [NestApplication] Nest application successfully started +7ms
Завершить выполнение пакетного файла [Y(да)/N(нет)]? Y
Here's my files
package.json scripts
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"
},
ormconfig.json
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "postgres",
"database": "postgres",
"entities": ["dist/**/*.js"],
"migrations": ["dist/migrations/*.js"],
"cli": {
"migrationsDir": "src/migrations"
}
}
Try adding keepConnectionAlive in your ormconfig.json file

What is the CMD command for nats-server inside Dockerfile

I am currently building an application using NestJS by following microservice architecture. And use NATS as a messaging system. It works fine on my local machine but I am unable to dockerized the NATS server.
The folder structure of my project:
├─── services/
├─── nats/
| Dockerfile
├─── client/
| ...
| ...
| .dockerignore
| Dockerfile
└─── docker-compose.yml
Now, the Dockerfile inside nats/ folder describes as below:
FROM nats:2.1.9
EXPOSE 4222
CMD [ "nats-server" ]
And Dockerfile inside client/ folder describes as:
FROM node:12
WORKDIR /myProject/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 8081
CMD [ "node", "dist/main" ]
And the docker-compose.yml is:
version: '3'
services:
nats-server:
image: nats:2.1.9
restart: always
build: ./nats
ports:
- 4222:4222
client-service:
build: ./client
volumes:
- ./client:/myProject/src/app
depends_on:
- nats-server
environment:
NODE_ENV: development
ports:
- 8081:3000
I've run the command sudo docker-compose build and found no error, after that run the command sudo docker-compose up and found the following error:
Recreating services_nats-server_1 ... done
Recreating services_client-service_1 ... done
Attaching to services_nats-server_1, services_client-service_1
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
services_nats-server_1 exited with code 1
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [NestFactory] Starting Nest application...
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] AppModule dependencies initialized +41ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] MongooseModule dependencies initialized +1ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] ClientsModule dependencies initialized +1ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] ClientsModule dependencies initialized +0ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] ConfigHostModule dependencies initialized +1ms
client-service_1 | [Nest] 1 - 12/03/2020, 4:44:34 PM [InstanceLoader] ConfigModule dependencies initialized +1ms
services_nats-server_1 exited with code 1
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
nats-server_1 | nats-server: unrecognized command: "nats-server_1"
There is no problem with the client service Dockerfile, as it depends on the nats-server tried to run it first. Can anyone please help me to figure out the problem?
I would like to thank you for your time and consideration.
The build: command for the nats-server service has overwritten the Docker Hub image. I just need to pull the image and expose the port.
So, my docker-compose.yml file will be as following:
version: '3'
services:
nats-server:
image: nats:2.1.9
restart: always
ports:
- 4222:4222
client-service:
build: ./client
volumes:
- ./client:/myProject/src/app
depends_on:
- nats-server
environment:
NODE_ENV: development
ports:
- 8081:3000
Check the Dockerfile and you'll notice that ENTRYPOINT is set to /nats-server and CMD is the parameters to ENTRYPOINT (--config nats-server.conf in that case).
Unless you extend the nats image you don't need to build a new one based on it (remove build: ./nats from the compose file). Overriding the default CMD can be achieved by setting command: on the docker-compose.yml.

NestJS Heroku Deploy H10

I deploy a NestJS Application to Heroku and get H10 Error
Log:
2020-05-07T13:12:51.967622+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-07T13:15:14.556288+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=scalenode.herokuapp.com request_id=535e5643-26ad-4fbe-acf3-7f805c8c463c fwd="91.64.79.140" dyno= connect= service= status=503 bytes= protocol=https
Its stunning, because the Error comes because of GET Request. Node has been compiled successfully. I deployed via HerokuCli and Github. Both has the same result. But if I test it with HerokuCli locally with heroku local web, it works.
It does not seems to be a deployment error itself, its seems to be the route / cant be resolved so the app crashes. When I open the app in browser after successfully deploy, it seems to be a timeout.
Here is the Repo. I have a 'Hello World' string on / route. I cant find out whats wrong.
I use a Procfile for the instance, which seems to be fine:
web: node dist/main.js
EDIT:
I tried to have a static HTML Response and tried stable Node Version 12.x.
I dont get it
2020-05-07T14:22:40.862206+00:00 app[web.1]: [32m[Nest] 4 - [39m05/07/2020, 2:22:40 PM [38;5;3m[RoutesResolver] [39m[32mAppController {}:[39m[38;5;3m +6ms[39m
2020-05-07T14:22:40.865503+00:00 app[web.1]: [32m[Nest] 4 - [39m05/07/2020, 2:22:40 PM [38;5;3m[RouterExplorer] [39m[32mMapped {, GET} route[39m[38;5;3m +3ms[39m
2020-05-07T14:22:40.865886+00:00 app[web.1]: [32m[Nest] 4 - [39m05/07/2020, 2:22:40 PM [38;5;3m[RoutesResolver] [39m[32mAuthController {}:[39m[38;5;3m +0ms[39m
2020-05-07T14:22:40.867140+00:00 app[web.1]: [32m[Nest] 4 - [39m05/07/2020, 2:22:40 PM [38;5;3m[RouterExplorer] [39m[32mMapped {/auth/login, POST} route[39m[38;5;3m +2ms[39m
2020-05-07T14:22:40.867778+00:00 app[web.1]: [32m[Nest] 4 - [39m05/07/2020, 2:22:40 PM [38;5;3m[RouterExplorer] [39m[32mMapped {/profile, GET} route[39m[38;5;3m +0ms[39m
2020-05-07T14:22:40.868162+00:00 app[web.1]: [32m[Nest] 4 - [39m05/07/2020, 2:22:40 PM [38;5;3m[RoutesResolver] [39m[32mFilesController {/files}:[39m[38;5;3m +1ms[39m
2020-05-07T14:22:40.868999+00:00 app[web.1]: [32m[Nest] 4 - [39m05/07/2020, 2:22:40 PM [38;5;3m[RouterExplorer] [39m[32mMapped {/files/upload, POST} route[39m[38;5;3m +0ms[39m
2020-05-07T14:22:40.873240+00:00 app[web.1]: [32m[Nest] 4 - [39m05/07/2020, 2:22:40 PM [38;5;3m[NestApplication] [39m[32mNest application successfully started[39m[38;5;3m +5ms[39m
2020-05-07T14:22:42.000000+00:00 app[api]: Build succeeded
It doesnt work... i cant find more details in logs, nothing more concrete. What the hell is going wrong...
In main.ts, have you tried changing the port number? To something like:
app.listen(parseInt(process.env.PORT) || 3000)
Since Heroku will try to use its own port. Maybe this could work too:
const port: number = parseInt(`${process.env.PORT}`) || 3000;
await app.listen(port);
Did you try setting heroku config variable NPM_CONFIG_PRODUCTION to false?
You can try this by going to your heroku dashboard -> settings -> reveal config vars, and then add the value above.
This error happens mainly because NestJS relies on some devDependencies in your project, and heroku removes devDependencies when building your Nest app if you're building in production mode. Setting NPM_CONFIG_PRODUCTION config to false would include all your dev dependencies in the Nest app's build process, just like building for a development environment. Alternatively, you can selectively move your nest related devDependencies to dependencies in package.json until the problem is gone.
Let me know if it works!

Cannot get more than ~980 connections with Socket.io and Docker

I am unable to scale my simple Socket.IO app past around 980 concurrent connections using Docker. However, if I run it locally on my macOS Sierra 10.12.6 I can get over 3000 connections. I have included this repo of a simple SocketIO application that I am testing with:
https://github.com/gsccheng/simple-socketIO-app
My Docker-for-Mac is configured at 4 CPUs and 5 GB memory. The Version is
Version 17.09.0-ce-mac35 (19611)
Channel: stable
a98b7c1b7c
I am using Artillery version 1.6.0-9 to load test it with
$ artillery run load-test.yaml
I'm showing some redundant configurations of the settings (to show you that they have been considered). Here are my steps to reproduce.
$ docker build . -t socket-test
$ docker run -p 8000:8000 -c 1024 -m 4096M --privileged --ulimit nofile=9000:9000 -it test-socket:latest /bin/sh
#> DEBUG=* npm start
Up to around 980 connections I will get logs like this:
Connected to Socket!
socket.io:client writing packet {"type":2,"data":["news",{"hello":"world"}],"nsp":"/"} +0ms
socket.io-parser encoding packet {"type":2,"data":["news",{"hello":"world"}],"nsp":"/"} +0ms
socket.io-parser encoded {"type":2,"data":["news",{"hello":"world"}],"nsp":"/"} as 2["news",{"hello":"world"}] +0ms
engine:socket sending packet "message" (2["news",{"hello":"world"}]) +0ms
socket.io:socket joined room 0ohCcHMWYASnfRgJAAPS +0ms
engine:ws received "2" +5ms
engine:socket packet +0ms
engine:socket got ping +0ms
engine:socket sending packet "pong" (undefined) +0ms
engine:socket flushing buffer to transport +0ms
engine:ws writing "3" +0ms
engine upgrading existing transport +2ms
engine:socket might upgrade socket transport from "polling" to "websocket" +0ms
engine intercepting request for path "/socket.io/" +2ms
engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=Ly8pfqL&b64=1&sid=0ohCcHMWYASnfRgJAAPS" +0ms
engine setting new request for existing client +0ms
engine:polling setting request +0ms
engine:socket flushing buffer to transport +0ms
engine:polling writing "28:42["news",{"hello":"world"}]" +0ms
engine:socket executing batch send callback +1ms
engine:ws received "2probe" +4ms
engine:ws writing "3probe" +0ms
engine intercepting request for path "/socket.io/" +4ms
engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=Ly8pfqV&b64=1&sid=0ohCcHMWYASnfRgJAAPS" +0ms
engine setting new request for existing client +0ms
engine:polling setting request +0ms
engine:socket writing a noop packet to polling for fast upgrade +10ms
engine:polling writing "1:6" +0ms
engine:ws received "5" +2ms
engine:socket got upgrade packet - upgrading +0ms
engine:polling closing +0ms
engine:polling transport discarded - closing right away +1ms
engine:ws received "2" +20ms
engine:socket packet +0ms
engine:socket got ping +0ms
engine:socket sending packet "pong" (undefined) +0ms
engine:socket flushing buffer to transport +1ms
engine:ws writing "3" +0ms
engine intercepting request for path "/socket.io/" +1ms
engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=Ly8pfr1&b64=1" +0ms
engine handshaking client "6ccAiZwbvrchxZEiAAPT" +0ms
engine:socket sending packet "open" ({"sid":"6ccAiZwbvrchxZEiAAPT","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}) +0ms
engine:socket sending packet "message" (0) +0ms
engine:polling setting request +0ms
engine:socket flushing buffer to transport +0ms
engine:polling writing "97:0{"sid":"6ccAiZwbvrchxZEiAAPT","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}2:40" +0ms
engine:socket executing batch send callback +0ms
socket.io:server incoming connection with id 6ccAiZwbvrchxZEiAAPT +0ms
socket.io:client connecting to namespace / +1ms
socket.io:namespace adding socket to nsp / +0ms
socket.io:socket socket connected - writing packet +0ms
socket.io:socket joining room 6ccAiZwbvrchxZEiAAPT +0ms
socket.io:socket packet already sent in initial handshake +0ms
Connected to Socket!
At about 980 connections I will begin seeing these disconnected events:
disconnected to Socket!
transport close
engine intercepting request for path "/socket.io/" +27ms
engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=Ly8pg1T&b64=1" +0ms
engine handshaking client "C-pdSXFCbwQaTeYLAAPh" +0ms
engine:socket sending packet "open" ({"sid":"C-pdSXFCbwQaTeYLAAPh","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}) +0ms
engine:socket sending packet "message" (0) +0ms
engine:polling setting request +0ms
engine:socket flushing buffer to transport +0ms
engine:polling writing "97:0{"sid":"C-pdSXFCbwQaTeYLAAPh","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}2:40" +0ms
engine:socket executing batch send callback +0ms
socket.io:server incoming connection with id C-pdSXFCbwQaTeYLAAPh +0ms
socket.io:client connecting to namespace / +0ms
socket.io:namespace adding socket to nsp / +0ms
socket.io:socket socket connected - writing packet +1ms
socket.io:socket joining room C-pdSXFCbwQaTeYLAAPh +0ms
socket.io:socket packet already sent in initial handshake +0ms
Connected to Socket!
socket.io:client writing packet {"type":2,"data":["news",{"hello":"world"}],"nsp":"/"} +0ms
socket.io-parser encoding packet {"type":2,"data":["news",{"hello":"world"}],"nsp":"/"} +0ms
socket.io-parser encoded {"type":2,"data":["news",{"hello":"world"}],"nsp":"/"} as 2["news",{"hello":"world"}] +0ms
engine:socket sending packet "message" (2["news",{"hello":"world"}]) +0ms
socket.io:socket joined room C-pdSXFCbwQaTeYLAAPh +0ms
engine intercepting request for path "/socket.io/" +13ms
engine handling "POST" http request "/socket.io/?EIO=3&transport=polling&t=Ly8pg1g&b64=1&sid=C-pdSXFCbwQaTeYLAAPh" +0ms
engine setting new request for existing client +1ms
engine:polling received "1:1" +0ms
engine:polling got xhr close packet +0ms
socket.io:client client close with reason transport close +0ms
socket.io:socket closing socket - reason transport close +1ms
disconnected to Socket!
Then it'll be this repeated over and over again:
engine:ws writing "3" +0ms
engine:ws received "2" +42ms
engine:socket packet +0ms
engine:socket got ping +0ms
engine:socket sending packet "pong" (undefined) +1ms
engine:socket flushing buffer to transport +0ms
engine:ws writing "3" +0ms
engine:ws received "2" +4ms
engine:socket packet +0ms
engine:socket got ping +0ms
engine:socket sending packet "pong" (undefined) +0ms
engine:socket flushing buffer to transport +0ms
engine:ws writing "3" +0ms
engine:ws received "2" +45ms
engine:socket packet +0ms
engine:socket got ping +0ms
engine:socket sending packet "pong" (undefined) +0ms
engine:socket flushing buffer to transport +0ms
engine:ws writing "3" +0ms
engine:ws received "2" +7ms
engine:socket packet +0ms
engine:socket got ping +0ms
engine:socket sending packet "pong" (undefined) +0ms
engine:socket flushing buffer to transport +0ms
engine:ws writing "3" +0ms
As you can see in my Dockerfile, I have set a few configurations that I've gathered from googling my problem:
COPY limits.conf /etc/security/
COPY sysctl.conf /etc/
COPY rc.local /etc/
COPY common-session /etc/pam.d/
COPY common-session-noninteractive /etc/pam.d/
COPY supervisord.conf /etc/supervisor/
On my local system I've also done a few configurations like following this example. Here is the state of my host machine:
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 64000
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 2048
virtual memory (kbytes, -v) unlimited
What can I do to get more than ~980 concurrent socket connections? Why do I fail to make any more connections at that point? How can my repo be tweaked (if needed) to get this to work?
Edit
When I lower the nofiles limit to say 500 for the container, I see that my application disconnects seem to fail the same way. When I increase or decrease my memory and CPU by say half/double. I don't see any different in behavior, so it doesn't seem like that is the issue.
There's a significant difference between the network path to the app locally and the app running in Docker for Mac.
The path to your app on the mac is straight in via the loopback interface:
mac
client -> lo -> nodejs
When using Docker for Mac, the path in includes more hops and includes two userland proxy processes, vpnkit on your mac and docker-proxy which accept TCP connections on the forwarded port and forward data in:
mac | vm | container
client -> lo -> vpnkit -> if -> docker-proxy -> NAT -> bridge -> if -> nodejs
Try with a VM that has a network directly accessible to the mac to see if vpnkit is making an appreciable difference.
mac | vm | container
client -> if -> if -> docker-proxy -> NAT -> bridge -> if -> nodejs
You can also remove docker-proxy by attaching the containers interface directly to the the VM network so the container doesn't require the port mapping (-p). This can be done by mapping a macvlan interface to the container or placing the container on a bridge attached to the VM network. This is a vagrant setup I use for the bridged network.
mac | container <- there is a little vm here, but minimal.
client -> if -> if -> nodejs
mac | vm | container
client -> if -> if -> bridge -> if -> nodejs
Once you've got rid of the network differences then I'd look at tuning the VM and container in a bit more detail. I'd guess you should see a 10-20% decrease in the VM, not 66%.
I faced the engine:polling got xhr close packet And I tried to search all from stackoverflow, but only this question has this info.
I have briefly investigated into it, and it is that when client sending both get+post http request, somehow, the load balancer rejected the get while the post may still work fine, so this also happens on our sites.
The problem should be escalated to the stability of load balancer. (Especially its stability of sticky session)

Resources