简体   繁体   English

NodeJs上的长setTimeout被事件队列忽略

[英]Long setTimeout on NodeJs gets ignored by event queue

I have a long-running nodejs (v.10.16) application that occasionally needs to schedule an event to fire one to two months in the future. 我有一个长期运行的nodejs(v.10.16)应用程序,偶尔需要安排一个事件在未来一到两个月内触发。 Unfortunately, this exceeds the maximum length of time that a timeout can wait. 不幸的是,这超出了超时可以等待的最大时间长度。 I have tried using various different packages (long-timeout) but they didn't seem to work for me. 我尝试使用各种不同的软件包(长时间超时),但它们似乎对我不起作用。

Essentially, the code I've settled on looks something like the below 本质上,我所确定的代码如下所示

function runInFuture(millisecondsInFuture,callback) {
    if(millisecondsInFuture > MAX_TIMEOUT) { // max timeout === 2^31-1 https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_args
        setTimeout(()=>{
            runInFuture(millisecondsInFuture - MAX_TIMEOUT,callback); //for simplicity, code to ensure no errors from floating point numbers excluded
        },MAX_TIMEOUT - 1);
     } else {  // timing is within setTimeout's ability to run 
        setTimeout(()=>{
            callback();
        },millisecondsInFuture);
     }
}

From what I understand, the above function should keep calling itself recursively every ~24.86 days until the event is expected to happen in less than that time, after which it will fire the callback. 据我了解,以上函数应每隔约24.86天递归调用一次,直到预计该事件在不到该时间发生之前,此后它将触发回调。 In actual practice, the callback doesn't get fired. 实际上,不会触发回调。 Is there any setting on nodejs I'm not following? 我没有关注的nodejs上有任何设置吗? Any tips on where to locate the bug? 有关在哪里找到错误的任何提示? Or, is it possible to somehow 'speed up' the system clock so I don't have to wait 25 days to find out that my code isn't working? 或者,是否有可能以某种方式“加速”系统时钟,这样我就不必等待25天才能发现我的代码不起作用?

I know you said you tried a few libraries, but Agenda seems like it would serve your purposes well. 我知道您说您尝试过一些图书馆,但议程似乎可以很好地满足您的目的。 As you are aware setTimeout has a 32 bit limit of just under 25 days, and is vulnerable to system failure due to only being stored in memory. 如您所知, setTimeout具有不到25天的32位限制,并且由于仅存储在内存中而容易受到系统故障的影响。 It is possible to restore from a reference to an Agenda schedule upon server restart if needed. 如果需要,可以在服务器重新启动时从参考时间表恢复到日程表。

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

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