I'm trying to seed some data to a database:
// seed.js
var Knex = require("knex");
var database = require("./config").database;
var knex = Knex.initialize(database);
knex("users").insert({
first_name: "John",
last_name: "Doe"
}).exec(function (err, id) {
console.log("Fulfilled", id);
});
// returns [ 1 ]
My problem is that if I put this in a file and running it using node seed.js, it executes the console.log then it seems to be waiting longer (about 20 seconds) before it returns the shell prompt. Doing the same in REPL where the result is instantaneous.
Is there a reason why my code will wait 20 seconds before it shows me the command prompt again if I run it in shell vs executing this in a REPL?
by default node won't give you the console back until the script is totally completely done, while inside the node repl it gives back the repl as soon as the last line stops blocking.
In the repl you actually get focus back before the console.log prints, while with the script it also has to wait for stuff to close up, you can speed it along by adding process.exit(0) after the console.log.
Related
Is possible to restart automatically a node cli script when it's finished to execute the code?
I have this cli script that will run until a variable reach a limit
const maxExecution = 200;
let i = 0;
let task = setInterval( () => {
i++;
if( i >= maxExecution ){
clearInterval(task);
}
// code here...
},5000);
This code will work fine and will stop the tasks when the i variable reach the set limit. I'm reading this question about how to manage process.exit. Is there any event I can listen to understand if the script execution have reached the end?
I had this code (typescript, not vanilla nodejs) when I was working with node<=10 (possibly between 6 ~ 10, not quite sure), it can restart itself on-demand:
import { spawn } from "child_process";
let need_restart:boolean=false;
process.on("exit",function(){
if(need_restart){
spawn(process.argv.shift(),process.argv,{
"cwd":process.cwd(),
"detached":true,
"stdio":"inherit"
});
}
});
It's part of a http/ws server, when all server closed (no more event), the process automatically exit. By setting the need_restart to true, it will restart itself when this happen.
I haven't used node for quite some time, so I'm not sure if this still work on later version, but I think it's worth mentioning it here.
I have made a Node.js script which checks for new entries in a MySQL database and uses socket.io to send data to the client's web browser. The script is meant to check for new entries approximately every 2 seconds. I am using Forever to keep the script running as this is hosted on a VPS.
I believe what's happening is that the for loop is looping infinitely (more on why I think that's the issue below). There are no error messages in the Forever generated log file and the script is "running" even when it's started to hang up. Specifically, the part of the script that hangs up is the script stops accepting browser requests at port 8888 and doesn't serve the client-side socket.io js files. I've done some troubleshooting and identified a few key components that may be causing this issue, but at the end of the day, I'm not sure why it's happening and can't seem to find a work around.
Here is the relevant part of the code:
http.listen(8888,function(){
console.log("Listening on 8888");
});
function checkEntry() {
pool.getConnection(function(err,connection) {
connection.query("SELECT * FROM `data_alert` WHERE processtime > " + (Math.floor(new Date() / 1000) - 172800) + " AND pushed IS NULL", function (err, rows) {
connection.release();
if (!err) {
if(Object.keys(rows).length > 0) {
var x;
for(x = 0; x < Object.keys(rows).length; x++) {
connection.query("UPDATE `data_alert` SET pushed = 1 WHERE id = " + rows[x]['id'],function() {
connection.release();
io.emit('refresh feed', 'refresh');
});
}
}
}
});
});
setTimeout(function() { checkEntry();var d = new Date();console.log(d.getTime()); },1000);
}
checkEntry();
Just a few interesting things I've discovered while trouble shooting...
This only happens when I run the script on Forever. Work's completely fine if I use shell and just leave my terminal open.
It starts to happen after 5-30 minutes of running the script, it does not immediately hang up on the first execution of the checkEntry function.
I originally tried this with setInterval instead of setTimeout, the issue has remained exactly the same.
If I remove the setInterval/setTimeout function and run the checkEntry function only once, it does not hang up.
If I take out the javascript for loop in the checkEntry function, the hang ups stop (but obviously, that for loop controls necessary functionality so I have to at least find another way of using it).
I've also tried using a for-in loop for the rows object and the performance is exactly the same.
Any ideas would be immensely helpful at this point. I started working with Node.js just recently so there may be a glaringly obvious reason that I'm missing here.
Thank you.
So I just wanted to come back to this and address what the issue was. It took me quite some time to figure out and it can only be explained by my own inexperience. There is a section to my script where my code contained the following:
app.get("/", (request, response) => {
// Some code to log things to the console here.
});
The issue was that I was not sending a response. The new code looks as follows and has resolved my hang up issues:
app.get("/", (request, response) => {
// Some code to log things to the console here.
response.send("OK");
});
The issue had nothing to do with the part of the code I presented in the initial question.
I'm writing a script to batch process some text documents and insert them into a mysql database. I'm trying to use the async library because using a standard while loop blocks the event queue and prevents the insert queries from getting run until all are generated. Since that may take 10 minutes or more, I get a timeout. So, I am trying to use async to avoid blocking the main thread. However, it's not working as expected. When I run the simplest form of the code below, using node test.js, in the command line, it only executes once, instead of infinitely. It seems like the computer is terminating the node process early since it is non-blocking. This, of course, is not what I want. Why is this, and how can I get it to work correctly?
//this code should run forever, constantly printing "working". However it only runs once.
var async = require('async')
async.whilst(function(){return true},function(){console.log("working")})
The second parameter for whilst() is a function that takes in a callback that needs to be called when the current iteration is "done."
So if you modify the code this way, you'll get what you're expecting:
var async = require('async');
async.whilst(function() {
return true
}, function(cb) {
console.log("working");
cb();
});
From within node.js I use the oriento module to access a OrientDB. In principle everything works fine, the only thing is the program dos not exit as it should. Here is the example:
const oriento = require("oriento");
const server = oriento({});
const db = server.use(dbname);
db.select("#rid").from("codelists").limit(1).scalar().then(function (result) {
console.dir(result);
}).finally(function () {
db.close();
server.close();
console.info("finished");
});
The programm executes the select, then the "then" and finally the "finally" clauses. All fine. But it does not exit. There seems to be something hanging on the event loop. But what? And how can I get rid of it? The problem actually is worse then just "press Ctrl-C" or have a process.exit() when everything is done, because the code is supposed to run within a unit test, where I cannot call exit. Any suggestions?
The problem is solved in the current master version of oriento. https://github.com/codemix/oriento/issues/170
You can use process._getActiveRequests() and process._getActiveHandles() to see what's holding the app open. They are undocumented but commonly used in this scenario.
I'm writing a library to abstract my data layer (it's going to use a mix of Mongo and Memcached). I've been testing Monk and can't figure out why the below script isn't finishing:
mongo = require("monk")("mongodb://#{options.mongodb.username}:#{options.mongodb.password}##{options.mongodb.hostname}:#{options.mongodb.port}/#{options.mongodb.database}")
users = mongo.get("users")
find = users.findById 12345
find.complete (err, doc) ->
console.dir doc
console.dir err
It's returning the document to the log, { _id: 12345, foo: "bar" }, successfully but not completing when run using node test.js. Why is this?
The reason the script stays alive is because the connection to MongoDB is still open. If you call mongo.close(); that should close the connection and provided you have nothing else keeping the event loop alive (e.g. network connections, timers, etc), then your script should terminate.