-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorc.js
149 lines (134 loc) · 3.07 KB
/
norc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const fs = require('fs')
const EventEmitter = require('events')
class Norc extends EventEmitter {
constructor() {
super()
this.cronjobs = new Map()
this.cronTimeout = null
this.changeStore(this.createMemStore())
}
static cronspecParse(cronspec) {
return ['year', 'month', 'day', 'hour', 'minute', 'dow']
.map(x => cronspec[x])
.map(x => x === undefined || x === null ? '*' : `${x}`)
.map(x => x.split(','))
.map(x => x.map(x => {
let step
[x, step] = x.split('/')
step = step || 1
if (x === '*') return [-Infinity, Infinity, step]
if (x.indexOf('-') === -1) x = `${+x}-${+x}`
return x.split('-').map(x => +x).concat([+step])
}))
}
static currentRange() {
const d = new Date()
return [
d.getFullYear(),
d.getMonth() + 1,
d.getDate(),
d.getHours(),
d.getMinutes(),
d.getDay() === 0 ? 7 : d.getDay(),
]
}
static inRange(cron, range) {
return cron.every((r, i) => r.some(r => r[0] <= range[i] && r[1] >= range[i] && range[i] % r[2] === 0))
}
createMemStore() {
let lastTs = 0
return (ts) => new Promise(resolve => {
if (ts === lastTs) {
return resolve(false)
}
lastTs = ts
return resolve(true)
})
}
createFileStore(filename) {
const createFile = () => new Promise((resolve, reject) => {
fs.writeFile(filename, '', (err) => {
if (err) return reject(err)
resolve(+new Date())
})
})
const updateMtime = (ts) => new Promise((resolve, reject) => {
fs.utimes(filename, new Date(ts), new Date(ts), (err) => {
if (err) return reject(err)
resolve(true)
})
})
const statFile = () => new Promise((resolve, reject) => {
fs.stat(filename, (err, stats) => {
if (err && err.code !== 'ENOENT') {
return reject(err)
} else if (err) {
return resolve(createFile())
} else {
return resolve(+stats.mtime)
}
})
})
return (ts) => statFile()
.then(mtime => {
if (mtime !== ts) {
return updateMtime(ts)
} else {
return false
}
})
}
changeStore(store) {
this.storageMethod = store
}
// cronspec = {
// year: 2017
// month: 8
// day: 28
// hour: 10
// minute: 10
// dow: 1 1 = monday
// }
addJob(name, cronspec, job) {
this.cronjobs.set(name, {
cronspec: Norc.cronspecParse(cronspec),
job: job,
})
if (this.cronTimeout === null) {
this.cronGo()
}
}
removeJob(name) {
this.cronjobs.delete(name)
if (this.cronjobs.size === 0) {
this.cronStop()
}
}
changeStore(store) {
this.storageMethod = store
}
cronGo() {
const d = new Date()
this.cronTimeout = setTimeout(() => this.cronGo(), 1000 * (60 - d.getSeconds()) - d.getMilliseconds() + 1)
let cr = Norc.currentRange()
this.storageMethod(+new Date(cr[0], cr[1] - 1, cr[2], cr[3], cr[4]))
.then(doJobs => {
if (doJobs) {
this.cronjobs.forEach(job => {
if (Norc.inRange(job.cronspec, cr))
job.job()
})
}
})
.catch(e => {
this.emit('error', e)
})
}
cronStop() {
if (this.cronTimeout !== null) {
clearTimeout(this.cronTimeout)
this.cronTimeout = null
}
}
}
module.exports = new Norc()