keuss 2.0.6 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.yaml +3 -0
- package/CHANGELOG.md +1 -1
- package/Pipeline/BaseLink.js +1 -1
- package/Pipeline/Queue.js +106 -90
- package/Queue.js +13 -2
- package/README.md +4 -4
- package/backends/bucket-mongo-safe.js +164 -144
- package/backends/intraorder.js +92 -82
- package/backends/mongo.js +137 -95
- package/backends/pl-mongo.js +18 -30
- package/backends/ps-mongo.js +121 -110
- package/backends/redis-oq.js +0 -2
- package/backends/stream-mongo.js +81 -63
- package/lib/mubsub/channel.js +313 -0
- package/lib/mubsub/connection.js +79 -0
- package/lib/mubsub/index.js +10 -0
- package/package.json +8 -10
- package/signal/mongo-capped.js +1 -1
- package/stats/mongo.js +91 -54
package/backends/ps-mongo.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const async = require ('async');
|
|
2
|
+
const _ = require ('lodash');
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const MongoClient = require ('mongodb').MongoClient;
|
|
5
|
+
const mongo = require ('mongodb');
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const Queue = require ('../Queue');
|
|
8
|
+
const QFactory_MongoDB_defaults = require ('../QFactory-MongoDB-defaults');
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
//////////////////////////////////////////////
|
|
11
12
|
class PersistentMongoQueue extends Queue {
|
|
12
|
-
|
|
13
13
|
//////////////////////////////////////////////
|
|
14
14
|
constructor (name, factory, opts, orig_opts) {
|
|
15
15
|
super (name, factory, opts, orig_opts);
|
|
@@ -32,54 +32,55 @@ class PersistentMongoQueue extends Queue {
|
|
|
32
32
|
|
|
33
33
|
/////////////////////////////////////////
|
|
34
34
|
// add element to queue
|
|
35
|
-
insert (entry,
|
|
36
|
-
this._col.insertOne (entry, {}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
});
|
|
35
|
+
insert (entry, cb) {
|
|
36
|
+
this._col.insertOne (entry, {})
|
|
37
|
+
.then (res => cb (null, res.insertedId))
|
|
38
|
+
.catch (cb);
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
|
|
43
42
|
/////////////////////////////////////////
|
|
44
43
|
// get element from queue
|
|
45
|
-
get (
|
|
46
|
-
|
|
44
|
+
get (cb) {
|
|
45
|
+
const q = {
|
|
47
46
|
mature: {$lte: Queue.nowPlusSecs (0)},
|
|
48
47
|
processed: {$exists: false}
|
|
49
48
|
};
|
|
50
49
|
|
|
51
|
-
|
|
50
|
+
const updt = {
|
|
52
51
|
$set: {
|
|
53
52
|
processed: new Date (),
|
|
54
53
|
mature: Queue.nowPlusSecs (100 * this._opts.ttl)
|
|
55
54
|
}
|
|
56
55
|
};
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
sort: {mature : 1}
|
|
57
|
+
const opts = {
|
|
58
|
+
sort: {mature : 1},
|
|
59
|
+
includeResultMetadata: true
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
this._col.findOneAndUpdate (q, updt, opts
|
|
63
|
-
|
|
64
|
-
const v =
|
|
65
|
-
if (!v) return
|
|
62
|
+
this._col.findOneAndUpdate (q, updt, opts)
|
|
63
|
+
.then (res => {
|
|
64
|
+
const v = res && res.value;
|
|
65
|
+
if (!v) return cb ();
|
|
66
66
|
if (v.payload._bsontype == 'Binary') v.payload = v.payload.buffer;
|
|
67
|
-
|
|
68
|
-
})
|
|
67
|
+
cb (null, v);
|
|
68
|
+
})
|
|
69
|
+
.catch (cb)
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
|
|
72
73
|
//////////////////////////////////
|
|
73
74
|
// reserve element: call cb (err, pl) where pl has an id
|
|
74
|
-
reserve (
|
|
75
|
-
|
|
75
|
+
reserve (cb) {
|
|
76
|
+
const delay = this._opts.reserve_delay || 120;
|
|
76
77
|
|
|
77
|
-
|
|
78
|
+
const query = {
|
|
78
79
|
processed: {$exists: false},
|
|
79
80
|
mature: {$lte: Queue.nowPlusSecs (0)}
|
|
80
81
|
};
|
|
81
82
|
|
|
82
|
-
|
|
83
|
+
const update = {
|
|
83
84
|
$set: {
|
|
84
85
|
mature: Queue.nowPlusSecs (delay),
|
|
85
86
|
reserved: new Date ()
|
|
@@ -87,37 +88,38 @@ class PersistentMongoQueue extends Queue {
|
|
|
87
88
|
$inc: {tries: 1}
|
|
88
89
|
};
|
|
89
90
|
|
|
90
|
-
|
|
91
|
+
const opts = {
|
|
91
92
|
sort: {mature : 1},
|
|
92
|
-
returnDocument: 'before'
|
|
93
|
+
returnDocument: 'before',
|
|
94
|
+
includeResultMetadata: true
|
|
93
95
|
};
|
|
94
96
|
|
|
95
|
-
this._col.findOneAndUpdate (query, update, opts
|
|
96
|
-
|
|
97
|
-
const v =
|
|
98
|
-
if (!v) return
|
|
97
|
+
this._col.findOneAndUpdate (query, update, opts)
|
|
98
|
+
.then (res => {
|
|
99
|
+
const v = res && res.value;
|
|
100
|
+
if (!v) return cb ();
|
|
99
101
|
if (v.payload._bsontype == 'Binary') v.payload = v.payload.buffer;
|
|
100
|
-
|
|
101
|
-
})
|
|
102
|
+
cb (null, v);
|
|
103
|
+
})
|
|
104
|
+
.catch (cb);
|
|
102
105
|
}
|
|
103
106
|
|
|
104
107
|
|
|
105
108
|
//////////////////////////////////
|
|
106
109
|
// commit previous reserve, by p.id
|
|
107
|
-
commit (id,
|
|
108
|
-
|
|
110
|
+
commit (id, cb) {
|
|
111
|
+
const query = {
|
|
112
|
+
reserved: {$exists: true}
|
|
113
|
+
};
|
|
109
114
|
|
|
110
115
|
try {
|
|
111
|
-
query =
|
|
112
|
-
_id: (_.isString(id) ? new mongo.ObjectID (id) : id),
|
|
113
|
-
reserved: {$exists: true}
|
|
114
|
-
};
|
|
116
|
+
query._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
|
|
115
117
|
}
|
|
116
118
|
catch (e) {
|
|
117
|
-
return
|
|
119
|
+
return cb ('id [' + id + '] can not be used as rollback id: ' + e);
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
|
|
122
|
+
const updt = {
|
|
121
123
|
$set: {
|
|
122
124
|
processed: new Date (),
|
|
123
125
|
mature: Queue.nowPlusSecs (100 * this._opts.ttl)
|
|
@@ -125,115 +127,121 @@ class PersistentMongoQueue extends Queue {
|
|
|
125
127
|
$unset: {reserved: ''}
|
|
126
128
|
};
|
|
127
129
|
|
|
128
|
-
|
|
130
|
+
const opts = {};
|
|
129
131
|
|
|
130
|
-
this._col.updateOne (query, updt, opts
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
});
|
|
132
|
+
this._col.updateOne (query, updt, opts)
|
|
133
|
+
.then (res => cb (null, res && (res.modifiedCount == 1)))
|
|
134
|
+
.catch (cb);
|
|
134
135
|
}
|
|
135
136
|
|
|
136
137
|
|
|
137
138
|
//////////////////////////////////
|
|
138
139
|
// rollback previous reserve, by p.id
|
|
139
|
-
rollback (id, next_t,
|
|
140
|
+
rollback (id, next_t, cb) {
|
|
140
141
|
if (_.isFunction (next_t)) {
|
|
141
|
-
|
|
142
|
+
cb = next_t;
|
|
142
143
|
next_t = null;
|
|
143
144
|
}
|
|
144
145
|
|
|
146
|
+
const query = {
|
|
147
|
+
reserved: {$exists: true}
|
|
148
|
+
};
|
|
149
|
+
|
|
145
150
|
try {
|
|
146
|
-
|
|
147
|
-
_id: (_.isString(id) ? new mongo.ObjectID (id) : id),
|
|
148
|
-
reserved: {$exists: true}
|
|
149
|
-
};
|
|
151
|
+
query._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
|
|
150
152
|
}
|
|
151
153
|
catch (e) {
|
|
152
|
-
return
|
|
154
|
+
return cb ('id [' + id + '] can not be used as rollback id: ' + e);
|
|
153
155
|
}
|
|
154
156
|
|
|
155
|
-
|
|
157
|
+
const update = {
|
|
156
158
|
$set: {mature: (next_t ? new Date (next_t) : Queue.now ())},
|
|
157
159
|
$unset: {reserved: ''}
|
|
158
160
|
};
|
|
159
161
|
|
|
160
|
-
this._col.updateOne (query, update, {}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
});
|
|
162
|
+
this._col.updateOne (query, update, {})
|
|
163
|
+
.then (res => cb (null, res && (res.modifiedCount == 1)))
|
|
164
|
+
.catch (cb);
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
|
|
167
168
|
//////////////////////////////////
|
|
168
169
|
// queue size including non-mature elements
|
|
169
|
-
totalSize (
|
|
170
|
-
|
|
170
|
+
totalSize (cb) {
|
|
171
|
+
const q = {
|
|
171
172
|
processed: {$exists: false}
|
|
172
173
|
};
|
|
173
174
|
|
|
174
|
-
|
|
175
|
-
this._col.countDocuments (q, opts
|
|
175
|
+
const opts = {};
|
|
176
|
+
this._col.countDocuments (q, opts)
|
|
177
|
+
.then (res => cb (null, res))
|
|
178
|
+
.catch (cb);
|
|
176
179
|
}
|
|
177
180
|
|
|
178
181
|
|
|
179
182
|
//////////////////////////////////
|
|
180
183
|
// queue size NOT including non-mature elements
|
|
181
|
-
size (
|
|
182
|
-
|
|
184
|
+
size (cb) {
|
|
185
|
+
const q = {
|
|
183
186
|
processed: {$exists: false},
|
|
184
187
|
mature : {$lte : Queue.now ()}
|
|
185
188
|
};
|
|
186
189
|
|
|
187
|
-
|
|
188
|
-
this._col.countDocuments (q, opts
|
|
190
|
+
const opts = {};
|
|
191
|
+
this._col.countDocuments (q, opts)
|
|
192
|
+
.then (res => cb (null, res))
|
|
193
|
+
.catch (cb);
|
|
189
194
|
}
|
|
190
195
|
|
|
191
196
|
|
|
192
197
|
//////////////////////////////////
|
|
193
198
|
// queue size of non-mature elements only
|
|
194
|
-
schedSize (
|
|
195
|
-
|
|
199
|
+
schedSize (cb) {
|
|
200
|
+
const q = {
|
|
196
201
|
mature : {$gt : Queue.now ()},
|
|
197
202
|
processed: {$exists: false},
|
|
198
203
|
reserved: {$exists: false}
|
|
199
204
|
};
|
|
200
205
|
|
|
201
|
-
|
|
202
|
-
this._col.countDocuments (q, opts
|
|
206
|
+
const opts = {};
|
|
207
|
+
this._col.countDocuments (q, opts)
|
|
208
|
+
.then (res => cb (null, res))
|
|
209
|
+
.catch (cb);
|
|
203
210
|
}
|
|
204
211
|
|
|
205
212
|
|
|
206
213
|
//////////////////////////////////
|
|
207
214
|
// queue size of reserved elements only
|
|
208
|
-
resvSize (
|
|
209
|
-
|
|
215
|
+
resvSize (cb) {
|
|
216
|
+
const q = {
|
|
210
217
|
mature : {$gt : Queue.now ()},
|
|
211
218
|
processed: {$exists: false},
|
|
212
219
|
reserved: {$exists: true}
|
|
213
220
|
};
|
|
214
221
|
|
|
215
|
-
|
|
216
|
-
this._col.countDocuments (q, opts
|
|
222
|
+
const opts = {};
|
|
223
|
+
this._col.countDocuments (q, opts)
|
|
224
|
+
.then (res => cb (null, res))
|
|
225
|
+
.catch (cb);
|
|
217
226
|
}
|
|
218
227
|
|
|
219
228
|
|
|
220
229
|
//////////////////////////////////////////////
|
|
221
230
|
// remove by id
|
|
222
|
-
remove (id,
|
|
223
|
-
|
|
231
|
+
remove (id, cb) {
|
|
232
|
+
const query ={
|
|
233
|
+
processed: {$exists: false},
|
|
234
|
+
reserved: {$exists: false}
|
|
235
|
+
};
|
|
224
236
|
|
|
225
237
|
try {
|
|
226
|
-
query =
|
|
227
|
-
_id: (_.isString(id) ? new mongo.ObjectID (id) : id),
|
|
228
|
-
processed: {$exists: false},
|
|
229
|
-
reserved: {$exists: false}
|
|
230
|
-
};
|
|
238
|
+
query._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
|
|
231
239
|
}
|
|
232
240
|
catch (e) {
|
|
233
|
-
return
|
|
241
|
+
return cb ('id [' + id + '] can not be used as remove id: ' + e);
|
|
234
242
|
}
|
|
235
243
|
|
|
236
|
-
|
|
244
|
+
const updt = {
|
|
237
245
|
$set: {
|
|
238
246
|
processed: new Date (),
|
|
239
247
|
mature: Queue.nowPlusSecs (100 * this._opts.ttl),
|
|
@@ -241,27 +249,25 @@ class PersistentMongoQueue extends Queue {
|
|
|
241
249
|
},
|
|
242
250
|
};
|
|
243
251
|
|
|
244
|
-
|
|
252
|
+
const opts = {};
|
|
245
253
|
|
|
246
|
-
this._col.updateOne (query, updt, opts
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
});
|
|
254
|
+
this._col.updateOne (query, updt, opts)
|
|
255
|
+
.then (res => cb (null, res && (res.modifiedCount == 1)))
|
|
256
|
+
.catch (cb);
|
|
250
257
|
}
|
|
251
258
|
|
|
252
259
|
|
|
253
260
|
/////////////////////////////////////////
|
|
254
261
|
// get element from queue
|
|
255
|
-
next_t (
|
|
262
|
+
next_t (cb) {
|
|
256
263
|
this._col
|
|
257
264
|
.find ({processed: {$exists: false}})
|
|
258
265
|
.limit(1)
|
|
259
266
|
.sort ({mature:1})
|
|
260
267
|
.project ({mature:1})
|
|
261
|
-
.next (
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
});
|
|
268
|
+
.next ()
|
|
269
|
+
.then (res => cb (null, res && res.mature))
|
|
270
|
+
.catch (cb);
|
|
265
271
|
}
|
|
266
272
|
|
|
267
273
|
|
|
@@ -271,14 +277,17 @@ class PersistentMongoQueue extends Queue {
|
|
|
271
277
|
//////////////////////////////////////////////////////////////////
|
|
272
278
|
// create needed indexes for O(1) functioning
|
|
273
279
|
_ensureIndexes (cb) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
this._col.createIndex({processed: 1}, {expireAfterSeconds: this._opts.ttl}
|
|
277
|
-
|
|
280
|
+
async.series ([
|
|
281
|
+
cb => this._col.createIndex ({mature : 1}).then (res => cb(null, res)).catch (cb),
|
|
282
|
+
cb => this._col.createIndex ({processed: 1}, {expireAfterSeconds: this._opts.ttl}).then (res => cb(null, res)).catch (cb),
|
|
283
|
+
])
|
|
284
|
+
.then (() => cb (null, this))
|
|
285
|
+
.catch (cb);
|
|
278
286
|
}
|
|
279
287
|
}
|
|
280
288
|
|
|
281
289
|
|
|
290
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
282
291
|
class Factory extends QFactory_MongoDB_defaults {
|
|
283
292
|
constructor (opts, mongo_conn) {
|
|
284
293
|
super (opts);
|
|
@@ -286,6 +295,7 @@ class Factory extends QFactory_MongoDB_defaults {
|
|
|
286
295
|
this._db = mongo_conn.db();
|
|
287
296
|
}
|
|
288
297
|
|
|
298
|
+
//////////////////////////////////////////////////////////////////
|
|
289
299
|
queue (name, opts, cb) {
|
|
290
300
|
if (!cb) {
|
|
291
301
|
cb = opts;
|
|
@@ -298,6 +308,7 @@ class Factory extends QFactory_MongoDB_defaults {
|
|
|
298
308
|
q._ensureIndexes (cb);
|
|
299
309
|
}
|
|
300
310
|
|
|
311
|
+
//////////////////////////////////////////////////////////////////
|
|
301
312
|
close (cb) {
|
|
302
313
|
super.close (() => {
|
|
303
314
|
if (this._mongo_conn) {
|
|
@@ -309,10 +320,12 @@ class Factory extends QFactory_MongoDB_defaults {
|
|
|
309
320
|
});
|
|
310
321
|
}
|
|
311
322
|
|
|
323
|
+
//////////////////////////////////////////////////////////////////
|
|
312
324
|
type () {
|
|
313
325
|
return PersistentMongoQueue.Type ();
|
|
314
326
|
}
|
|
315
327
|
|
|
328
|
+
//////////////////////////////////////////////////////////////////
|
|
316
329
|
capabilities () {
|
|
317
330
|
return {
|
|
318
331
|
sched: true,
|
|
@@ -324,20 +337,18 @@ class Factory extends QFactory_MongoDB_defaults {
|
|
|
324
337
|
}
|
|
325
338
|
}
|
|
326
339
|
|
|
340
|
+
|
|
341
|
+
//////////////////////////////////////////////////////////////////
|
|
327
342
|
function creator (opts, cb) {
|
|
328
|
-
|
|
329
|
-
|
|
343
|
+
const _opts = opts || {};
|
|
344
|
+
const m_url = _opts.url || 'mongodb://localhost:27017/keuss';
|
|
330
345
|
|
|
331
|
-
MongoClient.connect (m_url
|
|
332
|
-
|
|
333
|
-
|
|
346
|
+
MongoClient.connect (m_url)
|
|
347
|
+
.then (cl => {
|
|
348
|
+
const F = new Factory (_opts, cl);
|
|
334
349
|
F.async_init (err => cb (null, F));
|
|
335
|
-
})
|
|
350
|
+
})
|
|
351
|
+
.catch (cb);
|
|
336
352
|
}
|
|
337
353
|
|
|
338
354
|
module.exports = creator;
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|