I simplified it to better understand what I need: The task that I have to do is, that we have a long unknown nodejs process (with a lot of queued async functions where we never know when they are finished or something like that) and we want to have an update process, that we store current process state in database. For that we have a start up (it stores "start") and a end process that is on top of process.on('beforeExit', function () { ... });
. Now we have to handle the "still in running" process that is requested by our customer. For that we want to update the state every ten minutes to running with timestamp (this function already exists and is called state.setRunningState()
Now I have the problem how can I trigger that function every ten minutes. For that I was going the approach to trigger on each event of the working process this function and pares if it is older then 10 minutes ago. Problem is: Some times there are much more time without any event. So second option is setInterval()
and here is what my question is about: If I use setInterval my nodejs process will never reach an end, so the process will run endless until Interval is cleared. But I also do never know when I should call clearInterval()
So the Question is: Is there a way to create such a timeout without extending the life time of the nodejs process. If everything is done it should end and ignore the rest of the interval.
I simplified it to better understand what I need: The task that I have to do is, that we have a long unknown nodejs process (with a lot of queued async functions where we never know when they are finished or something like that) and we want to have an update process, that we store current process state in database. For that we have a start up (it stores "start") and a end process that is on top of process.on('beforeExit', function () { ... });
. Now we have to handle the "still in running" process that is requested by our customer. For that we want to update the state every ten minutes to running with timestamp (this function already exists and is called state.setRunningState()
Now I have the problem how can I trigger that function every ten minutes. For that I was going the approach to trigger on each event of the working process this function and pares if it is older then 10 minutes ago. Problem is: Some times there are much more time without any event. So second option is setInterval()
and here is what my question is about: If I use setInterval my nodejs process will never reach an end, so the process will run endless until Interval is cleared. But I also do never know when I should call clearInterval()
So the Question is: Is there a way to create such a timeout without extending the life time of the nodejs process. If everything is done it should end and ignore the rest of the interval.
Share Improve this question asked Apr 6, 2017 at 12:10 Jessica WeberJessica Weber 1322 silver badges11 bronze badges 6-
FYI you can exit the process by
process.exit()
– E. Sundin Commented Apr 6, 2017 at 12:12 - I know that, but that is no answer here because still: I do not know, when the main process is ending. It is pletely async and only event driven. Btw: process.exit() also skip the event process.on("beforeExit") – Jessica Weber Commented Apr 6, 2017 at 12:14
-
I've read this question like 3 times and I still can't understand what you are asking... "Is there a way to create such a timeout without extending the life time of the nodejs process" <-- So, you don't want an interval? Have you had a look at
setTimeOut
instead? "But I also do never know when I should call clearInterval()" <-- So, it's event-based, but you don't have an event that will help you to know when to clear the interval? And you expect other people to know? – Josep Commented Apr 6, 2017 at 12:21 - Again: I have to solve it on side of the interval. setTimeout will not change that because it has to be used recursive. The problem is here the behaviour of setTimeout. Means I need an alternative to basic timeout handling of nodejs – Jessica Weber Commented Apr 6, 2017 at 12:23
- "I have to solve it on side of the interval." <-- What does that even mean? You need to solve it inside the interval function? "I need an alternative to basic timeout handling of nodejs" <--- And this? What does this mean? – Josep Commented Apr 6, 2017 at 12:27
2 Answers
Reset to default 18Contrary to some of the ments here, this is not a strange requirement to have something executed periodically while the process is running without making the process run infinitely which would make it quite pointless.
There is a built-in mechanism for that. If you don't want your interval (or timeout) to stop the process from exiting then you need to use the .unref()
method.
Instead of:
setInterval(() => {
console.log('Interval');
}, 1000);
use:
setInterval(() => {
console.log('Interval');
}, 1000).unref();
and your interval will not stop the process from exiting if there are no other events pending.
Try running this example:
setInterval(() => {
console.log('Interval');
}, 1000).unref();
setTimeout(() => {
console.log('Timeout 1');
}, 3000);
setTimeout(() => {
console.log('Timeout 2');
}, 5000);
See the docs:
- https://nodejs/api/timers.html#timers_timeout_unref
Let me see if I get this straight. 1- You want to trigger an event every x mins unless if the existing process has ended? 2- You say that node would not quit the process as long as there is a running set interval. 3- you say that since your process runs " lot of queued async functions" you cannot know when the process should end
I think the simplest solution would be to just set another interval to run at a higher frequency and to clear the interval if all functions have returned. Otherwise you might be interested in reading about webworkers
https://www.npmjs./package/webworker-threads