Node.js setTimeout for 24 hours - any caveats?

后端 未结 3 1748
旧巷少年郎
旧巷少年郎 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:55

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

提交回复
热议问题