setInterval 重置后保持旧间隔

setInterval keeping old interval after reset

我的应用每 5 小时轮询一次体育赛事。如果没有发现体育赛事,则此间隔将减少到 1 小时。我省略了文件中不相关的部分,因为它是一个巨大的文件并且与此间隔无关。您可以看到,最初如果 NODE_ENV 是生产,调用 pollEvents 的间隔是 5 小时。如果没有有效事件,setNoEventsInterval 被称为 (你可以在下面的日志中看到),这应该重置间隔 (eventPollInterval) 到 1 小时。如果在该间隔之后发现事件,则间隔重置为 5 小时。

index.js

const intervalPolls = {}
let eventPollInterval

function setNoEventsInterval() {
    logger.info('----- setNoEventsInterval -----')
    logger.info(`EVENT_POLL_RESET: ${process.EVENT_POLL_RESET}`)
    if (!process.EVENT_POLL_RESET) {
        clearInterval(eventPollInterval)
        eventPollInterval = null

        eventPollInterval = setInterval(() => {
            logger.info('----- NEW POLL (1 HOUR) -----')
            pollForEvents(_redis)
        }, 3600000)
        intervalPolls.timeSet = dayjs().add(3600000, 'millisecond')
        intervalPolls.check = setInterval(() => {
            const difference = dayjs().diff(intervalPolls.timeSet, 'minute')

            logger.info(`Time remaining: ${-difference} mins`)
        }, 300000) // 5 min
        logger.info(`eventPollInterval: ${eventPollInterval}`)
    }
    process.EVENT_POLL_RESET = true
}

async function pollForEvents(redis) {
    try {
        logger.info('----- pollForEvents -----')

        if (process.EVENT_POLL_RESET) {
            logger.info(`Re-polling after no events...`)
        }
        await redis.setConfig(config)

        const _config = await redis.getConfig()
        const bettableCompanies = _config.companies.filter(comp => {
            return comp.bettable && comp.active
        })
        if (process.EVENT_POLL_RESET) {
            logger.info(`Getting exchange events after reset...`)
            logger.info(`Bettable Companies: ${JSON.stringify(bettableCompanies.join(), null, 4)}`)
        }
        const exchangeEvents = await getExchangeEvents(bettableCompanies, redis)
        const exchangesWithEvents = exchangeEvents.filter(exchange => {
            return exchange.events.length
        })

        if (exchangeEvents.length === bettableCompanies.length && exchangesWithEvents.length > 1) {
            const sameEvents = utils.findSameEvents(exchangesWithEvents)

            if (sameEvents && sameEvents.length) {
                logger.info(`Same Events #: ${sameEvents.length}`)

                // If the interval was changed due to having no mathing events
                // Reset back to 5 hrs when does
                if (process.EVENT_POLL_RESET) {
                    clearInterval(eventPollInterval)

                    eventPollInterval = setInterval(pollForEvents, 18000000, redis)
                    process.EVENT_POLL_RESET = false
                }
                await writeMatchedEvents(sameEvents)
                await writeUnmatchedEvents(exchangesWithEvents)

                await dbApi.postMatchedEvents(sameEvents)

                process.MATCHED_EVENTS = await dbApi.getMatchedEvents()

                const nextEventStartTime = process.MATCHED_EVENTS[0].startTime
                const parsedStartTime = new Date(nextEventStartTime).toString()

                logger.info(`The next event is @ ${parsedStartTime}`)

                if (process.env.NODE_ENV === 'production') {
                    handleProduction()
                }
            } else {
                // Was originally exiting here but the docker container will just restart immediately
                // So a bit pointless. 1 hour
                logger.info('[NO MATCHED EVENTS]')

                setNoEventsInterval()
            }
        } else {
            logger.info('[NO EVENTS FROM ALL EXCHANGES/NOT ALL HAVE EVENTS]')

            setNoEventsInterval()
        }
    } catch(err) {
        throw err
    }
}

module.exports = async function(redis) {
    try {
        _redis = redis

        await pollForEvents(redis)

        if (process.env.NODE_ENV === 'production') {
            // Poll for new events every 5 hours
            clearInterval(eventPollInterval)

            eventPollInterval = setInterval(pollForEvents, 18000000, redis)
        }
    } catch (err) {
        process.exit(1)
    }
}

这些是日志:

$ node --max-old-space-size=8192 src/app.js
[2020-03-24 02:00:42] ~ INFO: ----- pollForEvents -----
[2020-03-24 02:00:44] ~ INFO: [NO EVENTS FROM ALL EXCHANGES/NOT ALL HAVE EVENTS]
[2020-03-24 02:00:44] ~ INFO: ----- setNoEventsInterval -----
[2020-03-24 02:00:44] ~ INFO: EVENT_POLL_RESET: undefined
[2020-03-24 02:00:44] ~ INFO: intervalPolls.event: [object Object]
[2020-03-24 02:05:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 02:10:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 02:15:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 02:20:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 02:25:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 02:30:44] ~ INFO: Time remaining: 29 mins
[2020-03-24 02:35:44] ~ INFO: Time remaining: 24 mins
[2020-03-24 02:40:44] ~ INFO: Time remaining: 19 mins
[2020-03-24 02:45:44] ~ INFO: Time remaining: 14 mins
[2020-03-24 02:50:44] ~ INFO: Time remaining: 9 mins
[2020-03-24 02:55:44] ~ INFO: Time remaining: 4 mins
[2020-03-24 03:00:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 03:05:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 03:10:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 03:15:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 03:20:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 03:25:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 03:30:44] ~ INFO: Time remaining: 29 mins
[2020-03-24 03:35:44] ~ INFO: Time remaining: 24 mins
[2020-03-24 03:40:44] ~ INFO: Time remaining: 19 mins
[2020-03-24 03:45:44] ~ INFO: Time remaining: 14 mins
[2020-03-24 03:50:44] ~ INFO: Time remaining: 9 mins
[2020-03-24 03:55:44] ~ INFO: Time remaining: 4 mins
[2020-03-24 04:00:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 04:05:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 04:10:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 04:15:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 04:20:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 04:25:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 04:30:44] ~ INFO: Time remaining: 29 mins
[2020-03-24 04:35:44] ~ INFO: Time remaining: 24 mins
[2020-03-24 04:40:44] ~ INFO: Time remaining: 19 mins
[2020-03-24 04:45:44] ~ INFO: Time remaining: 14 mins
[2020-03-24 04:50:44] ~ INFO: Time remaining: 9 mins
[2020-03-24 04:55:44] ~ INFO: Time remaining: 4 mins
[2020-03-24 05:00:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 05:05:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 05:10:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 05:15:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 05:20:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 05:25:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 05:30:44] ~ INFO: Time remaining: 29 mins
[2020-03-24 05:35:44] ~ INFO: Time remaining: 24 mins
[2020-03-24 05:40:44] ~ INFO: Time remaining: 19 mins
[2020-03-24 05:45:44] ~ INFO: Time remaining: 14 mins
[2020-03-24 05:50:44] ~ INFO: Time remaining: 9 mins
[2020-03-24 05:55:44] ~ INFO: Time remaining: 4 mins
[2020-03-24 06:00:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 06:05:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 06:10:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 06:15:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 06:20:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 06:25:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 06:30:44] ~ INFO: Time remaining: 29 mins
[2020-03-24 06:35:44] ~ INFO: Time remaining: 24 mins
[2020-03-24 06:40:44] ~ INFO: Time remaining: 19 mins
[2020-03-24 06:45:44] ~ INFO: Time remaining: 14 mins
[2020-03-24 06:50:44] ~ INFO: Time remaining: 9 mins
[2020-03-24 06:55:44] ~ INFO: Time remaining: 4 mins 
[2020-03-24 07:00:44] ~ INFO: ----- pollForEvents -----
[2020-03-24 07:00:44] ~ INFO: Re-polling after no events...
[2020-03-24 07:00:44] ~ INFO: Getting exchange events after reset...
[2020-03-24 07:00:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 07:00:45] ~ INFO: Same Events #: 2
[2020-03-24 07:00:45] ~ INFO: The next event is @ Tue Mar 24 2020 09:30:00 GMT+0000 (Coordinated Universal Time)
[2020-03-24 07:05:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 07:10:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 07:15:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 07:20:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 07:25:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 07:30:44] ~ INFO: Time remaining: 29 mins
[2020-03-24 07:35:44] ~ INFO: Time remaining: 24 mins
[2020-03-24 07:40:44] ~ INFO: Time remaining: 19 mins
[2020-03-24 07:45:44] ~ INFO: Time remaining: 14 mins
[2020-03-24 07:50:44] ~ INFO: Time remaining: 9 mins
[2020-03-24 07:55:44] ~ INFO: Time remaining: 4 mins
[2020-03-24 08:00:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 08:05:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 08:10:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 08:15:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 08:20:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 08:25:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 08:30:44] ~ INFO: Time remaining: 29 mins
[2020-03-24 08:35:44] ~ INFO: Time remaining: 24 mins
[2020-03-24 08:40:44] ~ INFO: Time remaining: 19 mins
[2020-03-24 08:45:44] ~ INFO: Time remaining: 14 mins
[2020-03-24 08:50:44] ~ INFO: Time remaining: 9 mins
[2020-03-24 08:55:44] ~ INFO: Time remaining: 4 mins
[2020-03-24 09:00:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 09:05:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 09:10:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 09:15:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 09:20:00] ~ INFO: New worker being forked: 5e79b01d808b70001ddd2986
[2020-03-24 09:20:00] ~ INFO: Worker 5e79b01d808b70001ddd2986 is now online [PROCESS #: 1]
[2020-03-24 09:20:01] ~ INFO: New worker being forked: 5e79b01d808b70001ddd2987
[2020-03-24 09:20:01] ~ INFO: Worker 5e79b01d808b70001ddd2987 is now online [PROCESS #: 2]
[2020-03-24 09:20:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 09:25:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 09:30:08] ~ WARN: [NO VALID MARKETS]
[2020-03-24 09:30:08] ~ INFO: Worker 5e79b01d808b70001ddd2986 died 0 [PROCESS #: 1]
[2020-03-24 09:30:09] ~ WARN: [NO VALID MARKETS]
[2020-03-24 09:30:09] ~ INFO: Worker 5e79b01d808b70001ddd2987 died 0 [PROCESS #: 0]
[2020-03-24 09:30:09] ~ INFO: No more matched events. Setting the poll to 1 hour
[2020-03-24 09:30:09] ~ INFO: ----- setNoEventsInterval -----
[2020-03-24 09:30:09] ~ INFO: EVENT_POLL_RESET: false
[2020-03-24 09:30:09] ~ INFO: intervalPolls.event: [object Object]
[2020-03-24 09:30:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 09:35:09] ~ INFO: Time remaining: 54 mins
[2020-03-24 09:35:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 09:40:09] ~ INFO: Time remaining: 49 mins
[2020-03-24 09:40:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 09:45:09] ~ INFO: Time remaining: 44 mins
[2020-03-24 09:45:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 09:50:09] ~ INFO: Time remaining: 39 mins
[2020-03-24 09:50:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 09:55:09] ~ INFO: Time remaining: 34 mins
[2020-03-24 09:55:44] ~ INFO: Time remaining: 34 mins
[2020-03-24 10:00:09] ~ INFO: Time remaining: 29 mins
[2020-03-24 10:00:44] ~ INFO: Time remaining: 29 mins
[2020-03-24 10:05:09] ~ INFO: Time remaining: 24 mins
[2020-03-24 10:05:44] ~ INFO: Time remaining: 24 mins
[2020-03-24 10:10:09] ~ INFO: Time remaining: 19 mins
[2020-03-24 10:10:44] ~ INFO: Time remaining: 19 mins
[2020-03-24 10:15:09] ~ INFO: Time remaining: 14 mins
[2020-03-24 10:15:44] ~ INFO: Time remaining: 14 mins
[2020-03-24 10:20:09] ~ INFO: Time remaining: 9 mins
[2020-03-24 10:20:44] ~ INFO: Time remaining: 9 mins
[2020-03-24 10:25:09] ~ INFO: Time remaining: 4 mins
[2020-03-24 10:25:44] ~ INFO: Time remaining: 4 mins
[2020-03-24 10:30:09] ~ INFO: ----- pollForEvents -----
[2020-03-24 10:30:09] ~ INFO: Re-polling after no events...
[2020-03-24 10:30:09] ~ INFO: Getting exchange events after reset...
[2020-03-24 10:30:09] ~ INFO: Time remaining: 59 mins
[2020-03-24 10:30:11] ~ INFO: [NO MATCHED EVENTS]
[2020-03-24 10:30:11] ~ INFO: ----- setNoEventsInterval -----
[2020-03-24 10:30:11] ~ INFO: EVENT_POLL_RESET: true
[2020-03-24 10:30:44] ~ INFO: Time remaining: 59 mins
[2020-03-24 10:35:09] ~ INFO: Time remaining: 54 mins
[2020-03-24 10:35:44] ~ INFO: Time remaining: 54 mins
[2020-03-24 10:40:09] ~ INFO: Time remaining: 49 mins
[2020-03-24 10:40:44] ~ INFO: Time remaining: 49 mins
[2020-03-24 10:45:09] ~ INFO: Time remaining: 44 mins
[2020-03-24 10:45:44] ~ INFO: Time remaining: 44 mins
[2020-03-24 10:50:09] ~ INFO: Time remaining: 39 mins
[2020-03-24 10:50:44] ~ INFO: Time remaining: 39 mins
[2020-03-24 10:55:09] ~ INFO: Time remaining: 34 mins
[2020-03-24 10:55:44] ~ INFO: Time remaining: 34 mins

从日志中可以看出,当setNoEventsInterval最初被调用时@[2020-03-2402:00:44],这将重置interval as EVENT_POLL_RESET 是错误的。但是,pollEvents 直到 [2020-03-24 07:00:44] 才被调用,也就是最初调用该函数的 5 小时后间隔是。

当没有更多有效事件时,将再次调用 setNoEventsInterval @ [2020-03-24 09:30:09] 它将重置再次间隔,但这次它起作用了,你可以看到 pollEvents 然后被称为 @ [2020-03-24 10:30:09].

为什么第一次时间隔似乎没有正常清除,但第二次却绕开了?我也对为什么 remainingMinutes 变量重置时间感到困惑,因为我不重置它的间隔。 [2020-03-24 07:55:44] & [2020-03-24 08:00:44]之间,剩余时间从 4 分钟回到 59 分钟,但据我所知,我根本没有重置 timeSet 变量

编辑

剩余的分钟数无关紧要,我认为它只是按小时滚动,我从小时开始得到分钟数。仍然对为什么不修改间隔感到困惑。

编辑 2

当间隔最初在 1 小时后解决时,我输入了更多日志记录,所以我希望记录 ----- NEW POLL (1 HOUR) ----- 但它永远不会

最后我在调试的时候弄明白了。问题出在即时函数调用中,我正在等待对 pollForEvents 的调用。如果此 returns 没有事件,或者由于某些其他原因调用 setNoEventsInterval,则间隔设置为一小时。但是,当 pollForEvents 最终解决时,如果 NODE_ENV 是 "production" 的条件再次将其设置为 5 小时。这就是为什么我只看到这种情况发生在最初的民意调查中,而不是随后的民意调查中。我刚刚添加了一个条件,如果 eventPollInterval 存在,请不要重置它。