-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchQueue.js
More file actions
95 lines (88 loc) · 2.48 KB
/
BatchQueue.js
File metadata and controls
95 lines (88 loc) · 2.48 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
const DB = require('./DB')
const uniqid = require('uniqid')
const schedule = require('node-schedule')
let connections = {}
class BatchQueue {
constructor(dbPath) {
this.db = new DB(dbPath)
}
static getSharedConnection(dbPath) {
let queue = connections[dbPath]
if (!queue) {
queue = new BatchQueue(dbPath)
connections[dbPath] = queue
}
return queue
}
/**
* add an array of items to the end of the queue
* @param {[type]} batch [description]
* @return {[type]} [description]
*/
push(batch) {
return batch.reduce((acc, entry) => {
return acc.then(() => this.db.push(
{
id: uniqid(),
data: entry
}
))
}, Promise.resolve())
.then(() => {
return "done"
})
}
/**
* Executes the given asyncJob on the items from 0-to-count, if the asyncJob was successful it
* then deletes those items from the queue and return prmoise that
* resolves to the poped items
* @param {[type]} count [description]
* @param {[type]} asyncJob [description]
* @return {[type]} [description]
*/
pop(count, asyncJob) {
return this.db.get(count)
.then(results => {
// execute the batch job on the data
return asyncJob(results.map(entry => entry.data))
.then(() => {
return results
})
})
.then(results => {
let ids = results.reduce((acc, result, index) => {
return acc += '"' + result.id + '"' + ( index < results.length - 1 ? ', ' : '')
} , '')
return this.db.delete(ids).then(() => results)
})
.then(results => {
return results
})
}
/**
* Schedule a given job (asyncJob) to run every given number of
* mintues on a batch of items defined by (forBatchCount)
* that is poped from the queue. , those items are deleted only if the asyncJob succeeds.
* onSuccess will be called after completion with the list of poped items.
* onFail will be called whenever an error occuers, the error given as arguemnt
* @param {[type]} asyncJob [description]
* @param {[type]} forBatchCount [description]
* @param {[type]} everyMins [description]
* @param {[type]} onSuccess [description]
* @param {[type]} onFail [description]
* @return {[type]} [description]
*/
schedule(asyncJob, forBatchCount, everyMins, onSuccess, onFail) {
schedule.scheduleJob(`*/${everyMins} * * * *`, () => {
this.pop(forBatchCount, asyncJob)
.then(results => {
onSuccess(results)
})
.catch(err => {
console.log(err)
onFail(err)
})
})
}
}
module.exports = BatchQueue