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/backends/mongo.js CHANGED
@@ -1,10 +1,12 @@
1
- var _ = require ('lodash');
1
+ const _ = require ('lodash');
2
2
 
3
- var MongoClient = require ('mongodb').MongoClient;
4
- var mongo = require ('mongodb');
3
+ const MongoClient = require ('mongodb').MongoClient;
4
+ const mongo = require ('mongodb');
5
+ const debug = require('debug')('keuss:Queue:mongo');
6
+
7
+ const Queue = require ('../Queue');
8
+ const QFactory_MongoDB_defaults = require ('../QFactory-MongoDB-defaults');
5
9
 
6
- var Queue = require ('../Queue');
7
- var QFactory_MongoDB_defaults = require ('../QFactory-MongoDB-defaults');
8
10
 
9
11
  class SimpleMongoQueue extends Queue {
10
12
 
@@ -20,187 +22,228 @@ class SimpleMongoQueue extends Queue {
20
22
  return 'mongo:simple';
21
23
  }
22
24
 
25
+
23
26
  /////////////////////////////////////////
24
27
  type () {
25
28
  return 'mongo:simple';
26
29
  }
27
30
 
31
+
28
32
  /////////////////////////////////////////
29
33
  // add element to queue
30
- insert (entry, callback) {
31
- this._col.insertOne (entry, {}, (err, result) => {
32
- if (err) return callback (err);
33
- callback (null, result.insertedId);
34
- });
34
+ insert (entry, cb) {
35
+ this._col.insertOne (entry)
36
+ .then (res => {
37
+ debug ('inserted entry %j -> %j', entry, res)
38
+ cb (null, res.insertedId);
39
+ })
40
+ .catch (cb);
35
41
  }
36
-
42
+ c
37
43
 
38
44
  /////////////////////////////////////////
39
45
  // get element from queue
40
- get (callback) {
41
- this._col.findOneAndDelete ({mature: {$lte: Queue.nowPlusSecs (0)}}, {sort: {mature : 1}}, (err, result) => {
42
- if (err) return callback (err);
46
+ get (cb) {
47
+ const query = {
48
+ mature: {$lte: Queue.nowPlusSecs (0)}
49
+ };
50
+
51
+ const opts = {
52
+ sort: {mature : 1},
53
+ includeResultMetadata: true
54
+ };
55
+
56
+ this._col.findOneAndDelete (query, opts)
57
+ .then (result => {
58
+ debug ('get() element, findOneAndDelete returned %j', result)
43
59
  const v = result && result.value;
44
- if (!v) return callback ();
60
+ if (!v) return cb ();
45
61
  if (v.payload._bsontype == 'Binary') v.payload = v.payload.buffer;
46
- callback (null, v);
47
- });
62
+ cb (null, v);
63
+ })
64
+ .catch (cb);
48
65
  }
49
-
66
+
50
67
 
51
68
  //////////////////////////////////
52
69
  // reserve element: call cb (err, pl) where pl has an id
53
- reserve (callback) {
54
- var delay = this._opts.reserve_delay || 120;
70
+ reserve (cb) {
71
+ const delay = this._opts.reserve_delay || 120;
55
72
 
56
- var query = {
73
+ const query = {
57
74
  mature: {$lte: Queue.nowPlusSecs (0)}
58
75
  };
59
76
 
60
- var update = {
77
+ const update = {
61
78
  $set: {mature: Queue.nowPlusSecs (delay), reserved: new Date ()},
62
79
  $inc: {tries: 1}
63
80
  };
64
81
 
65
- var opts = {
82
+ const opts = {
66
83
  sort: {mature : 1},
67
- returnDocument: 'before'
84
+ returnDocument: 'before',
85
+ includeResultMetadata: true
68
86
  };
69
87
 
70
- this._col.findOneAndUpdate (query, update, opts, (err, result) => {
71
- if (err) return callback (err);
72
- const v = result && result.value;
73
- if (!v) return callback ();
88
+ this._col.findOneAndUpdate (query, update, opts)
89
+ .then (res => {
90
+ debug ('reserve() element, findOneAndUpdate returned %j', res)
91
+ const v = res && res.value;
92
+ if (!v) return cb ();
74
93
  if (v.payload._bsontype == 'Binary') v.payload = v.payload.buffer;
75
- callback (null, v);
76
- });
94
+ cb (null, v);
95
+ })
96
+ .catch (cb);
77
97
  }
78
98
 
79
99
 
80
100
  //////////////////////////////////
81
101
  // commit previous reserve, by p.id
82
- commit (id, callback) {
102
+ commit (id, cb) {
103
+ const query = {
104
+ reserved: {$exists: true}
105
+ };
106
+
83
107
  try {
84
- var query = {
85
- _id: (_.isString(id) ? new mongo.ObjectID (id) : id),
86
- reserved: {$exists: true}
87
- };
108
+ query._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
88
109
  }
89
110
  catch (e) {
90
- return callback ('id [' + id + '] can not be used as rollback id: ' + e);
111
+ return cb ('id [' + id + '] can not be used as rollback id: ' + e);
91
112
  }
92
113
 
93
- this._col.deleteOne (query, {}, (err, result) => {
94
- if (err) return callback (err);
95
- callback (null, result && (result.deletedCount == 1));
96
- });
114
+ this._col.deleteOne (query, {})
115
+ .then (res => {
116
+ debug ('commit(%s) element, deleteOne returned %j', id, res)
117
+ cb (null, res && (res.deletedCount == 1));
118
+ })
119
+ .catch (cb);
97
120
  }
98
121
 
99
122
 
100
123
  //////////////////////////////////
101
124
  // rollback previous reserve, by p.id
102
- rollback (id, next_t, callback) {
125
+ rollback (id, next_t, cb) {
103
126
  if (_.isFunction (next_t)) {
104
- callback = next_t;
127
+ cb = next_t;
105
128
  next_t = null;
106
129
  }
107
130
 
131
+ const query = {
132
+ reserved: {$exists: true}
133
+ };
134
+
108
135
  try {
109
- var query = {
110
- _id: (_.isString(id) ? new mongo.ObjectID (id) : id),
111
- reserved: {$exists: true}
112
- };
136
+ query ._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
113
137
  }
114
138
  catch (e) {
115
- return callback ('id [' + id + '] can not be used as rollback id: ' + e);
139
+ return cb ('id [' + id + '] can not be used as rollback id: ' + e);
116
140
  }
117
141
 
118
- var update = {
142
+ const update = {
119
143
  $set: {mature: (next_t ? new Date (next_t) : Queue.now ())},
120
144
  $unset: {reserved: ''}
121
145
  };
122
146
 
123
- this._col.updateOne (query, update, {}, (err, result) => {
124
- if (err) return callback (err);
125
- callback (null, result && (result.modifiedCount == 1));
126
- });
147
+ this._col.updateOne (query, update, {})
148
+ .then (res => {
149
+ debug ('rollback(%s) element, updateOne returned %j', id, res)
150
+ cb (null, res && (res.modifiedCount == 1));
151
+ })
152
+ .catch (cb);
127
153
  }
128
154
 
129
155
 
130
156
  //////////////////////////////////
131
157
  // queue size including non-mature elements
132
- totalSize (callback) {
133
- var q = {};
134
- var opts = {};
135
- this._col.countDocuments (q, opts, callback);
158
+ totalSize (cb) {
159
+ const q = {};
160
+ const opts = {};
161
+ this._col.countDocuments (q, opts)
162
+ .then (res => cb (null, res))
163
+ .catch (cb);
136
164
  }
137
165
 
138
166
 
139
167
  //////////////////////////////////
140
168
  // queue size NOT including non-mature elements
141
- size (callback) {
142
- var q = {
169
+ size (cb) {
170
+ const q = {
143
171
  mature : {$lte : Queue.now ()}
144
172
  };
145
173
 
146
- var opts = {};
147
- this._col.countDocuments (q, opts, callback);
174
+ const opts = {};
175
+ this._col.countDocuments (q, opts)
176
+ .then (res => cb (null, res))
177
+ .catch (cb);
148
178
  }
149
179
 
150
180
 
151
181
  //////////////////////////////////
152
182
  // queue size of non-mature elements only
153
- schedSize (callback) {
154
- var q = {
183
+ schedSize (cb) {
184
+ const q = {
155
185
  mature : {$gt : Queue.now ()},
156
186
  reserved: {$exists: false}
157
187
  };
158
188
 
159
- var opts = {};
160
- this._col.countDocuments (q, opts, callback);
189
+ const opts = {};
190
+ this._col.countDocuments (q, opts)
191
+ .then (res => cb (null, res))
192
+ .catch (cb);
161
193
  }
162
194
 
163
195
 
164
196
  //////////////////////////////////
165
197
  // queue size of reserved elements only
166
- resvSize (callback) {
167
- var q = {
198
+ resvSize (cb) {
199
+ const q = {
168
200
  mature : {$gt : Queue.now ()},
169
201
  reserved: {$exists: true}
170
202
  };
171
203
 
172
- var opts = {};
173
- this._col.countDocuments (q, opts, callback);
204
+ const opts = {};
205
+ this._col.countDocuments (q, opts)
206
+ .then (res => cb (null, res))
207
+ .catch (cb);
174
208
  }
175
209
 
176
210
 
177
211
  /////////////////////////////////////////
178
212
  // get element from queue
179
- next_t (callback) {
180
- this._col.find ({}).limit(1).sort ({mature:1}).project ({mature:1}).next ((err, result) => {
181
- if (err) return callback (err);
182
- callback (null, result && result.mature);
183
- });
213
+ next_t (cb) {
214
+ this._col.find ({})
215
+ .limit(1)
216
+ .sort ({mature:1})
217
+ .project ({mature:1})
218
+ .next ()
219
+ .then (res => {
220
+ debug ('next_t(), find returned %j', res)
221
+ cb (null, res && res.mature);
222
+ })
223
+ .catch (cb);
184
224
  }
185
225
 
186
226
 
187
227
  //////////////////////////////////////////////
188
228
  // remove by id
189
- remove (id, callback) {
229
+ remove (id, cb) {
230
+ const query = {
231
+ reserved: {$exists: false}
232
+ };
233
+
190
234
  try {
191
- var query = {
192
- _id: (_.isString(id) ? new mongo.ObjectID (id) : id),
193
- reserved: {$exists: false}
194
- };
235
+ query._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
195
236
  }
196
237
  catch (e) {
197
- return callback ('id [' + id + '] can not be used as remove id: ' + e);
238
+ return cb ('id [' + id + '] can not be used as remove id: ' + e);
198
239
  }
199
240
 
200
- this._col.deleteOne (query, {}, (err, result) => {
201
- if (err) return callback (err);
202
- callback (null, result && (result.deletedCount == 1));
203
- });
241
+ this._col.deleteOne (query, {})
242
+ .then (res => {
243
+ debug ('remove(%s) element, deleteOne returned %j', id, res)
244
+ cb (null, res && (res.deletedCount == 1));
245
+ })
246
+ .catch (cb)
204
247
  }
205
248
 
206
249
 
@@ -210,11 +253,14 @@ class SimpleMongoQueue extends Queue {
210
253
  //////////////////////////////////////////////////////////////////
211
254
  // create needed indexes for O(1) functioning
212
255
  _ensureIndexes (cb) {
213
- this._col.createIndex ({mature : 1}, err => cb (err, this));
256
+ this._col.createIndex ({mature : 1})
257
+ .then (() => cb (null, this))
258
+ .catch (cb);
214
259
  }
215
260
  };
216
261
 
217
262
 
263
+ ///////////////////////////////////////////////////////////////////////////////
218
264
  class Factory extends QFactory_MongoDB_defaults {
219
265
  constructor (opts, mongo_conn) {
220
266
  super (opts);
@@ -262,19 +308,15 @@ class Factory extends QFactory_MongoDB_defaults {
262
308
  }
263
309
 
264
310
  function creator (opts, cb) {
265
- var _opts = opts || {};
266
- var m_url = _opts.url || 'mongodb://localhost:27017/keuss';
311
+ const _opts = opts || {};
312
+ const m_url = _opts.url || 'mongodb://localhost:27017/keuss';
267
313
 
268
- MongoClient.connect (m_url, { useNewUrlParser: true }, (err, cl) => {
269
- if (err) return cb (err);
270
- var F = new Factory (_opts, cl);
314
+ MongoClient.connect (m_url)
315
+ .then (cl => {
316
+ const F = new Factory (_opts, cl);
271
317
  F.async_init (err => cb (null, F));
272
- });
318
+ })
319
+ .catch (cb);
273
320
  }
274
321
 
275
322
  module.exports = creator;
276
-
277
-
278
-
279
-
280
-
@@ -31,7 +31,8 @@ class Factory extends QFactory_MongoDB_defaults {
31
31
  }
32
32
  }, {
33
33
  upsert: true
34
- });
34
+ })
35
+ .finally (() => {});
35
36
  }
36
37
 
37
38
 
@@ -98,9 +99,8 @@ class Factory extends QFactory_MongoDB_defaults {
98
99
  }
99
100
  }, {
100
101
  upsert: true
101
- });
102
-
103
-
102
+ })
103
+ .finally (() => {});
104
104
  }
105
105
 
106
106
 
@@ -141,11 +141,15 @@ class Factory extends QFactory_MongoDB_defaults {
141
141
  close (cb) {
142
142
  super.close (() => {
143
143
  async.parallel ([
144
- cb => this._mongo_data_conn.close (cb),
145
- cb => this._mongo_topology_conn.close (cb)
146
- ], err => {
144
+ cb => this._mongo_data_conn.close ().then (res => cb (null, res)).catch (cb),
145
+ cb => this._mongo_topology_conn.close ().then (res => cb (null, res)).catch (cb),
146
+ ])
147
+ .then (res => {
147
148
  this._mongo_data_conn = null;
148
149
  this._mongo_topology_conn = null;
150
+ if (cb) return cb ();
151
+ })
152
+ .catch (err => {
149
153
  if (cb) return cb (err);
150
154
  });
151
155
  });
@@ -194,30 +198,14 @@ function creator (opts, cb) {
194
198
  }
195
199
 
196
200
  async.series ([
197
- cb => MongoClient.connect (m_url, { useNewUrlParser: true }, (err, cl) => {
198
- if (err) {
199
- debug ('error while connecting to data mongoDB [%s]', m_url, err);
200
- return cb (err);
201
- }
202
-
203
- debug ('connected OK to data mongoDB %s', m_url);
204
- cb (null, cl);
205
- }),
206
- cb => MongoClient.connect (m_topology_url, { useNewUrlParser: true }, (err, cl) => {
207
- if (err) {
208
- debug ('error while connecting to topology mongoDB [%s]', m_topology_url, err);
209
- return cb (err);
210
- }
211
-
212
- debug ('connected OK to topology mongoDB %s', m_topology_url);
213
- cb (null, cl);
214
- }),
215
- ], (err, res) => {
216
- if (err) return cb (err);
217
-
218
- var F = new Factory (_opts, res[0], res[1]);
201
+ cb => MongoClient.connect (m_url).then (res => cb (null, res)).catch (cb),
202
+ cb => MongoClient.connect (m_topology_url).then (res => cb (null, res)).catch (cb),
203
+ ])
204
+ .then (res => {
205
+ const F = new Factory (_opts, res[0], res[1]);
219
206
  F.async_init (err => cb (null, F));
220
- });
207
+ })
208
+ .catch (cb);
221
209
  }
222
210
 
223
211
  module.exports = creator;