Debugging nodejs while using Kinesis MultiLangDaemon/KCL - node.js

Following this post I was able to connect our existing nodejs code into Kinesis logs (using KCL and MultiLangDaemon).
The problem is that I can't debug the code anymore.
Since MultiLangDaemon uses STDIN/STDOUT to interact with executed "script", once I call "node --debug" and get the message:
"debugger listening on port 57846"
I get an error from the MultiLangDaemon saying:
"SEVERE: Received error line from subprocess [debugger listening on port 57846] for shard shardId-000000000000"
Is there a way to execute nodejs "quietly", so it won't send this STDERR message ?
Does anyone have experience with MultiLangDaemon and debugging ?
Thanks,
Shushu

I got an answer in here, recommending to work with node inspector.
After installing, all I had to do is to change the kinesis.properties executableName from "node" to "node-debug", and I got it working.

Related

Make nodemon auto-restart a program on crash without waiting for file changes at custom error?

I'm building an E-commerce site, where there's an Authentication system.
I noticed that if the client login with a wrong user or password, the backend/server that works with nodemon will crach and hang in there crashed till i restart manually nodemon. This is example output error of the nodemon crash:
[nodemon] app crashed - waiting for file changes before starting...
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent
to the client
Ofcourse, when server crashes, client can no more access or do login again till server restarts.
After some googling, i found this question and this repository that fix my problem but particulary and not as expected precisely, i dont want nodemon to restart forever on any error that occure ofcourse, but only with specifics errors that i set them -like Authentication errors as i mentionned above-.
So, my idea/question is: is there anyway to get nodemon restarts by itself in some cases of failures or errors (NOT ALL)?
Seems like you a referring to a production situation, and nodemon is a development node server, which is not intended for use in production, as the intro states:
nodemon is a tool that helps develop Node.js based applications by
automatically restarting the node application when file changes in the
directory are detected.
You should use node.js in production, instead of nodemon.
For managing your node server in production, you could use a process manager like PM2..
That said, an authentication server that crashes every time a user uses a wrong password seams very ineffective in handling a common use case. So I would advise to start with fixing the root cause, which is the buggy server, and then for recovery from incidental crashes use something like PM2.
PS:
The error you are getting looks like an express error you get when you send a response (in this case an error response) without exiting the function e.g. by using return. Because you are not returning, another res.send is called, which causes the 'ERR_HTTP_HEADERS_SENT' error. See this answer.
This is really bad since it can send your program into a loop of restarting, but if you really want it, replace app.js with your file's name and try this:
nodemon -x 'node app.js || copy /b app.js +,,'
Linux version:
nodemon -x 'node app.js || touch app.js'
Next time try a little googleing before you ask since it is most likely faster.

Express JS code doesnt work with pm2, but when started manually ("node ."/"node index.js")

I got a Discord JS project running which works fine, now I wanted to also run a simple express script to receive a post request. When I do "node ." the code works and successfully logs the requests, if I do "pm2 start index.js --name xyz" the code also starts, logs its boot up message, doesn't shut down, but also doesn't react to any requests. There is simply no response at the set port by express.
Running on an Ubuntu VPS.
Works if I do "pm2 start index.js --name xyz -- --port XXXX" I don't know why and would like to know how these extra arguments work if anyone could explain.
You can read about arguments on the official docs
It's likely you have a runtime error. Please read your own logger files and ~/.pm2/logs/*.log and see if you can find anything. Had it been a syntax error, it would have shut down immediately. Yet, always pm2 statusafter start to double check it.

NodeJS debug Starting inspector on 127.0.0.1:42457 failed: address already in use error

I am trying to debug my Discord bot using the DiscordJS lib. My trouble is that when I start a debug session (launching with vscode or with --inspect-brk | --debug-brk)
I get a Starting inspector on 127.0.0.1:42457 failed: address already in use error I have tried changing the port in both situations and still getting the same error on different ports. My bot is sharded so its start child process of itself. I have tried using vscode to attach to child processes but still the same error.
If anybody is familiar with DiscordJS and knows how to debug a sharded bot some help would be much appreciated
In my case to solve this, just add that config on nodemon.json
{
"execMap": {
"js": "node --inspect=9300 -r sucrase/register"
}
}
I added --inspect=9300 to set port to this specific port
First Ctrl C (or Cmd C) to quit all your progress in the cmd.
Start Task Manager > Find all tasks named "node" > End process.
Now go back to your cmd and start server.
That is because you are already running that application in with node filename.js, hence stop that process and start with debugging again with same port.
You can take a look that on the below video might help
https://www.youtube.com/watch?v=b8e9RAekktY&t=28s

Strange ECONNRESET error I cannot figure out

I do not know, if this is related to koa, or is problem of some other npm module or something else. I am going to start from here.
So to the problem. I am having REST api written in koa v1. We are running node server in the Docker image. One of the endpoints we have, starts the import and returns the status 200 with message: "import started", and when the import finishes, we send Slack message to notify us.
So first I tested the server on my local machine, everything works (endpoint does not throw any errors). Then I built docker image. I run container localy, everything works (endpoint does not throw any errors). I deploy my image to Mesos environment, everything works so far. Container runs, every endpoint works, beside import endpoint. When I call it, after few seconds (5 to 10), I get ECONNRESET error, the running container gets killed and new running instance is started. So import is terminated.
At the beginning we assigned 128 MB ram to the docker container and that seems to be enough. After import error occurred, we thought maybe OOM killed process. So we decided to check dmesg and we could not find any log entries related to the OOM and the process of the running container. Then we checked ram usage of the container locally (with htop) and found out it uses aprox. 250+ MB, so we decided to add more ram in marathon config (512 MB). That however did not help, same error occurred.
Because the error was not explicit enough we installed longjohn module, so we could get more detailed error message. That got us just a little bit more information, but not as much as we thought it would.
Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TCP.onread (net.js:569:26)
---------------------------------------------
at Application.app.callback (/src/node_modules/koa/lib/application.js:130:45)
at Application.app.listen (/src/node_modules/koa/lib/application.js:73:39)
at Promise.then.result (/src/server.js:97:13)
Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TCP.onread (net.js:569:26)
Line 97 of the server.js is:
96:if(!module.parent) {
97: app.listen(port, (err) => {
98: if (err) {
99: console.error('Server error', err);
100: }
101: console.log('Listening on the port', port);
102: });
103:}
So what exactly happens in the endpoint logic. We are using postgres npm module pg. We are passing pg.Pool to the context, so later we can use it in our models. We are executing insert query encapsulated in promise and push promises in the array. There are roughly 2700+ records. Later we do Promise.all on the array of promises and with then we send the message to Slack.
As you can see I do not know if the error is related to koa or pg or some other thing. What is more intriguing is that locally everything works (node server as well as in docker container), but on Mesos it does not. How can I find out what is wrong?
version of koa npm module: 1.2.0
version of pg npm module: 6.1.0
version of Postgres 9.5
version of Mesos: 1.0.1
According to this github issue this is an error caused by tiny-lr.
It seems that downgrading to version 0.2.1 stops it, but this is usually a dependency of other packages you're using that you've got no control over. You might be able to filter out the error by displaying all errors except this, as such:
if (error.code !== 'ECONNRESET') { console.log(error) }
The issue is still open, and dates from Oct 27, 2016. Don't know if it will get fixed or not. But as far as feedback goes, it doesn't seem like a dangerous error, or to have any impact whatsoever. But heh, I'd rather fix mine too, if there was a way.
Thanks to another developer, we found out what was the cause of the ERROR. We used all connections in the pool when there was an import running.
When the marathon was requesting the service status at the time of the import, service tried to connect to the database to test the connection and at that time the connection to the database was terminated. Service became unhealthy and marathon restarted the service. We re-factored the import code. We are limiting the number of pool connections.

NodeJS: forever.js throws binding error : EROFS read only file system

Okay, so I am using nodejitsu's forever(v0.11.1) module to keep my NodeJS(v0.10.28) + expressJS(3.5.1) server running on VPS(CentOS-6.4). Everything was working smoothly untill recently when I started getting following error while running command: forever start server.js As soon as I run this command I got the following error:
I tried to see the file: ls - l /root/.forever/tVYM.log, No such file was found
When I tried to start my node server using pm2, I got the following error:
I don't understand why is this happening even if I am root(su) user. Also, if I try to edit my server.js file, CentOS won't let me edit file and warns me about insufficient privilege.
But when I rebooted VPS and used forever, things were okay again but after some time, my server went down again and when I used forever to run my node app, forever threw the same error. I just can not see reason behind this. Thanks in advance

Resources