Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js setTimeout for 24 hours - any caveats?

Simple question, I want to set 24 or 12 hours timeout in Node.js to periodically (once or twice a day) check some db data and clean suspicious garbage, if any.

Is there any possible problems or performance issues, caused by setting huge timeout, that i need to be aware of? I don't mind if it's not exact 12-24hr in ms, and don't mind loosing this timeout on server crash, as I will run same garbage collector on server startup anyway.

Conclusion:

  • I'm not using native OS cron to run separate script as I need to access current Node.js process data inside this script.
  • In the end I decided to use https://www.npmjs.com/package/cron package for its ability to shedule at specific time (presumably on time of low server load).
  • Thanks everyone for quick responses!
like image 888
Max Yari Avatar asked May 07 '15 14:05

Max Yari


People also ask

Does setTimeout affect performance?

No significant effect at all, setTimeout runs in an event loop, it doesn't block or harm execution.

Is there a limit to setTimeout?

Browsers including Internet Explorer, Chrome, Safari, and Firefox store the delay as a 32-bit signed integer internally. This causes an integer overflow when using delays larger than 2,147,483,647 ms (about 24.8 days), resulting in the timeout being executed immediately.

Is js setTimeout blocking?

Explanation: setTimeout() is non-blocking which means it will run when the statements outside of it have executed and then after one second it will execute.

Does setTimeout work in node JS?

The setTimeout function is used to call a function after the specified number of milliseconds. The delay of the called function begins after the remaining statements in the script have finished executing. The setTimeout function is found in the Timers module of Node. js.


2 Answers

I've had success using the package cron. It's simple to use and is generally compatible with a CronTab. I've had a job that runs once a month and this has worked consistently since last year, so I can attest to it.

That being said, this package ultimately does just use setTimeout internally so there's no real issue with you doing that. If your timeout number is too large (larger than the maximum JavaScript integer) there may be a problem, but 1000 * 60 * 60 * 24 is significantly smaller than that.

Obviously if your system goes down or the script crashes for some other reason the timeout won't work.

You could also just use crontab directly if it's available (or Windows task scheduling).

like image 69
Explosion Pills Avatar answered Sep 20 '22 11:09

Explosion Pills


Personally, I would use a cron job to do this sort of thing (in Unix/Linux), or a "scheduled task" in Windows. In any case, the work would be done entirely on the server, by the server ... and thus, there's really no reason to have a JavaScript app (on "some other" computer) to be involved with it.

More generally: "no, don't tell someone to 'go to sleep for 12 hours,' somehow trusting that this means s/he will wake up in time." Instead, use an alarm-clock. Calculate the absolute-time at which the activity should [next] occur, then see to it that the activity does occur "not-sooner." Arrange for the computer that actually needs to do the work, to do the work at the appropriate time, using whatever scheduling facilities are available on that computer.

like image 35
Mike Robinson Avatar answered Sep 22 '22 11:09

Mike Robinson