Skip to content Skip to sidebar Skip to footer

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 possi

Solution 1:

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

Solution 2:

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.

Solution 3:

There should not be any problem at all, but in my opinion it is just better to do this thing with a OS cron job. This will use the OS timer, will call your node app; that will be clear to everybody, even to the one who has never seen node or JavaScript in action. Also it will automatically protect you from long-term memory leaks because your app will be killed after each iteration.

Post a Comment for "Node.js Settimeout For 24 Hours - Any Caveats?"