简体   繁体   English

Node.js setTimeout 24小时 - 任何警告?

[英]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. 简单的问题,我想在Node.js设置24或12小时的超时,以定期(每天一次或两次)检查一些数据库数据并清除可疑垃圾(如果有的话)。

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. 我不介意它是不是确切的12-24小时毫秒,并不介意在服务器崩溃时失去这个超时,因为我将在服务器启动时运行相同的垃圾收集器。

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. 我没有使用本机OS cron来运行单独的脚本,因为我需要访问此脚本中的当前Node.js进程数据。
  • 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). 最后,我决定使用https://www.npmjs.com/package/cron软件包,以便在特定时间(可能是在服务器负载较低的情况下)进行转发。
  • Thanks everyone for quick responses! 谢谢大家快速回复!

I've had success using the package cron . 我已经成功使用了包cron It's simple to use and is generally compatible with a CronTab. 它使用简单,通常与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. 话虽这么说,这个包最终只是在内部使用setTimeout所以你没有真正的问题。 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. 如果您的超时数量太大(大于最大JavaScript整数),则可能存在问题,但1000 * 60 * 60 * 24明显小于此值。

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). 如果可用(或Windows任务调度),您也可以直接使用crontab

Personally, I would use a cron job to do this sort of thing (in Unix/Linux), or a "scheduled task" in Windows. 就个人而言,我会使用cron作业来执行此类操作(在Unix / Linux中)或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. 在任何情况下,工作都将完全在服务器上,由服务器完成......因此,没有理由让JavaScript应用程序(在“其他”计算机上)参与其中。

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." 更一般地说: “不,不要告诉别人'去睡12个小时,'不知怎的,相信这意味着他/她会及时醒来。” 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. 安排实际需要完成工作的计算机,使用该计算机上可用的任何调度工具在适当的时间完成工作。

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. 应该没有任何问题,但在我看来,用OS cron工作做这件事情会更好。 This will use the OS timer, will call your node app; 这将使用OS计时器,将调用您的节点应用程序; that will be clear to everybody, even to the one who has never seen node or JavaScript in action. 这对所有人来说都是清楚的,即使对于那些从未见过节点或JavaScript的人来说也是如此。 Also it will automatically protect you from long-term memory leaks because your app will be killed after each iteration. 此外,它会自动保护您免受长期内存泄漏,因为您的应用程序将在每次迭代后被终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM