-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
298 lines (252 loc) · 8.39 KB
/
index.js
File metadata and controls
298 lines (252 loc) · 8.39 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict'
// **Github:** https://github.com/teambition/timed-queue
//
// **License:** MIT
const fs = require('fs')
const path = require('path')
const thunks = require('thunks')
const redis = require('thunk-redis')
const EventEmitter = require('events').EventEmitter
const thunk = thunks()
const slice = Array.prototype.slice
const luaScript = fs.readFileSync(path.join(__dirname, 'queue.lua'), { encoding: 'utf8' })
class TimedQueue extends EventEmitter {
constructor (options) {
super()
options = options || {}
this.prefix = options.prefix || 'TIMEDQ'
this.queuesKey = '{' + this.prefix + '}:QUEUES'
this.count = Math.floor(options.count) || 64
this.interval = Math.floor(options.interval) || 1000 * 60
this.expire = Math.floor(options.expire) || this.interval * 5
this.retry = Math.floor(options.retry) || Math.floor(this.interval / 2)
this.accuracy = Math.floor(options.accuracy) || Math.floor(this.interval / 5)
this.autoScan = options.autoScan !== false
this.redis = null
this.timer = null
this.scanTime = 0
this.scanning = false
this.delay = this.interval
this.queues = Object.create(null)
this.thunk = thunks((err) => err && this.emit('error', err))
}
connect (redisClient) {
if (this.redis) return this
if (redisClient && redisClient.info && typeof redisClient.evalauto === 'function') {
this.redis = redisClient
} else {
this.redis = redis.createClient.apply(null, arguments)
}
this.redis.on('connect', () => this.emit('connect'))
.on('error', (err) => this.emit('error', err))
.on('close', () => this.emit('close'))
// auto scan jobs
if (this.autoScan) {
thunk.delay(Math.random() * this.interval)(() => this.scan())
}
return this
}
queue (queueName, options) {
validateString(queueName)
if (!this.queues[queueName]) this.queues[queueName] = new Queue(this, queueName, options)
else if (options) this.queues[queueName].init(options)
return this.queues[queueName]
}
destroyQueue (queueName) {
return thunk.call(this, function * () {
let redis = this.redis
let queue = this.queue(queueName)
delete this.queues[queueName]
yield [
redis.srem(this.queuesKey, queue.name),
redis.del(queue.queueOptionsKey),
redis.del(queue.activeQueueKey),
redis.del(queue.queueKey)
]
})
}
scan () {
if (this.scanning || !this.redis) return this
let scanStart
this.scanning = true
this.scanTime = scanStart = Date.now()
this.thunk(function * () {
let queues = yield this.redis.smembers(this.queuesKey)
queues = queues.filter((queueName) => this.queues[queueName])
this.emit('scanStart', queues.length)
let queueScores = yield queues.map((queue) => this.queue(queue).scan())
this.emit('scanEnd', queueScores.length, Date.now() - scanStart)
this.regulateFreq(scoresDeviation(queueScores))
if (!this.timer && this.scanning) {
this.scanning = false
this.scan()
} else this.scanning = false
})()
clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.timer = null
if (!this.scanning) this.scan()
}, this.delay)
return this
}
stop () {
clearTimeout(this.timer)
this.scanning = false
this.timer = null
return this
}
close () {
this.stop()
this.redis.clientEnd()
this.redis = null
return this
}
// x > 0 | 2 - 1 / (1 + x)
// x < 0 | 1 / (1 - x)
regulateFreq (factor) {
if (factor < -0.05) this.delay = Math.max(this.delay / (1 - factor), this.interval / 10)
else if (factor > 0.05) this.delay = Math.min(this.delay * (2 - 1 / (1 + factor)), this.interval)
return this
}
}
class Queue extends EventEmitter {
constructor (timedQueue, queueName, options) {
super()
this.root = timedQueue
this.name = queueName
this.queueKey = '{' + timedQueue.prefix + ':' + queueName + '}' // hash tag
this.activeQueueKey = this.queueKey + ':-'
this.queueOptionsKey = this.queueKey + ':O'
this.init(options)
}
init (options) {
let root = this.root
root.thunk(root.redis.sadd(root.queuesKey, this.name))()
if (!options) return this
root.thunk(root.redis.hmset(this.queueOptionsKey, {
count: options.count || root.count,
retry: options.retry || root.retry,
expire: options.expire || root.expire,
accuracy: options.accuracy || root.accuracy
}))()
return this
}
addjob (job, timing) {
let args = slice.call(Array.isArray(job) ? job : arguments)
return thunk.call(this, function * () {
let data = [this.queueKey]
let current = Date.now()
for (let i = 0, l = args.length || 2; i < l; i += 2) {
validateString(args[i])
timing = Math.floor(args[i + 1])
if (!timing || timing <= current) throw new Error(`${String(args[i + 1])} is invalid time in "${job}".`)
data.push(timing, args[i])
}
return yield this.root.redis.zadd(data)
})
}
show (job) {
return thunk.call(this, function * () {
validateString(job)
let timing = yield this.root.redis.zscore(this.queueKey, job)
if (timing) return new Job(this.name, job, timing, 0, 0)
let times = yield this.root.redis.hget(this.activeQueueKey, job)
if (!times) return null
times = times.split(':')
return new Job(this.name, job, times[0], times[1], times.length - 2)
})
}
deljob (job) {
let args = slice.call(Array.isArray(job) ? job : arguments)
return thunk.call(this, function * () {
for (let i = 0, l = args.length || 1; i < l; i++) validateString(args[i])
args.unshift(this.queueKey)
let count = yield this.root.redis.zrem(args)
args[0] = this.activeQueueKey
let _count = yield this.root.redis.hdel(args)
return count + _count
})
}
getjobs (scanActive) {
return thunk.call(this, function * () {
let root = this.root
let timestamp = Date.now()
let res = yield root.redis.evalauto(luaScript, 3,
this.queueKey, this.activeQueueKey, this.queueOptionsKey,
root.count, root.retry, root.expire, root.accuracy, timestamp, +(scanActive !== false))
return new ScanResult(this.name, timestamp, res)
})
}
ackjob (job) {
let args = slice.call(Array.isArray(job) ? job : arguments)
return thunk.call(this, function * () {
for (let i = 0, l = args.length || 1; i < l; i++) validateString(args[i])
args.unshift(this.activeQueueKey)
return yield this.root.redis.hdel(args)
})
}
scan () {
return thunk.call(this, function * () {
let scores = []
if (!this.listenerCount('job')) {
return // Don't scan if no 'job' listener
}
let res = yield this.getjobs(true) // get active jobs firstly
while (true) {
res.jobs.map((job) => {
if (!job.retryCount) scores.push((job.timing - job.active) / res.retry)
process.nextTick(() => this.emit('job', job))
})
if (!res.hasMore) return scores
res = yield this.getjobs(false)
}
})
}
len () {
return thunk.call(this, function * () {
return yield this.root.redis.zcard(this.queueKey)
})
}
showActive () {
return thunk.call(this, function * () {
let res = yield this.root.redis.hgetall(this.activeQueueKey)
return Object.keys(res)
.map((job) => {
let times = res[job].split(':')
return new Job(this.name, job, times[0], times[1], times.length - 2)
}).sort((a, b) => a.timing - b.timing)
})
}
}
class Job {
constructor (queue, job, timing, active, retryCount) {
this.queue = queue
this.job = job
this.timing = +timing
this.active = +active
this.retryCount = +retryCount
}
}
function ScanResult (name, timestamp, res) {
let jobs = res[0]
this.retry = +res[1]
this.hasMore = +res[2]
this.jobs = []
for (let i = 0, l = jobs.length - 2; i < l; i += 3) {
this.jobs.push(new Job(name, jobs[i], jobs[i + 1], timestamp, jobs[i + 2]))
}
}
function scoresDeviation (queueScores) {
let count = 0
let score = queueScores.reduce((prev, scores) => {
return prev + scores.reduce((p, s) => {
count++
return p + s
}, 0)
}, 0)
return count ? (score / count) : 0
}
function validateString (str) {
if (typeof str !== 'string' || !str) throw new TypeError(`${String(str)} is invalid string`)
}
module.exports = TimedQueue