-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
manager.js
348 lines (271 loc) · 9.53 KB
/
manager.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
const assert = require('assert')
const EventEmitter = require('events')
const pMap = require('p-map')
const uuid = require('uuid')
const Worker = require('./worker')
const plans = require('./plans')
const Attorney = require('./attorney')
const completedJobPrefix = plans.completedJobPrefix
const events = {
error: 'error'
}
class Manager extends EventEmitter {
constructor (db, config) {
super()
this.config = config
this.db = db
this.events = events
this.subscriptions = {}
this.nextJobCommand = plans.fetchNextJob(config.schema)
this.insertJobCommand = plans.insertJob(config.schema)
this.completeJobsCommand = plans.completeJobs(config.schema)
this.cancelJobsCommand = plans.cancelJobs(config.schema)
this.failJobsCommand = plans.failJobs(config.schema)
// exported api to index
this.functions = [
this.fetch,
this.complete,
this.cancel,
this.fail,
this.publish,
this.subscribe,
this.unsubscribe,
this.onComplete,
this.offComplete,
this.fetchCompleted,
this.publishDebounced,
this.publishThrottled,
this.publishOnce,
this.publishAfter,
this.deleteQueue,
this.deleteAllQueues,
this.clearStorage,
this.getQueueSize
]
}
async stop () {
Object.keys(this.subscriptions).forEach(name => this.unsubscribe(name))
this.subscriptions = {}
}
async subscribe (name, ...args) {
const { options, callback } = Attorney.checkSubscribeArgs(name, args, this.config)
return this.watch(name, options, callback)
}
async onComplete (name, ...args) {
const { options, callback } = Attorney.checkSubscribeArgs(name, args, this.config)
return this.watch(completedJobPrefix + name, options, callback)
}
async watch (name, options, callback) {
options.newJobCheckInterval = options.newJobCheckInterval || this.config.newJobCheckInterval
const sendItBruh = async (jobs) => {
if (!jobs) {
return
}
// If you get a batch, for now you should use complete() so you can control
// whether individual or group completion responses apply to your use case
// Failing will fail all fetched jobs
if (options.batchSize) {
return Promise.all([callback(jobs)]).catch(err => this.fail(jobs.map(job => job.id), err))
}
const concurrency = options.teamConcurrency || 1
// either no option was set, or teamSize was used
return pMap(jobs, job =>
callback(job)
.then(value => this.complete(job.id, value))
.catch(err => this.fail(job.id, err))
, { concurrency }
).catch(() => {}) // allow promises & non-promises to live together in harmony
}
const fetchOptions = { includeMetadata: options.includeMetadata || false }
const onError = error => this.emit(events.error, error)
const workerConfig = {
name,
fetch: () => this.fetch(name, options.batchSize || options.teamSize || 1, fetchOptions),
onFetch: jobs => sendItBruh(jobs),
onError,
interval: options.newJobCheckInterval
}
const worker = new Worker(workerConfig)
worker.start()
if (!this.subscriptions[name]) { this.subscriptions[name] = { workers: [] } }
this.subscriptions[name].workers.push(worker)
}
async unsubscribe (name) {
assert(this.subscriptions[name], `No subscriptions for ${name} were found.`)
this.subscriptions[name].workers.forEach(worker => worker.stop())
delete this.subscriptions[name]
}
async offComplete (name) {
return this.unsubscribe(completedJobPrefix + name)
}
async publish (...args) {
const { name, data, options } = Attorney.checkPublishArgs(args, this.config)
return this.createJob(name, data, options)
}
async publishOnce (name, data, options, key) {
options = options || {}
options.singletonKey = key || name
const result = Attorney.checkPublishArgs([name, data, options], this.config)
return this.createJob(result.name, result.data, result.options)
}
async publishAfter (name, data, options, after) {
options = options || {}
options.startAfter = after
const result = Attorney.checkPublishArgs([name, data, options], this.config)
return this.createJob(result.name, result.data, result.options)
}
async publishThrottled (name, data, options, seconds, key) {
options = options || {}
options.singletonSeconds = seconds
options.singletonNextSlot = false
options.singletonKey = key
const result = Attorney.checkPublishArgs([name, data, options], this.config)
return this.createJob(result.name, result.data, result.options)
}
async publishDebounced (name, data, options, seconds, key) {
options = options || {}
options.singletonSeconds = seconds
options.singletonNextSlot = true
options.singletonKey = key
const result = Attorney.checkPublishArgs([name, data, options], this.config)
return this.createJob(result.name, result.data, result.options)
}
async createJob (name, data, options, singletonOffset = 0) {
const {
expireIn,
priority,
startAfter,
keepUntil,
singletonKey = null,
singletonSeconds,
retryBackoff,
retryLimit,
retryDelay,
onComplete
} = options
const id = uuid[this.config.uuid]()
const values = [
id, // 1
name, // 2
priority, // 3
retryLimit, // 4
startAfter, // 5
expireIn, // 6
data, // 7
singletonKey, // 8
singletonSeconds, // 9
singletonOffset, // 10
retryDelay, // 11
retryBackoff, // 12
keepUntil, // 13
onComplete // 14
]
const result = await this.db.executeSql(this.insertJobCommand, values)
if (result.rowCount === 1) {
return result.rows[0].id
}
if (!options.singletonNextSlot) {
return null
}
// delay starting by the offset to honor throttling config
options.startAfter = this.getDebounceStartAfter(singletonSeconds, this.timekeeper.clockSkew)
// toggle off next slot config for round 2
options.singletonNextSlot = false
singletonOffset = singletonSeconds
return this.createJob(name, data, options, singletonOffset)
}
getDebounceStartAfter (singletonSeconds, clockOffset) {
const debounceInterval = singletonSeconds * 1000
const now = Date.now() + clockOffset
const slot = Math.floor(now / debounceInterval) * debounceInterval
// prevent startAfter=0 during debouncing
let startAfter = (singletonSeconds - Math.floor((now - slot) / 1000)) || 1
if (singletonSeconds > 1) {
startAfter++
}
return startAfter
}
async fetch (name, batchSize, options = {}) {
const values = Attorney.checkFetchArgs(name, batchSize, options)
const result = await this.db.executeSql(
this.nextJobCommand(options.includeMetadata || false),
[values.name, batchSize || 1]
)
const jobs = result.rows.map(job => {
job.done = async (error, response) => {
if (error) {
await this.fail(job.id, error)
} else {
await this.complete(job.id, response)
}
}
return job
})
return jobs.length === 0
? null
: jobs.length === 1 && !batchSize
? jobs[0]
: jobs
}
async fetchCompleted (name, batchSize, options = {}) {
return this.fetch(completedJobPrefix + name, batchSize, options)
}
mapCompletionIdArg (id, funcName) {
const errorMessage = `${funcName}() requires an id`
assert(id, errorMessage)
const ids = Array.isArray(id) ? id : [id]
assert(ids.length, errorMessage)
return ids
}
mapCompletionDataArg (data) {
if (data === null || typeof data === 'undefined' || typeof data === 'function') { return null }
if (data instanceof Error) { data = JSON.parse(JSON.stringify(data, Object.getOwnPropertyNames(data))) }
return (typeof data === 'object' && !Array.isArray(data))
? data
: { value: data }
}
mapCompletionResponse (ids, result) {
return {
jobs: ids,
requested: ids.length,
updated: parseInt(result.rows[0].count)
}
}
async complete (id, data) {
const ids = this.mapCompletionIdArg(id, 'complete')
const result = await this.db.executeSql(this.completeJobsCommand, [ids, this.mapCompletionDataArg(data)])
return this.mapCompletionResponse(ids, result)
}
async fail (id, data) {
const ids = this.mapCompletionIdArg(id, 'fail')
const result = await this.db.executeSql(this.failJobsCommand, [ids, this.mapCompletionDataArg(data)])
return this.mapCompletionResponse(ids, result)
}
async cancel (id) {
const ids = this.mapCompletionIdArg(id, 'cancel')
const result = await this.db.executeSql(this.cancelJobsCommand, [ids])
return this.mapCompletionResponse(ids, result)
}
async deleteQueue (queue, options) {
assert(queue, 'Missing queue name argument')
const sql = plans.deleteQueue(this.config.schema, options)
const result = await this.db.executeSql(sql, [queue])
return result.rowCount
}
async deleteAllQueues (options) {
const sql = plans.deleteAllQueues(this.config.schema, options)
const result = await this.db.executeSql(sql)
return result.rowCount
}
async clearStorage () {
const sql = plans.clearStorage(this.config.schema)
await this.db.executeSql(sql)
}
async getQueueSize (queue, options) {
assert(queue, 'Missing queue name argument')
const sql = plans.getQueueSize(this.config.schema, options)
const { rows } = await this.db.executeSql(sql, [queue])
return parseFloat(rows[0].count)
}
}
module.exports = Manager