feat(cron): better API (#40)
This commit is contained in:
parent
179aadafa4
commit
be3b684866
@ -13,17 +13,20 @@ Installation of the [npm package](https://npmjs.org/package/@xen-orchestra/cron)
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import * as Cron from '@xen-orchestra/cron'
|
||||
import { createSchedule } from '@xen-orchestra/cron'
|
||||
|
||||
Cron.parse('* * * jan,mar *')
|
||||
// → { month: [ 1, 3 ] }
|
||||
const schedule = createSchedule('0 0 * * sun', 'America/New_York')
|
||||
|
||||
Cron.next('* * * jan,mar *', 2, 'America/New_York')
|
||||
// → [ 2018-01-19T22:15:00.000Z, 2018-01-19T22:16:00.000Z ]
|
||||
schedule.next(2)
|
||||
// [ 2018-02-11T05:00:00.000Z, 2018-02-18T05:00:00.000Z ]
|
||||
|
||||
const stop = Cron.schedule('@hourly', () => {
|
||||
const job = schedule.createJob(() => {
|
||||
console.log(new Date())
|
||||
}, 'UTC+05:30')
|
||||
})
|
||||
|
||||
job.start()
|
||||
|
||||
job.stop()
|
||||
```
|
||||
|
||||
### Pattern syntax
|
||||
|
@ -1,40 +1,49 @@
|
||||
import { DateTime } from 'luxon'
|
||||
|
||||
import nextDate from './next'
|
||||
import next from './next'
|
||||
import parse from './parse'
|
||||
|
||||
const autoParse = pattern =>
|
||||
typeof pattern === 'string' ? parse(pattern) : schedule
|
||||
|
||||
export const next = (cronPattern, n = 1, zone = 'utc') => {
|
||||
const schedule = autoParse(cronPattern)
|
||||
|
||||
let date = DateTime.fromObject({ zone })
|
||||
const dates = []
|
||||
for (let i = 0; i < n; ++i) {
|
||||
dates.push((date = nextDate(schedule, date)).toJSDate())
|
||||
}
|
||||
return dates
|
||||
}
|
||||
|
||||
export { parse }
|
||||
|
||||
export const schedule = (cronPattern, fn, zone = 'utc') => {
|
||||
const wrapper = () => {
|
||||
fn()
|
||||
scheduleNextRun()
|
||||
class Job {
|
||||
constructor (schedule, fn) {
|
||||
const wrapper = scheduledRun => {
|
||||
if (scheduledRun) {
|
||||
fn()
|
||||
}
|
||||
this._timeout = setTimeout(wrapper, schedule.next() - Date.now(), true)
|
||||
}
|
||||
this._fn = wrapper
|
||||
this._timeout = undefined
|
||||
}
|
||||
|
||||
let handle
|
||||
const schedule = autoParse(cronPattern)
|
||||
const scheduleNextRun = () => {
|
||||
const now = DateTime.fromObject({ zone })
|
||||
const nextRun = next(schedule, now)
|
||||
handle = setTimeout(wrapper, nextRun - now)
|
||||
start () {
|
||||
this.stop()
|
||||
this._fn()
|
||||
}
|
||||
|
||||
scheduleNextRun()
|
||||
return () => {
|
||||
clearTimeout(handle)
|
||||
stop () {
|
||||
clearTimeout(this._timeout)
|
||||
}
|
||||
}
|
||||
|
||||
class Schedule {
|
||||
constructor (pattern, zone = 'utc') {
|
||||
this._schedule = parse(pattern)
|
||||
this._dateTimeOpts = { zone }
|
||||
}
|
||||
|
||||
createJob (fn) {
|
||||
return new Job(this, fn)
|
||||
}
|
||||
|
||||
next (n) {
|
||||
const dates = new Array(n)
|
||||
const schedule = this._schedule
|
||||
let date = DateTime.fromObject(this._dateTimeOpts)
|
||||
for (let i = 0; i < n; ++i) {
|
||||
dates[i] = (date = next(schedule, date)).toJSDate()
|
||||
}
|
||||
return dates
|
||||
}
|
||||
}
|
||||
|
||||
export const createSchedule = (...args) => new Schedule(...args)
|
||||
|
Loading…
Reference in New Issue
Block a user