2

My understanding is that NodeJS runs until all the events are drained from the event queue, and then it will exit. On various occasions I have written a script, that uses third party modules and it does what I want it to do, but doesn't exit. I assume that's because some of the other third party modules have something in the event queue.

Is there anything I can do to watch the queue, encourage it to be drained, or even determine who still has something to do (and perhaps get enough information to know that if I process.exit anyway, no harm will be done)

Thanks for your suggestions.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
nwaltham
  • 2,067
  • 1
  • 22
  • 40
  • 2
    "NodeJS runs until all the events are drained from the event queue, and then it will exit" - really? So if you build a website using Node, it'll shut itself down if no-one visits it? Maybe I'm misunderstanding your question. – Paul D. Waite Jul 04 '13 at 09:56
  • take a look at process._getActiveHandles() and process._getActiveRequests() ( https://groups.google.com/d/msg/nodejs/ztspFn-BKgE/qw1I9VLSG_gJ ) – Andrey Sidorov Jul 04 '13 at 10:25
  • @PaulD.Waite that would really save resources wouldn't ;-) But seriously - if you have a link to "Circumstances when NodeJS will/wont exit" that would be helpful. I see in the answer below at least adding a SetInterval will prevent the application from stopping. – nwaltham Jul 04 '13 at 10:32
  • Seems to be a related question http://stackoverflow.com/questions/5916066/node-js-tool-to-see-why-process-is-still-running – nwaltham Jul 04 '13 at 10:35

1 Answers1

1

Without access to those modules there is not very much you can do.

SetInterval will prevent your application of stopping, but you can call ClearInterval on those timers. Your challenge will be to manage those interval objects and perform cleaning if application needs to be removed.

One of hacky and straight forward solution would be to 'replace' SetInterval default method with bespoke that would register interval and act the same way as default from use point of view. That way then you will have ability to collect all interval handles easier.

moka
  • 22,846
  • 4
  • 51
  • 67