-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
150 lines (123 loc) · 3.66 KB
/
index.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
/**
LevelUp Queue.
**/
var timestamp = require('monotonic-timestamp')
var hooks = require('level-hooks')
var hash = require('sha1sum')
module.exports = function (prefix, work) {
return function (db, delay) {
if(db.queue) {
for(var job in work) {
db.queue.add(job, work[job])
}
return
}
var jobs = [], pending = {}, batch = []
if('string' !== typeof prefix)
work = prefix, prefix = '~queue'
if(!work) work = {}
/**
on start up, read any unfinished jobs from database
(incase there was a crash)
cleanup any duplicates,
(possible if a delete fails and that job is requeued concurrently)
then start the jobs.
make i into a timestamp! a monotonic-timestamp
**/
var inProgress = 0
var count = 0
function emit(name, a, b) {
if(!a)
return db.emit('queue:' + name)
db.emit('queue:'+name, a, b)
db.emit('queue:'+name+':'+a, b)
}
function onJob (data, recover) {
//KEY should be VALUE
var value = ''+data.value
var ary = (''+data.key).split('~')
var t = ary.pop()
var job = ary.pop()
if(recover) emit('recover', job, value)
start(job, t, value)
}
//read any jobs left from last run.
db.readStream({start: prefix , end: prefix+'~~'})
.on('data', function (data) {
count=true; onJob(data, true)
})
.on('end', function () {
//emit drain if there was no data.
if(!count && !inProgress)
db.emit('queue:drain')
})
//listen for new jobs.
hooks()(db)
db.hooks.post(function (change) {
if(change.type == 'put' && /^~queue/.test(''+change.key)) {
onJob(change)
}
})
function toKey(job, ts) {
return [prefix, job, (ts || timestamp()).toString()].join('~')
}
function start (job, ts, value) {
if('function' !== typeof work[job]) {
//log this error, but don't need to fail,
//run this job next time.
return (
console.error || console.log
)('level-queue has no work function for job:'+job)
}
inProgress ++
setTimeout(function () {
delete pending[hash(job+':'+value)]
var n = 1
function done () {
if(--n) return
db.del(toKey(job, ts), function () {
inProgress --
try {
db.emit('queue:done', job, value)
db.emit('queue:done:'+job, value)
} finally {
if(!inProgress)
db.emit('queue:drain')
}
})
}
db.emit('queue:start', job, value, done)
//you should probably just use this pattern...
db.emit('queue:start:'+job, value, done)
work[job](value, done)
}, db.queue.delay)
}
//put=false means return a job to be queued
function queue (job, value, put) {
var ts = timestamp()
var key = toKey(job, ts)
var id = hash(job+':'+value)
if(pending[id]) return null //this job is already queued.
pending[id] = Date.now()
if(put === false) {
//return the job to be queued, to include it in a batch insert.
return {
type: 'put',
key: Buffer.isBuffer(key) ? key : new Buffer(key),
value: Buffer.isBuffer(key) ? value : new Buffer(value)
}
} else {
db.put(key, value)
}
}
//you should only add jobs in the first tick.
queue.add = function (job, worker) {
work[job] = worker
return queue
}
db.queue = queue
db.queue.delay = delay || 1000
for(var job in work)
db.queue.add(job, work[job])
}
}