0

我正在尝试使用agenda图书馆安排工作。我已经这样做了:

await this.agenda.start()

this.agenda.define(scheduler.id, { lockLifetime: 10000 }, (job, done) => {
    console.log('Hello world!')
    //some logic using values on job variable
    done()
})

const result = await this.agenda.every(scheduler.cron, scheduler.id, scheduler, { startDate: new Date() })
await this.agenda.start() //i added start here too just in case
console.log(result)

最后一个 console.log 显示作业已成功创建,它甚至保存在数据库中,但nextRunAt出于某种原因默认为 2022 年 1 月 1 日(如下面的完整对象所示)。我尝试startDateevery()通话中添加,但它仍然显示 2022,我不知道为什么。无论cron我发送什么,它仍然不起作用。我发送给Agenda构造函数的唯一参数是数据库,与何时开始无关。谢谢!

Job {
  agenda: Agenda {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    _findAndLockNextJob: [Function: findAndLockNextJob],
    _name: undefined,
    _processEvery: 5000,
    _defaultConcurrency: 5,
    _maxConcurrency: 20,
    _defaultLockLimit: 0,
    _lockLimit: 0,
    _definitions: { '61af764f8f26fb369c76b571': [Object] },
    _runningJobs: [],
    _lockedJobs: [],
    _jobQueue: JobProcessingQueue { _queue: [] },
    _defaultLockLifetime: 600000,
    _sort: { nextRunAt: 1, priority: -1 },
    _indices: { name: 1, nextRunAt: 1, priority: -1, lockedAt: 1, disabled: 1 },
    _isLockingOnTheFly: false,
    _isJobQueueFilling: Map(0) {},
    _jobsToLock: [],
    _ready: Promise { undefined },
    _db: MongoClient {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      s: [Object],
      topology: [Topology],
      [Symbol(kCapture)]: false,
      [Symbol(options)]: [Object: null prototype]
    },
    _mdb: Db { s: [Object] },
    _collection: Collection { s: [Object] },
    _processInterval: Timeout {
      _idleTimeout: 5000,
      _idlePrev: [TimersList],
      _idleNext: [TimersList],
      _idleStart: 28573,
      _onTimeout: [Function: bound processJobs],
      _timerArgs: undefined,
      _repeat: 5000,
      _destroyed: false,
      [Symbol(refed)]: true,
      [Symbol(kHasPrimitive)]: false,
      [Symbol(asyncId)]: 221,
      [Symbol(triggerId)]: 0
    },
    [Symbol(kCapture)]: false
  },
  attrs: {
    name: '61af764f8f26fb369c76b571',
    data: {
      //stuff that i use here
    },
    priority: 0,
    type: 'single',
    nextRunAt: 2022-01-01T03:00:00.000Z, //next run at 2022
    repeatInterval: '* * * 1 *',
    repeatTimezone: null,
    startDate: 2021-12-07T14:57:19.394Z,
    endDate: null,
    skipDays: null,
    _id: new ObjectId("61af764fcb12102f92e637dc")
  }
}
4

1 回答 1

0

使用crontab我注意到问题是我的 cron 作业将在下个月的第一天运行,与 lib 无关。现在有了正确的 cron 它按预期工作

于 2021-12-07T18:58:01.353 回答