【问题标题】:Run all past jobs at once一次运行所有过去的作业
【发布时间】:2019-09-20 02:03:45
【问题描述】:

我正在使用node-schedule 创建一个基于数组的作业队列。但是,我注意到 node-schedule 只会在未来运行作业。

为了证明这一点,我创建了一个最小可行示例:

const schedule = require('node-schedule');

function randomDate(start, end) {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}

let start = new Date()
let end = new Date()
start.setMinutes(start.setMinutes() - 35) // set job 35 Minutes in the past
end.setSeconds(end.setMinutes() - 10) // set job 10 Minutes in the past

/**
 * Create Scheduler
 */
let arr = []
const numb_jobs = 50
for (let i = 0; i < numb_jobs; i++) {
  d = randomDate(start, end)
  arr.push([i, d])

  schedule.scheduleJob(arr[i][1], function () {
    console.log('Job  -- #' + arr[i][0] + ' -- executed at: ' + arr[i][1]);
  });
}

脚本运行时如何在队列开头运行所有过去的作业有什么建议吗?

感谢您的回复!

【问题讨论】:

  • 我认为这是一个合理的行为。时间点(过去)从未到达,因此任务从未运行。
  • 你有具体的用例吗?
  • @appleapple 感谢您的回复!我正在使用电子开发,当有人启动应用程序时,所有过去的工作都应该运行。
  • 那么也许自己执行那些?很容易说出应该运行哪个任务。
  • 或注册(长期)服务? (不过,您仍然需要处理断电时间。)

标签: javascript node.js node-schedule


【解决方案1】:

scheduleJob 返回一个作业对象。 您可以取消/重新安排并从 Job::invocations 数据中获取数据。

https://github.com/node-schedule/node-schedule/blob/master/lib/schedule.js

https://www.npmjs.com/package/node-schedule#jobreschedulespec

【讨论】:

    猜你喜欢
    • 2018-07-02
    • 2022-10-05
    • 2011-06-20
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多