NodeJs setInterval without extending process life time
Asked Answered
C

2

5

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 compares 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.

Countertype answered 6/4, 2017 at 12:10 Comment(6)
FYI you can exit the process by process.exit()Linseylinseywoolsey
I know that, but that is no answer here because still: I do not know, when the main process is ending. It is completely async and only event driven. Btw: process.exit() also skip the event process.on("beforeExit")Countertype
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?Fabien
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 nodejsCountertype
"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?Fabien
I means what is written there. I cannot use the existing events on the other async side process so I have to find something on side of creating the interval (or within the interval function) and alternatives is a meaning of the word alternative. So what is not understandable? Please consider that I am talking about nodeJs not browser side JS.Countertype
B
18

Contrary to some of the comments 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:

Benham answered 6/4, 2017 at 12:32 Comment(0)
S
0

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.com/package/webworker-threads

Shipman answered 6/4, 2017 at 12:34 Comment(1)
.unref() would be a better option as the accepted answer suggestsShipman

© 2022 - 2024 — McMap. All rights reserved.