What should I do to get the process status by pid? - javascript

I tried process.kill(pid, 0) to get the status, but got
node:internal/process/per_thread:221
throw errnoException(err, 'kill');
^
Error: kill ESRCH
at process.kill (node:internal/process/per_thread:221:13)
at Timeout._onTimeout (C:\Users\Administrator\Desktop\demo\web.js:7:43)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7) {
errno: -4040,
code: 'ESRCH',
syscall: 'kill'
}
A total of two executions. It returned true for the first time, then set the timer to get it again, and returned error.
This method will throw an error if the target pid does not exist. As a special case, a signal of 0 can be used to test for the existence of a process. Windows platforms will throw an error if the pid is used to kill a process group.
From the above, if the process does not exist, process.kill(pid, 0) should return false instead of an error. Could it be that 0 is not only a check, but also kill?

Related

MetaMask Ether Faucet

Please anyone can tell what should I do? I get this error:
Error: 500 {"error":"[ethjs-query] while formatting outputs from RPC '{\"value\":{\"code\":-32603,\"message\":\"Too Many Requests\",\"data\":{\"originalError\":{}},\"stack\":\"Error: Too Many Requests\\n at eval (/www/node_modules/web3-provider-engine/subproviders/rpc.js:52:23)\\n at Request.eval [as _callback] (/www/node_modules/web3-provider-engine/subproviders/rpc.js:54:11)\\n at Request.self.callback (/www/node_modules/request/request.js:186:22)\\n at Request.emit (events.js:315:20)\\n at Request.eval (/www/node_modules/request/request.js:1155:10)\\n at Request.emit (events.js:315:20)\\n at IncomingMessage.eval (/www/node_modules/request/request.js:1077:12)\\n at Object.onceWrapper (events.js:421:28)\\n at IncomingMessage.emit (events.js:327:22)\\n at endReadableNT (internal/streams/readable.js:1327:12)\"}}'"}
I think you have sent too many requests in a short period of time as it hints in the response property called message "message": "Too Many Requests". Now rate limits cooldown time differs for every service out there, but you can try to wait for a couple of minutes and try again, if you still receive you might have to wait for an hour, a day and so on.

Why doesn't Node.js throw an error when it can't connect to socket?

When i call both of these functions only the upper one fails and returns an error
const { Socket } = require("net")
class Client {
connect () {
this.a = new Socket()
this.a.connect(this.port, this.host)
this.socket = new Socket()
this.socket.connect(this.port, this.host)
}
}
The problem is, that i know that the port this should connect to, is not used. Both functions should throw an error.
If i call the lower one first, still the one with this.a fails.
If i use this.socket for both, the first one always fails even if i change the order of them.
To differentiate between them i used a different port to connect to but also unused.
this.socket = new Socket()
this.socket.connect(6743, this.host)
this.a = new Socket()
this.a.connect(6744, this.host)
The this.port and this.host variables are not the problem, because if run the script while the server on the port is online it works.
Error Message that should be thrown:
events.js:291
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:6744
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6744
}```
Like other users have mentioned, the second error is not being triggered because the application effectively stops running after the first error. To prove this, you could try putting a console.log between your two attempts to connect to a socket and see that won't run as expected either.
In nodeJs some module throw error in event
Socket throw error in event.on('error') and you should checked event
I hope below link help you:
https://stackoverflow.com/a/25847067/6759368

Node.js - events.js throw er

Before posting this question I went through a couple of SO posts with the same heading. All of them suggested me to restart my app..I tried that but I am still getting the error.
The code that is throwing the error is
app.post('/insert', function(request, response) {
var connection = mySequel.createConnection(ConnectionParams);
connection.connect();
var UserName = request.body.UserName;
// insert into userrecords values ("123456",DEFAULT,DEFAULT,DEFAULT,10);
var InsertQuery = "insert into userrecords values (" + UserName + ",DEFAULT,DEFAULT,DEFAULT,-10);";
connection.query(InsertQuery, function(error, result, fields) {
if (error) {
response.send("error");
console.log(error);
throw error;
} else {
// response.send("success");
console.log(result.insertId);
response.send(result.insertId);
}
});
connection.end();
});
The error caused by the code is this
events.js:160
2017-04-26T11:42:52.410094+00:00 app[web.1]:
2017-04-26T11:42:52.410092+00:00 app[web.1]: throw er; // Unhandled 'error' event
2017-04-26T11:42:52.410096+00:00 app[web.1]: Error: Quit inactivity timeout
2017-04-26T11:42:52.410097+00:00 app[web.1]: at Quit.<anonymous> (/app/node_modules/mysql/lib/protocol/Protocol.js:160:17)
2017-04-26T11:42:52.410093+00:00 app[web.1]: ^
2017-04-26T11:42:52.410098+00:00 app[web.1]: at emitNone (events.js:86:13)
2017-04-26T11:42:52.410099+00:00 app[web.1]: at Quit._onTimeout (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:127:8)
2017-04-26T11:42:52.410099+00:00 app[web.1]: at Quit.emit (events.js:185:7)
2017-04-26T11:42:52.410100+00:00 app[web.1]: at ontimeout (timers.js:380:14)
2017-04-26T11:42:52.410101+00:00 app[web.1]: at tryOnTimeout (timers.js:244:5)
2017-04-26T11:42:52.410102+00:00 app[web.1]: at Timer.listOnTimeout (timers.js:214:5)
I am hosting my app on Heroku and whenever I try to access this URL the app is crashing forcing me to restart the dyno. The data I am sending is being successfully inserted into the DB but the auto incremented primary key is not being returned to my app. Instead I get a server error.
JSON.stringify() returns the correct value once and after that the app crashed which maked the subsequent requests to fail.
How can I solve this problem?
Try converting your response into JSON before sending it.
Like
response.send(JSON.stringify(result.insertId));
I too once had a similar problem and I think that is how I solved it.
EDIT:
I went through the myql npm library and found something called Pool. Pool is used when you app needs to handle more than one request at a time. I suggest that you try that. There is a good example availble here.
I have same problem, i just downgrade to react-scripts#2.1.8
this is the steps:
cd my-app
npm install react-scripts#2.1.8
npm start

Scraping request run through Heroku Scheduler (Node.js) fails half the time

I set up a very basic web scraper to check stock of a specific item on Costco.com for my grandfather. It's working great locally, but when I run it through Heroku it fails (seemingly 50% of the time). Here's the code for the scraper
const task = () => {
// toggle so doesn't send message multiple times if continuously available
let alreadyAvailable = false;
let url = 'http://www.costco.com/Kirkland-Signature-Four-Piece-Urethane-Cover-Golf-Ball,-2-dozen.product.100310467.html';
request(url, function(error, response, html){
let $ = cheerio.load(html);
if(error){
throw new Error(error);
}
if ( $('#product-page #product-details #ctas #add-to-cart input[type="button"]')['0'].attribs.value === 'Out of Stock') {
alreadyAvailable = false;
console.log("still out of stock");
} else {
if (alreadyAvailable === false) {
sendMessage();
alreadyAvailable = true;
}
}
});
};
and here are the logs
2016-12-25T03:48:39.675549+00:00 heroku[scheduler.5440]: Starting process with command `node scraper.js`
2016-12-25T03:48:40.262503+00:00 heroku[scheduler.5440]: State changed from starting to up
2016-12-25T03:48:41.509416+00:00 app[scheduler.5440]: /app/scraper.js:34
2016-12-25T03:48:41.509432+00:00 app[scheduler.5440]: if ( $('#product-page #product-details #ctas #add-to-cart input[type="button"]')['0'].attribs.value === 'Out of Stock') {
2016-12-25T03:48:41.509433+00:00 app[scheduler.5440]: ^
2016-12-25T03:48:41.509433+00:00 app[scheduler.5440]:
2016-12-25T03:48:41.509434+00:00 app[scheduler.5440]: TypeError: Cannot read property 'attribs' of undefined
2016-12-25T03:48:41.509434+00:00 app[scheduler.5440]: at Request._callback (/app/scraper.js:34:90)
2016-12-25T03:48:41.509435+00:00 app[scheduler.5440]: at Request.self.callback (/app/node_modules/request/request.js:186:22)
2016-12-25T03:48:41.509436+00:00 app[scheduler.5440]: at emitTwo (events.js:106:13)
2016-12-25T03:48:41.509436+00:00 app[scheduler.5440]: at Request.emit (events.js:191:7)
2016-12-25T03:48:41.509436+00:00 app[scheduler.5440]: at Request.<anonymous> (/app/node_modules/request/request.js:1081:10)
2016-12-25T03:48:41.509437+00:00 app[scheduler.5440]: at emitOne (events.js:96:13)
2016-12-25T03:48:41.509437+00:00 app[scheduler.5440]: at Request.emit (events.js:188:7)
2016-12-25T03:48:41.509438+00:00 app[scheduler.5440]: at IncomingMessage.<anonymous> (/app/node_modules/request/request.js:1001:12)
2016-12-25T03:48:41.509438+00:00 app[scheduler.5440]: at IncomingMessage.g (events.js:291:16)
2016-12-25T03:48:41.509439+00:00 app[scheduler.5440]: at emitNone (events.js:91:20)
2016-12-25T03:48:41.509439+00:00 app[scheduler.5440]: at IncomingMessage.emit (events.js:185:7)
2016-12-25T03:48:41.509439+00:00 app[scheduler.5440]: at endReadableNT (_stream_readable.js:974:12)
2016-12-25T03:48:41.509440+00:00 app[scheduler.5440]: at _combinedTickCallback (internal/process/next_tick.js:74:11)
2016-12-25T03:48:41.509440+00:00 app[scheduler.5440]: at process._tickCallback (internal/process/next_tick.js:98:9)
2016-12-25T03:48:41.560539+00:00 heroku[scheduler.5440]: State changed from up to complete
2016-12-25T03:48:41.550655+00:00 heroku[scheduler.5440]: Process exited with status 1
2016-12-25T03:58:42.438807+00:00 app[api]: Starting process with command `node scraper.js` by user scheduler#addons.heroku.com
2016-12-25T03:58:43.701468+00:00 heroku[scheduler.5038]: Starting process with command `node scraper.js`
2016-12-25T03:58:44.312279+00:00 heroku[scheduler.5038]: State changed from starting to up
2016-12-25T03:58:45.769564+00:00 app[scheduler.5038]: still out of stock
2016-12-25T03:58:45.827867+00:00 heroku[scheduler.5038]: State changed from up to complete
2016-12-25T03:58:45.814921+00:00 heroku[scheduler.5038]: Process exited with status 0
You can see that sometimes I get the console log inside of the if-block and others I get a type error because it's trying to read attributes from an html element that don't exist. I was thinking that this might be a async issue, but I'm not sure how to go about fixing it. I assumed Request wasn't running the callback until it got all the html.
The problem here is what Costco's website is returning.
Your code is failing when parsing the DOM via Cheerio. What this means in your case is that the particular HTML you're attempting to scrape doesn't actually exist (that's what the error is saying).
This could be caused by a few possible things:
Costco is rendering a DIFFERENT page than what you expect (maybe it thinks you're a bot, or is doing some throttling).
You are receiving a redirect or some other sort of non-error HTTP status code, and the HTML you're looking for doesn't exist there.
Costco's website changes the HTML dynamically to prevent people from scraping.
What I would do if I were you is this:
Have your process log all the page's HTML when the task runs.
The next time your process fails, copy the HTML from the Heroku logs into a local editor, and see what it returned.
I'm willing to bet you will be surprised =)

Catch exception in node.js

I have a simple program, which needs to make sure I can connect to a Redis server. I use node-redis to connect and need to wait until Redis is started. I use this piece of code:
function initializeRedis(callback) {
(function createClient(){
var runner;
try {
client = redis.createClient();
} catch (e) {
setTimeout(createClient, 1000);
}
callback();
})();
};
initializeRedis(function() {
// Work here
});
This is because without the try/catch, I got an exception from node.js:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^ Error: Redis connection to 127.0.0.1:6379 failed - ECONNREFUSED, Connection refused
at Socket.<anonymous> (/var/www/php-jobs/node_modules/redis/index.js:88:28)
at Socket.emit (events.js:64:17)
at Array.<anonymous> (net.js:830:27)
at EventEmitter._tickCallback (node.js:126:26)
When I start redis-server (Ubuntu machine) and start this script, everything works fine. If I stop redis-server and start the script, it doesn't catch the exception and still throws this same exception. How is that possible? I have a try/catch statement!
After client = redis.createClient();, set a handler for the error event:
client.on('error', function(err) {
// handle async errors here
});
Have a look at the stack trace - your code isn't in it, so there's no place where a try/catch could catch the error.

Categories

Resources