keuss 2.0.7 → 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.
@@ -1,15 +1,15 @@
1
- var async = require ('async');
2
- var _ = require ('lodash');
1
+ const async = require ('async');
2
+ const _ = require ('lodash');
3
3
 
4
- var MongoClient = require ('mongodb').MongoClient;
5
- var mongo = require ('mongodb');
4
+ const MongoClient = require ('mongodb').MongoClient;
5
+ const mongo = require ('mongodb');
6
6
 
7
- var Queue = require ('../Queue');
8
- var QFactory_MongoDB_defaults = require ('../QFactory-MongoDB-defaults');
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, callback) {
36
- this._col.insertOne (entry, {}, (err, result) => {
37
- if (err) return callback (err);
38
- callback (null, result.insertedId);
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 (callback) {
46
- var q = {
44
+ get (cb) {
45
+ const q = {
47
46
  mature: {$lte: Queue.nowPlusSecs (0)},
48
47
  processed: {$exists: false}
49
48
  };
50
49
 
51
- var updt = {
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
- var opts = {
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, (err, result) => {
63
- if (err) return callback (err);
64
- const v = result && result.value;
65
- if (!v) return callback ();
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
- callback (null, v);
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 (callback) {
75
- var delay = this._opts.reserve_delay || 120;
75
+ reserve (cb) {
76
+ const delay = this._opts.reserve_delay || 120;
76
77
 
77
- var query = {
78
+ const query = {
78
79
  processed: {$exists: false},
79
80
  mature: {$lte: Queue.nowPlusSecs (0)}
80
81
  };
81
82
 
82
- var update = {
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
- var opts = {
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, (err, result) => {
96
- if (err) return callback (err);
97
- const v = result && result.value;
98
- if (!v) return callback ();
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
- callback (null, v);
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, callback) {
108
- var query;
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 callback ('id [' + id + '] can not be used as rollback id: ' + e);
119
+ return cb ('id [' + id + '] can not be used as rollback id: ' + e);
118
120
  }
119
121
 
120
- var updt = {
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
- var opts = {};
130
+ const opts = {};
129
131
 
130
- this._col.updateOne (query, updt, opts, (err, result) => {
131
- if (err) return callback (err);
132
- callback (null, result && (result.modifiedCount == 1));
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, callback) {
140
+ rollback (id, next_t, cb) {
140
141
  if (_.isFunction (next_t)) {
141
- callback = next_t;
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
- var query = {
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 callback ('id [' + id + '] can not be used as rollback id: ' + e);
154
+ return cb ('id [' + id + '] can not be used as rollback id: ' + e);
153
155
  }
154
156
 
155
- var update = {
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, {}, (err, result) => {
161
- if (err) return callback (err);
162
- callback (null, result && (result.modifiedCount == 1));
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 (callback) {
170
- var q = {
170
+ totalSize (cb) {
171
+ const q = {
171
172
  processed: {$exists: false}
172
173
  };
173
174
 
174
- var opts = {};
175
- this._col.countDocuments (q, opts, callback);
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 (callback) {
182
- var q = {
184
+ size (cb) {
185
+ const q = {
183
186
  processed: {$exists: false},
184
187
  mature : {$lte : Queue.now ()}
185
188
  };
186
189
 
187
- var opts = {};
188
- this._col.countDocuments (q, opts, callback);
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 (callback) {
195
- var q = {
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
- var opts = {};
202
- this._col.countDocuments (q, opts, callback);
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 (callback) {
209
- var q = {
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
- var opts = {};
216
- this._col.countDocuments (q, opts, callback);
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, callback) {
223
- var query;
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 callback ('id [' + id + '] can not be used as remove id: ' + e);
241
+ return cb ('id [' + id + '] can not be used as remove id: ' + e);
234
242
  }
235
243
 
236
- var updt = {
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
- var opts = {};
252
+ const opts = {};
245
253
 
246
- this._col.updateOne (query, updt, opts, (err, result) => {
247
- if (err) return callback (err);
248
- callback (null, result && (result.modifiedCount == 1));
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 (callback) {
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 ((err, result) => {
262
- if (err) return callback (err);
263
- callback (null, result && result.mature);
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
- this._col.createIndex ({mature : 1}, err => {
275
- if (err) return cb (err);
276
- this._col.createIndex({processed: 1}, {expireAfterSeconds: this._opts.ttl}, err => cb (err, this));
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
- var _opts = opts || {};
329
- var m_url = _opts.url || 'mongodb://localhost:27017/keuss';
343
+ const _opts = opts || {};
344
+ const m_url = _opts.url || 'mongodb://localhost:27017/keuss';
330
345
 
331
- MongoClient.connect (m_url, { useNewUrlParser: true }, (err, cl) => {
332
- if (err) return cb (err);
333
- var F = new Factory (_opts, cl);
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
-
@@ -171,5 +171,3 @@ function creator (opts, cb) {
171
171
  }
172
172
 
173
173
  module.exports = creator;
174
-
175
-