Compare commits

..

11 Commits

Author SHA1 Message Date
Julien Fontanet
1f283d285a feat(cron): 0.2.0 2018-02-06 17:30:36 +01:00
Julien Fontanet
adaa7ca413 feat(cron): wait promise before next run (#47) 2018-02-06 17:29:28 +01:00
Julien Fontanet
247e797d2b feat(cron): 0.1.2 2018-02-06 15:42:26 +01:00
Julien Fontanet
9c7c030b00 fix(cron): handle setTimeout max delay (#45) 2018-02-06 15:41:19 +01:00
Julien Fontanet
867c9ea66c fix(cron): correctly compute the delay 2018-02-06 15:28:54 +01:00
Julien Fontanet
fd4504f0ee chore(xen-api): tested with XS 7.2 & 7.3 2018-02-06 14:36:33 +01:00
Julien Fontanet
6c6aacc64e feat(cron): 0.1.1
New release due to publish issue.
2018-02-06 12:04:49 +01:00
Julien Fontanet
678adc0726 feat(cron): 0.1.0 2018-02-06 10:22:17 +01:00
Julien Fontanet
be3b684866 feat(cron): better API (#40) 2018-02-06 10:20:11 +01:00
Julien Fontanet
179aadafa4 feat(xo-server): update value-matcher to 0.1.0 2018-02-05 15:52:05 +01:00
Julien Fontanet
f9dc129601 chore(value-matcher): remove unused @babel/polyfill 2018-02-05 15:47:24 +01:00
6 changed files with 72 additions and 41 deletions

View File

@@ -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
```

View File

@@ -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": [

View File

@@ -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)

View File

@@ -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",

View File

@@ -4,6 +4,8 @@
Tested with:
- XenServer 7.3
- XenServer 7.2
- XenServer 7.1
- XenServer 7
- XenServer 6.5

View File

@@ -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",