Compare commits
11 Commits
value-matc
...
cron-v0.2.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f283d285a | ||
|
|
adaa7ca413 | ||
|
|
247e797d2b | ||
|
|
9c7c030b00 | ||
|
|
867c9ea66c | ||
|
|
fd4504f0ee | ||
|
|
6c6aacc64e | ||
|
|
678adc0726 | ||
|
|
be3b684866 | ||
|
|
179aadafa4 | ||
|
|
f9dc129601 |
@@ -13,19 +13,25 @@ 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()
|
||||
```
|
||||
|
||||
> If the scheduled job returns a promise, its resolution (or
|
||||
> rejection) will be awaited before scheduling the next run.
|
||||
|
||||
### Pattern syntax
|
||||
|
||||
```
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "@xen-orchestra/cron",
|
||||
"version": "0.0.0",
|
||||
"version": "0.2.0",
|
||||
"license": "ISC",
|
||||
"description": "Focused, well maintained, cron parser/scheduler",
|
||||
"keywords": [
|
||||
|
||||
@@ -1,40 +1,66 @@
|
||||
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
|
||||
const MAX_DELAY = 2 ** 31 - 1
|
||||
|
||||
export const next = (cronPattern, n = 1, zone = 'utc') => {
|
||||
const schedule = autoParse(cronPattern)
|
||||
class Job {
|
||||
constructor (schedule, fn) {
|
||||
const wrapper = () => {
|
||||
const result = fn()
|
||||
let then
|
||||
if (result != null && typeof (then = result.then) === 'function') {
|
||||
then.call(result, scheduleNext, scheduleNext)
|
||||
} else {
|
||||
scheduleNext()
|
||||
}
|
||||
}
|
||||
const scheduleNext = () => {
|
||||
const delay = schedule._nextDelay()
|
||||
this._timeout = delay < MAX_DELAY
|
||||
? setTimeout(wrapper, delay)
|
||||
: setTimeout(scheduleNext, MAX_DELAY)
|
||||
}
|
||||
|
||||
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()
|
||||
this._scheduleNext = scheduleNext
|
||||
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._scheduleNext()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
_nextDelay () {
|
||||
const now = DateTime.fromObject(this._dateTimeOpts)
|
||||
return next(this._schedule, now) - now
|
||||
}
|
||||
}
|
||||
|
||||
export const createSchedule = (...args) => new Schedule(...args)
|
||||
|
||||
@@ -23,9 +23,7 @@
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/polyfill": "7.0.0-beta.39"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "7.0.0-beta.39",
|
||||
"@babel/core": "7.0.0-beta.39",
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
Tested with:
|
||||
|
||||
- XenServer 7.3
|
||||
- XenServer 7.2
|
||||
- XenServer 7.1
|
||||
- XenServer 7
|
||||
- XenServer 6.5
|
||||
|
||||
@@ -106,7 +106,7 @@
|
||||
"through2": "^2.0.3",
|
||||
"tmp": "^0.0.33",
|
||||
"uuid": "^3.0.1",
|
||||
"value-matcher": "^0.0.0",
|
||||
"value-matcher": "^0.1.0",
|
||||
"ws": "^4.0.0",
|
||||
"xen-api": "^0.16.4",
|
||||
"xml2js": "^0.4.19",
|
||||
|
||||
Reference in New Issue
Block a user