本文介绍了Node.js setTimeout 24小时 - 任何警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,我想在 Node.js 中设置24或12小时超时,定期(每天一次或两次)检查一些数据库数据并清除可疑垃圾,如果有的话。

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.

是否有任何可能的问题或性能问题,由设置巨大的超时引起,我需要注意?我不介意它是不是确切的12-24小时毫秒,并不介意在服务器崩溃时失去此超时,因为我将在服务器启动时运行相同的垃圾收集器。

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.

结论:


  • 我没有使用本机OS cron来运行我需要的单独脚本访问此脚本中的当前Node.js流程数据。

  • 最后我决定使用
    包在特定时间(可能是服务器负载较低时)进行转换的能力。

  • 感谢大家快速回复!

  • 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/cronpackage for its ability to shedule at specific time (presumably on time of low server load).
  • Thanks everyone for quick responses!

推荐答案

我已成功使用包。它使用简单,通常与CronTab兼容。我有一份工作每月运行一次,自去年以来一直有效,所以我可以证明这一点。

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.

话虽这么说,这个套餐最终确实如此只需在内部使用 setTimeout ,这样就没有真正的问题。如果您的超时数量太大(大于最大JavaScript整数),则可能存在问题,但 1000 * 60 * 60 * 24 明显小于此。

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.

你也可以直接使用 crontab 如果它可用(或Windows任务调度)。

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

这篇关于Node.js setTimeout 24小时 - 任何警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:52
查看更多