Node.js setTimeout for 24 hours - any caveats?

后端 未结 3 1736
旧巷少年郎
旧巷少年郎 2020-12-20 14:15

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.

3条回答
  •  囚心锁ツ
    2020-12-20 14:43

    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.

提交回复
热议问题