0

If I'm writing something simple and want it to run until explicitly terminated, is there a best practice to prevent script termination without causing blocking, using CPU time or preventing callbacks from working?

I'm assuming at that point I'd need some kind of event loop implementation or a way to unblock the execution of events that come in from other async handlers (network io, message queues)?

A specific example might be something along the lines of "I want my node script to sleep until a job is available via Beanstalkd".

2 Answers 2

2

I think the relevant counter-question is "How are you checking for the exit condition?".

If you're polling a web service, then the underlying setInterval() for the poll will keep it alive until cancelled. If you're taking in input from a stream, that should keep it alive until the stream closes, etc.

Basically, you must be monitoring something in order to know whether or not you should exit. That monitoring should be the thing keeping the script alive.

Sign up to request clarification or add additional context in comments.

Comments

0

Node.js end when it have nothing else to do.

If you listen on a port, it have something to do, and a way to receive beanstalk command, so it will wait.

Create a function that close the port and you ll have your explicit exit, but it will wait for all current job to end before closing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.