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.
package/.mocharc.yaml ADDED
@@ -0,0 +1,3 @@
1
+ bail: false
2
+ timeout: false
3
+
@@ -55,6 +55,7 @@ class BaseLink extends EventEmitter {
55
55
  /////////////////////////////////////////
56
56
  stop () {
57
57
  this._src.cancel ();
58
+ debug ('%s: stopped', this._name);
58
59
  }
59
60
 
60
61
 
@@ -164,7 +165,6 @@ class BaseLink extends EventEmitter {
164
165
  }
165
166
  catch (e) {
166
167
  debug ('catch error, emitting it: ', e);
167
- console.log ('catch error, emitting it: ', e);
168
168
  this.emit (e);
169
169
  this._on_error (e, elem, ondata);
170
170
  }
package/Pipeline/Queue.js CHANGED
@@ -32,125 +32,133 @@ class PipelinedMongoQueue extends Queue {
32
32
 
33
33
  /////////////////////////////////////////
34
34
  // add element to queue
35
- insert (entry, callback) {
35
+ insert (entry, cb) {
36
36
  entry._q = this._name;
37
37
 
38
- this._col.insertOne (entry, {}, (err, result) => {
39
- if (err) return callback (err);
40
- callback (null, result.insertedId);
41
- });
38
+ this._col.insertOne (entry, {})
39
+ .then (res => cb (null, res.insertedId))
40
+ .catch (cb);
42
41
  }
43
42
 
44
43
 
45
44
  /////////////////////////////////////////
46
45
  // get element from queue
47
- get (callback) {
48
- this._col.findOneAndDelete ({_q: this._name, mature: {$lte: Queue.nowPlusSecs (0)}}, {sort: {mature : 1}}, (err, result) => {
49
- if (err) return callback (err);
50
- const v = result && result.value;
51
- if (!v) return callback ();
46
+ get (cb) {
47
+ const query = {
48
+ _q: this._name,
49
+ mature: {$lte: Queue.nowPlusSecs (0)}
50
+ };
51
+
52
+ const opts = {
53
+ sort: {mature : 1},
54
+ includeResultMetadata: true
55
+ }
56
+
57
+ this._col.findOneAndDelete (query, opts)
58
+ .then (res => {
59
+ const v = res && res.value;
60
+ if (!v) return cb ();
52
61
  if (v.payload._bsontype == 'Binary') v.payload = v.payload.buffer;
53
- callback (null, v);
54
- });
62
+ cb (null, v);
63
+ })
64
+ .catch (cb);
55
65
  }
56
66
 
57
67
 
58
68
  //////////////////////////////////
59
69
  // reserve element: call cb (err, pl) where pl has an id
60
- reserve (callback) {
61
- var delay = this._opts.reserve_delay || 120;
70
+ reserve (cb) {
71
+ const delay = this._opts.reserve_delay || 120;
62
72
 
63
- var query = {
73
+ const query = {
64
74
  _q: this._name,
65
75
  mature: {$lte: Queue.nowPlusSecs (0)}
66
76
  };
67
77
 
68
- var update = {
78
+ const update = {
69
79
  $set: {mature: Queue.nowPlusSecs (delay), reserved: new Date ()},
70
80
  $inc: {tries: 1}
71
81
  };
72
82
 
73
- var opts = {
83
+ const opts = {
74
84
  sort: {mature : 1},
75
- returnDocument: 'before'
85
+ returnDocument: 'before',
86
+ includeResultMetadata: true
76
87
  };
77
88
 
78
- this._col.findOneAndUpdate (query, update, opts, (err, result) => {
79
- if (err) return callback (err);
80
- const v = result && result.value;
81
- if (!v) return callback ();
89
+ this._col.findOneAndUpdate (query, update, opts)
90
+ .then (res => {
91
+ const v = res && res.value;
92
+ if (!v) return cb ();
82
93
  if (v.payload._bsontype == 'Binary') v.payload = v.payload.buffer;
83
- callback (null, v);
84
- });
94
+ cb (null, v);
95
+ })
96
+ .catch (cb);
85
97
  }
86
98
 
87
99
 
88
100
  //////////////////////////////////
89
101
  // commit previous reserve, by p.id
90
- commit (id, callback) {
91
- var query;
102
+ commit (id, cb) {
103
+ const query = {
104
+ _q: this._name,
105
+ reserved: {$exists: true}
106
+ }
92
107
 
93
108
  try {
94
- query = {
95
- _id: (_.isString(id) ? new mongo.ObjectID (id) : id),
96
- _q: this._name,
97
- reserved: {$exists: true}
98
- };
109
+ query._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
99
110
  }
100
111
  catch (e) {
101
- return callback ('id [' + id + '] can not be used as rollback id: ' + e);
112
+ return cb ('id [' + id + '] can not be used as rollback id: ' + e);
102
113
  }
103
114
 
104
- this._col.deleteOne (query, {}, (err, result) => {
105
- if (err) return callback (err);
106
- callback (null, result && (result.deletedCount == 1));
107
- });
115
+ this._col.deleteOne (query, {})
116
+ .then (res => cb (null, res && (res.deletedCount == 1)))
117
+ .catch (cb);
108
118
  }
109
119
 
110
120
 
111
121
  //////////////////////////////////
112
122
  // rollback previous reserve, by p.id
113
- rollback (id, next_t, callback) {
123
+ rollback (id, next_t, cb) {
114
124
  if (_.isFunction (next_t)) {
115
- callback = next_t;
125
+ cb = next_t;
116
126
  next_t = null;
117
127
  }
118
128
 
119
- var query;
129
+ const query = {
130
+ _q: this._name,
131
+ reserved: {$exists: true}
132
+ }
120
133
 
121
134
  try {
122
- query = {
123
- _id: (_.isString(id) ? new mongo.ObjectID (id) : id),
124
- _q: this._name,
125
- reserved: {$exists: true}
126
- };
135
+ query._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
127
136
  }
128
137
  catch (e) {
129
- return callback ('id [' + id + '] can not be used as rollback id: ' + e);
138
+ return cb ('id [' + id + '] can not be used as rollback id: ' + e);
130
139
  }
131
140
 
132
- var update = {
141
+ const update = {
133
142
  $set: {mature: (next_t ? new Date (next_t) : Queue.now ())},
134
143
  $unset: {reserved: ''}
135
144
  };
136
145
 
137
- this._col.updateOne (query, update, {}, (err, result) => {
138
- if (err) return callback (err);
139
- callback (null, result && (result.modifiedCount == 1));
140
- });
146
+ this._col.updateOne (query, update, {})
147
+ .then (res => cb (null, res && (res.modifiedCount == 1)))
148
+ .catch (cb);
141
149
  }
142
150
 
143
151
 
144
152
  //////////////////////////////////
145
153
  // passes element to the next queue in pipeline
146
- pl_step (id, next_queue, opts, callback) {
147
- var q = {
154
+ pl_step (id, next_queue, opts, cb) {
155
+ const q = {
148
156
  _id: id,
149
157
  _q: this._name,
150
158
  reserved: {$exists: true}
151
159
  };
152
160
 
153
- var upd = {
161
+ const upd = {
154
162
  $set: {
155
163
  mature: opts.mature || Queue.now (),
156
164
  tries: opts.tries || 0,
@@ -170,93 +178,101 @@ class PipelinedMongoQueue extends Queue {
170
178
  _.each (opts.hdrs, (v, k) => upd.$set['hdrs.' + k] = v);
171
179
  }
172
180
 
173
- this._col.updateOne (q, upd, {}, (err, result) => {
174
- if (err) return callback (err);
175
- callback (null, result && (result.modifiedCount == 1));
176
- });
181
+ this._col.updateOne (q, upd, {})
182
+ .then (res => cb (null, res && (res.modifiedCount == 1)))
183
+ .catch (cb);
177
184
  }
178
185
 
179
186
 
180
187
  //////////////////////////////////
181
188
  // queue size including non-mature elements
182
- totalSize (callback) {
183
- var q = {_q: this._name};
184
- var opts = {};
185
- this._col.countDocuments (q, opts, callback);
189
+ totalSize (cb) {
190
+ const q = {_q: this._name};
191
+ const opts = {};
192
+ this._col.countDocuments (q, opts)
193
+ .then (res => cb (null, res))
194
+ .catch (cb);
186
195
  }
187
196
 
188
197
 
189
198
  //////////////////////////////////
190
199
  // queue size NOT including non-mature elements
191
- size (callback) {
192
- var q = {
200
+ size (cb) {
201
+ const q = {
193
202
  _q: this._name,
194
203
  mature : {$lte : Queue.now ()}
195
204
  };
196
205
 
197
- var opts = {};
198
- this._col.countDocuments (q, opts, callback);
206
+ const opts = {};
207
+ this._col.countDocuments (q, opts)
208
+ .then (res => cb (null, res))
209
+ .catch (cb);
199
210
  }
200
211
 
201
212
 
202
213
  //////////////////////////////////
203
214
  // queue size of non-mature elements only
204
- schedSize (callback) {
205
- var q = {
215
+ schedSize (cb) {
216
+ const q = {
206
217
  _q: this._name,
207
218
  mature : {$gt : Queue.now ()},
208
219
  reserved: {$exists: false}
209
220
  };
210
221
 
211
- var opts = {};
212
- this._col.countDocuments (q, opts, callback);
222
+ const opts = {};
223
+ this._col.countDocuments (q, opts)
224
+ .then (res => cb (null, res))
225
+ .catch (cb);
213
226
  }
214
227
 
215
228
 
216
229
  //////////////////////////////////
217
230
  // queue size of reserved elements only
218
- resvSize (callback) {
219
- var q = {
231
+ resvSize (cb) {
232
+ const q = {
220
233
  _q: this._name,
221
234
  mature : {$gt : Queue.now ()},
222
235
  reserved: {$exists: true}
223
236
  };
224
237
 
225
- var opts = {};
226
- this._col.countDocuments (q, opts, callback);
238
+ const opts = {};
239
+ this._col.countDocuments (q, opts)
240
+ .then (res => cb (null, res))
241
+ .catch (cb);
227
242
  }
228
243
 
229
244
 
230
245
  /////////////////////////////////////////
231
246
  // get element from queue
232
- next_t (callback) {
233
- this._col.find ({_q: this._name}).limit(1).sort ({mature:1}).project ({mature:1}).next ((err, result) => {
234
- if (err) return callback (err);
235
- callback (null, result && result.mature);
236
- });
247
+ next_t (cb) {
248
+ this._col.find ({_q: this._name})
249
+ .limit(1)
250
+ .sort ({mature:1})
251
+ .project ({mature:1})
252
+ .next ()
253
+ .then (res => cb (null, res && res.mature))
254
+ .catch (cb);
237
255
  }
238
256
 
239
257
 
240
258
  //////////////////////////////////////////////
241
259
  // remove by id
242
- remove (id, callback) {
243
- var query;
260
+ remove (id, cb) {
261
+ const query = {
262
+ _q: this._name,
263
+ reserved: {$exists: false}
264
+ }
244
265
 
245
266
  try {
246
- query = {
247
- _id: (_.isString(id) ? new mongo.ObjectID (id) : id),
248
- _q: this._name,
249
- reserved: {$exists: false}
250
- };
267
+ query._id = (_.isString(id) ? new mongo.ObjectId (id) : id);
251
268
  }
252
269
  catch (e) {
253
- return callback ('id [' + id + '] can not be used as remove id: ' + e);
270
+ return cb ('id [' + id + '] can not be used as remove id: ' + e);
254
271
  }
255
272
 
256
- this._col.deleteOne (query, {}, (err, result) => {
257
- if (err) return callback (err);
258
- callback (null, result && (result.deletedCount == 1));
259
- });
273
+ this._col.deleteOne (query, {})
274
+ .then (res => cb (null, res && (res.deletedCount == 1)))
275
+ .catch (cb);
260
276
  }
261
277
 
262
278
 
package/Queue.js CHANGED
@@ -489,9 +489,9 @@ class Queue {
489
489
  //////////////////////////////////
490
490
  // cancel a waiting consumer
491
491
  cancel (tid) {
492
- debug ('%s: cancelling tid %s', this._name, tid);
493
-
494
492
  if (tid) {
493
+ debug ('%s: cancelling tid %s', this._name, tid);
494
+
495
495
  let consumer_data = this._consumers_by_tid.get (tid);
496
496
 
497
497
  if (!consumer_data) {
@@ -535,18 +535,27 @@ class Queue {
535
535
 
536
536
  debug ('%s: cancelling tid %s (cid %s): callback called and removed', this._name, tid, consumer_data.cid);
537
537
  }
538
+ else {
539
+ debug ('%s: cancelling tid %s (cid %s): has no callback!', this._name, tid);
540
+ }
538
541
 
539
542
  if (consumer_data.cleanup_timeout) {
540
543
  clearTimeout (consumer_data.cleanup_timeout);
541
544
  consumer_data.cleanup_timeout = null;
542
545
  debug ('%s: cancelling tid %s (cid %s): cleanup timeout removed', this._name, tid, consumer_data.cid);
543
546
  }
547
+ else {
548
+ debug ('%s: cancelling tid %s (cid %s): has no cleanup timeout!', this._name, tid);
549
+ }
544
550
 
545
551
  if (consumer_data.wakeup_timeout) {
546
552
  clearTimeout (consumer_data.wakeup_timeout);
547
553
  consumer_data.wakeup_timeout = null;
548
554
  debug ('%s: cancelling tid %s (cid %s): wakeup timeout removed', this._name, tid, consumer_data.cid);
549
555
  }
556
+ else {
557
+ debug ('%s: cancelling tid %s (cid %s): has no wakeup timeout!', this._name, tid);
558
+ }
550
559
 
551
560
  debug ('%s: cancelling tid %s (cid %s): end', this._name, tid, consumer_data.cid);
552
561
  });
@@ -654,9 +663,11 @@ class Queue {
654
663
  };
655
664
 
656
665
  if (consumer.reserve) {
666
+ debug ('%s - tid %s: calling getOrReserve_cb with reserve', this._name, consumer.tid);
657
667
  this.reserve (getOrReserve_cb);
658
668
  }
659
669
  else {
670
+ debug ('%s - tid %s: calling getOrReserve_cb with get', this._name, consumer.tid);
660
671
  this.get (getOrReserve_cb);
661
672
  }
662
673
  }