Cron 是 运行 node-cron 的 2 倍
Cron is running 2 times node-cron
我安装了 node-cron 库并在同一个文件中配置了 2 个 crons 运行 以不同的时间间隔。第一个 cron 运行s 每 45 秒一次,第二个 cron 运行s 每 60 秒一次,它工作得很好。当 60 秒的 cron 为 运行 时会出现问题,而 45 秒的 cron 也会自动再次为 运行(请注意差异为 15 秒)。为什么会这样?
const cron = require('node-cron');
cron.schedule('*/45 * * * * *', async () => {
console.log('running 45 seconds')
})
cron.schedule('*/60 * * * * *', async () => {
console.log('running 60 seconds')
})
Cron 将 运行 在“每 45 秒,每分钟...”执行此操作。
从 0 秒开始,然后是 45 秒。
从 0 秒开始,然后是 45 秒。
只要 */x
间隔不能被父时间单位整除(一分钟秒数,一小时分钟数等),这就会很明显。
例如,*/17 * * * * *
2021-08-17T23:07:00.230Z running */17 seconds
2021-08-17T23:07:17.273Z running */17 seconds
2021-08-17T23:07:34.302Z running */17 seconds
2021-08-17T23:07:51.344Z running */17 seconds
2021-08-17T23:08:00.374Z running */17 seconds
2021-08-17T23:08:17.424Z running */17 seconds
2021-08-17T23:08:34.467Z running */17 seconds
2021-08-17T23:08:51.494Z running */17 seconds
2021-08-17T23:09:00.525Z running */17 seconds
2021-08-17T23:09:17.564Z running */17 seconds
要通过 cron 在 45 秒计时器上进行 运行 操作,您可能需要每 15 秒执行一次任务,然后需要一些逻辑来检查之前的 运行s。
我安装了 node-cron 库并在同一个文件中配置了 2 个 crons 运行 以不同的时间间隔。第一个 cron 运行s 每 45 秒一次,第二个 cron 运行s 每 60 秒一次,它工作得很好。当 60 秒的 cron 为 运行 时会出现问题,而 45 秒的 cron 也会自动再次为 运行(请注意差异为 15 秒)。为什么会这样?
const cron = require('node-cron');
cron.schedule('*/45 * * * * *', async () => {
console.log('running 45 seconds')
})
cron.schedule('*/60 * * * * *', async () => {
console.log('running 60 seconds')
})
Cron 将 运行 在“每 45 秒,每分钟...”执行此操作。
从 0 秒开始,然后是 45 秒。 从 0 秒开始,然后是 45 秒。
只要 */x
间隔不能被父时间单位整除(一分钟秒数,一小时分钟数等),这就会很明显。
例如,*/17 * * * * *
2021-08-17T23:07:00.230Z running */17 seconds
2021-08-17T23:07:17.273Z running */17 seconds
2021-08-17T23:07:34.302Z running */17 seconds
2021-08-17T23:07:51.344Z running */17 seconds
2021-08-17T23:08:00.374Z running */17 seconds
2021-08-17T23:08:17.424Z running */17 seconds
2021-08-17T23:08:34.467Z running */17 seconds
2021-08-17T23:08:51.494Z running */17 seconds
2021-08-17T23:09:00.525Z running */17 seconds
2021-08-17T23:09:17.564Z running */17 seconds
要通过 cron 在 45 秒计时器上进行 运行 操作,您可能需要每 15 秒执行一次任务,然后需要一些逻辑来检查之前的 运行s。