简体   繁体   English

Node.js,命令行数据处理,一下子排队43200个setTimeouts?

[英]Node.js, command line data processing, queue up 43,200 setTimeouts at once?

I want to process an event every 10 seconds, exactly on the 10 second mark all day every weekday.我想每 10 秒处理一次事件,正好在每个工作日全天的 10 秒标记上。 More than about a second late or early is a problem.迟到或早到一秒以上都是问题。

I'll manually start the app up every Sunday evening, and shut it down every Saturday morning.我会在每个星期天晚上手动启动应用程序,并在每个星期六早上关闭它。 There are 43,200 x 10 second blocks for a five day week.五天一周有 43,200 x 10 秒块。

I already have an array with all those 43,200 times in it (needed for other parts of the app).我已经有一个数组,其中包含所有这 43,200 次(应用程序的其他部分需要)。

I could potentially do this with setInterval, or with a setTimeout where the callback sets another setTimeout afterwards.我可能会使用 setInterval 或 setTimeout 来执行此操作,其中回调会在之后设置另一个 setTimeout。 I'm having issues with both those -- for a separate question/post if the following won't work.我对这两个问题都有疑问——如果以下内容不起作用,请单独提问/发帖。

So third option: 14,200x setTimeout all racked up at once.所以第三个选项:14,200x setTimeout 一次全部累积。 If I start the app on Sunday evening and run this:如果我在周日晚上启动应用程序并运行:

datesArray.forEach(theDate => {
    ms = theDate.valueOf() - Date.Now();
    setTimeout(eventFn, ms)
}

where datesArray has 14K elements... are there technical limits to where a large number of setTimeouts break down or drift, or anything else? datesArray 有 14K 个元素的地方......是否存在大量 setTimeouts 崩溃或漂移或其他任何地方的技术限制?

More specifically:进一步来说:

  1. Assuming my hardware has enough resources to handle each event when it happens, can racking up a lot of setTimeouts at once break anything, overload anything, cause them to mess with each other in any way, etc?假设我的硬件有足够的资源来处理每个事件发生时,是否可以同时设置大量 setTimeouts 来破坏任何东西、使任何东西过载、导致它们以任何方式相互干扰等等?
  2. Is setTimeout reliable enough such that an event running 4-5 days after it's set up, will run at the exact right time, or is there any chance it will drift or anything like that? setTimeout 是否足够可靠,以至于在设置后运行 4-5 天的事件将在准确的正确时间运行,或者它是否有可能漂移或类似的事情?
  3. Are there any other reasons this would be a bad idea?还有其他原因这不是一个好主意吗? Or... is this a technically sound approach?或者......这是一种技术上合理的方法吗?

Please note, SO doesn't like opinion-based questions.请注意,SO 不喜欢基于意见的问题。 I'm not asking people's opinions here on whether you like this idea or not, or whether there are other ways to do this.我不是在这里询问人们是否喜欢这个想法,或者是否有其他方法可以做到这一点。 Feel free to volunteer any of that if you want, but specifically, I'm asking if this is technically sound, based on limits with JS, node, setTimeout, etc. as they're documented/technically defined (assuming they are, somewhere)?如果你愿意,可以随意自愿参与其中,但具体来说,我想问的是这在技术上是否合理,基于 JS、节点、setTimeout 等的限制,因为它们被记录/技术定义(假设它们在某处)?

Thanks!谢谢!

I share your concerns about scheduling thousands of timers.我和你一样担心安排数千个计时器。

The desired behavior could be achieved in the following way:可以通过以下方式实现所需的行为:

// 43200 Date objects
const dates = [
    new Date(2023, 0, 22, 12, 56, 0),
    new Date(2023, 0, 22, 12, 56, 10),
    new Date(2023, 0, 22, 12, 56, 20),
    new Date(2023, 0, 22, 12, 56, 30),
    new Date(2023, 0, 22, 12, 56, 40),
];

const eventFn = () => {
    console.log('event', new Date());
};

let nowDate = new Date();
let dateIndex = dates.findIndex((date) => nowDate < date);

const scheduleNextRun = (scheduled) => {
    if (scheduled) {
        eventFn();
    }
    if (dateIndex >= 0 && dateIndex < dates.length) {
        nowDate = new Date();
        setTimeout(() => scheduleNextRun(true), dates[dateIndex].getTime() - nowDate.getTime());
        dateIndex ++;
    }
};

scheduleNextRun(false);

The output of my test run was:我的测试运行的 output 是:

event 2023-01-22T01:56:10.003Z
event 2023-01-22T01:56:20.001Z
event 2023-01-22T01:56:30.001Z
event 2023-01-22T01:56:40.001Z

The error is between 1-3 msec which sounds acceptable based on your requirements.误差在 1-3 毫秒之间,根据您的要求这听起来是可以接受的。

Benefits of this approach:这种方法的好处:

  • Code can be started at any moment, and it will find the next run date/time代码可以随时启动,它会找到下一次运行的日期/时间
  • Only one timer is scheduled at a time一次只安排一个计时器
  • Lightweight and easy to follow轻巧且易于遵循
  • Stops automatically after all events executed所有事件执行后自动停止

Please let me know if this helps.请让我知道这可不可以帮你。

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

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