node.js - How to find out whether there are still events registered in the event loop (bonus: how many) -


i trying write node.js program execute , monitor javascript programs. looking way find out whether monitored program still "running" i.e. doing useful.

in current approach, when receiving code test, start new child process , hand code it. child process instruments code creates sandbox using contextify , executes code using sandbox.

after sandbox.run(code) call returned know blocking part of code finished , can show in ui. however, don't whether code registered timers using settimeouts or created other event sources cause parts of code exited later. don't know whether it's "finished" yet.

is there way in node.js check whether there still events on event loop handled (or better, how many left)?

i found this other question, talks how monitor event loop find out whether performance of node still fine. i'm not interested in performance (i don't care if executing code blocking 10s or doing 1ms every 2 minutes) , don't want use outside tools find out state of event loop inside node itself. possible?

i solved problem in way, although didn't find general answer question.

the idea here process exit if has executed code started , no eventemitters registered anymore. wanted, since wanted notified when process "done" , listen "exit" event of child_process.

but process executed code didn't exit itself. had 2 reasons:

  1. i used timer regularly send data gathered execution parent process. if such timer registered process won't exit. unref timer afraid lead data loss since process quit before last bit of data sent out (since wouldn't wait timer execute). changed code schedule timer if there data sent out instead of regularly checking data sent.
  2. i used fork create child process. creates communication channel between parent , child process , since needed send code execute child_process registered child process messages received parent using process.on("message", callback). however, have eventemitter registered prevents process quitting. luckily, realized needed 1 message parent process , no further messages remove event emitter after receiving message. instead of process.on() used process.once() execute callback once , automatically remove event emitter after that. wanted. alternatively use process.removelistener().

now wait child_process exit , know finished , can notify client.

so solution here make sure none of own eventemitters keep process alive , wait exit.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -