0

In a server running Nodejs, I am using multiple setInterval functions, each having a relatively very long interval (24 hours, 36 hours, etc.).

setInterval(funcOne, 86400000)
...
setInterval(funcTwo, 172800000)

While they seem to work fine for now, I am a bit concerned about their reliability and performance. The server of coarse is running all the time. I am okay with restarting the functions if the server failed.

I tried to investigate about their interaction with the event loop, but it is still a bit vague for me.

  • I want to make sure that these multiple functions are not blocking the event loop no matter how many they were.
  • I am looking for possible failures or crashes due to them.
  • I am keen to understand any performance overhead they might cause, if any.

PS: I am not willing to migrate those functionalities from the server to a cloud function or a serverless app at the moment.

Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35
  • haven't you considered maybe using `cron`? (https://www.npmjs.com/package/cron) – Tibebes. M Jun 06 '20 at 15:00
  • I’m not sure I understand the specific concerns—can you elaborate? It also seems unlikely this is the best approach, but without any context, it’s impossible to say. – Dave Newton Jun 06 '20 at 15:16
  • @Tibebes.M, I actually haven't. But I am willing to explore it if it is any better. So far, I understand that cron runs all the time as well as `setInterval`! Are you thinking of any major benefits which I should search for? – Ahmed Hammad Jun 06 '20 at 18:38
  • @DaveNewton My concerns are regarding whether there are problems in this approach or not, will it affect other code pieces trying to get executed for example? Will it slow the server for a reason or another? – Ahmed Hammad Jun 06 '20 at 18:41
  • @DaveNewton Would you please illustrate what context should I provide? – Ahmed Hammad Jun 06 '20 at 18:41
  • Anything. Timers are essentially free, but there are other mechanisms better suited to long delays (including explicitly in Node). Scheduling is almost always better left to schedulers rather than rolling your own. YMMV, and it really depends on the exact requirements. – Dave Newton Jun 06 '20 at 19:36

0 Answers0