keuss 2.1.0 → 2.2.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/Pipeline/Queue.js CHANGED
@@ -37,7 +37,16 @@ class PipelinedMongoQueue extends Queue {
37
37
 
38
38
  this._col.insertOne (entry, {})
39
39
  .then (res => cb (null, res.insertedId))
40
- .catch (cb);
40
+ .catch (err => {
41
+ if (err.code == 11000) {
42
+ const e = new Error (`duplicated entry with _id ${entry._id}`)
43
+ e.code = 'EDUP';
44
+ cb(e)
45
+ }
46
+ else {
47
+ cb (err)
48
+ }
49
+ });
41
50
  }
42
51
 
43
52
 
package/Queue.js CHANGED
@@ -321,6 +321,8 @@ class Queue {
321
321
  hdrs: opts.hdrs || {}
322
322
  };
323
323
 
324
+ if (opts.id) msg._id = opts.id;
325
+
324
326
  debug ('%s: about to insert %o', this._name, msg);
325
327
 
326
328
  // insert into queue
@@ -959,7 +959,8 @@ class Factory extends QFactory_MongoDB_defaults {
959
959
  reserve: true,
960
960
  pipeline: false,
961
961
  tape: false,
962
- remove: true
962
+ remove: true,
963
+ id: false,
963
964
  };
964
965
  }
965
966
  }
@@ -364,7 +364,8 @@ class Factory extends QFactory_MongoDB_defaults {
364
364
  reserve: true,
365
365
  pipeline: false,
366
366
  tape: false,
367
- remove: false
367
+ remove: false,
368
+ id: false,
368
369
  };
369
370
  }
370
371
  }
package/backends/mongo.js CHANGED
@@ -37,7 +37,16 @@ class SimpleMongoQueue extends Queue {
37
37
  debug ('inserted entry %j -> %j', entry, res)
38
38
  cb (null, res.insertedId);
39
39
  })
40
- .catch (cb);
40
+ .catch (err => {
41
+ if (err.code == 11000) {
42
+ const e = new Error (`duplicated entry with _id ${entry._id}`)
43
+ e.code = 'EDUP';
44
+ cb(e)
45
+ }
46
+ else {
47
+ cb (err)
48
+ }
49
+ });
41
50
  }
42
51
  c
43
52
 
@@ -302,7 +311,8 @@ class Factory extends QFactory_MongoDB_defaults {
302
311
  reserve: true,
303
312
  pipeline: false,
304
313
  tape: false,
305
- remove: true
314
+ remove: true,
315
+ id: true,
306
316
  };
307
317
  }
308
318
  }
@@ -169,7 +169,8 @@ class Factory extends QFactory_MongoDB_defaults {
169
169
  reserve: true,
170
170
  pipeline: true,
171
171
  tape: false,
172
- remove: true
172
+ remove: true,
173
+ id: true,
173
174
  };
174
175
  }
175
176
 
@@ -58,7 +58,7 @@ class PGQueue extends Queue {
58
58
  /////////////////////////////////////////
59
59
  // add element to queue
60
60
  insert (entry, cb) {
61
- const _id = entry.id || uuid.v4();
61
+ const _id = entry._id || uuid.v4();
62
62
  const tries = entry.tries || 0;
63
63
  const mature = new Date (entry.mature);
64
64
 
@@ -73,7 +73,19 @@ class PGQueue extends Queue {
73
73
  }
74
74
 
75
75
  this._pool.query (`INSERT INTO ${this._tbl_name} VALUES($1, $2, $3, $4)`, [_id, pl, mature, tries], (err, res) => {
76
- if (err) return cb (err);
76
+ if (err) {
77
+ if (err.code == '23505') {
78
+ const e = new Error (`duplicated entry with _id ${entry._id}`)
79
+ e.code = 'EDUP';
80
+ cb(e)
81
+ }
82
+ else {
83
+ cb (err)
84
+ }
85
+
86
+ return;
87
+ }
88
+
77
89
  cb (null, _id)
78
90
  })
79
91
  }
@@ -353,7 +365,8 @@ class Factory extends QFactory {
353
365
  reserve: true,
354
366
  pipeline: false,
355
367
  tape: false,
356
- remove: true
368
+ remove: true,
369
+ id: true,
357
370
  };
358
371
  }
359
372
  }
@@ -35,7 +35,16 @@ class PersistentMongoQueue extends Queue {
35
35
  insert (entry, cb) {
36
36
  this._col.insertOne (entry, {})
37
37
  .then (res => cb (null, res.insertedId))
38
- .catch (cb);
38
+ .catch (err => {
39
+ if (err.code == 11000) {
40
+ const e = new Error (`duplicated entry with _id ${entry._id}`)
41
+ e.code = 'EDUP';
42
+ cb(e)
43
+ }
44
+ else {
45
+ cb (err)
46
+ }
47
+ });
39
48
  }
40
49
 
41
50
 
@@ -332,7 +341,8 @@ class Factory extends QFactory_MongoDB_defaults {
332
341
  reserve: true,
333
342
  pipeline: false,
334
343
  tape: true,
335
- remove: true
344
+ remove: true,
345
+ id: true,
336
346
  };
337
347
  }
338
348
  }
@@ -134,7 +134,8 @@ class Factory extends QFactory {
134
134
  reserve: false,
135
135
  pipeline: false,
136
136
  tape: false,
137
- remove: true
137
+ remove: true,
138
+ id: false,
138
139
  };
139
140
  }
140
141
  }
@@ -156,7 +156,8 @@ class Factory extends QFactory {
156
156
  reserve: true,
157
157
  pipeline: false,
158
158
  tape: false,
159
- remove: true
159
+ remove: true,
160
+ id: true,
160
161
  };
161
162
  }
162
163
  }
@@ -469,7 +469,8 @@ class Factory extends QFactory_MongoDB_defaults {
469
469
  pipeline: false,
470
470
  tape: true,
471
471
  remove: false,
472
- stream: true
472
+ stream: true,
473
+ id: false,
473
474
  };
474
475
  }
475
476
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keuss",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "keywords": [
5
5
  "queue",
6
6
  "persistent",
@@ -39,13 +39,13 @@
39
39
  "pg": "~8.12.0"
40
40
  },
41
41
  "devDependencies": {
42
- "chance": "~1.1.12",
43
- "mocha": "~11.7.4",
42
+ "chance": "~1.1.13",
43
+ "mocha": "~11.7.5",
44
44
  "should": "~13.2.3",
45
45
  "nyc": "~17.1.0"
46
46
  },
47
47
  "scripts": {
48
- "test": "docker compose up -d; sleep 5; mocha --bail --reporter spec --check-leaks --no-timeouts --exit test/ ; docker compose down",
48
+ "test": "docker compose up -d; sleep 5; mocha --reporter spec --check-leaks --no-timeouts --exit test/ ; docker compose down",
49
49
  "test-with-coverage": "docker compose up -d; sleep 5; nyc --reporter=html -- mocha --reporter spec --check-leaks --no-timeouts --exit ; test/docker compose down"
50
50
  }
51
51
  }
@@ -7,6 +7,13 @@ const _s_lua_code_push = `
7
7
  -- mature-t in ARGV[2]
8
8
  -- val in ARGV[3]
9
9
 
10
+ -- check id exists
11
+ local exists = redis.call ('HGET', 'keuss:q:ordered_queue:hash:' .. KEYS[1], ARGV[1])
12
+
13
+ if (exists) then
14
+ return redis.error_reply('EDUP duplicated _id ' .. ARGV[1])
15
+ end
16
+
10
17
  -- insert obj in hash by id
11
18
  redis.call ('HSET', 'keuss:q:ordered_queue:hash:' .. KEYS[1], ARGV[1], ARGV[3])
12
19
 
@@ -174,20 +181,33 @@ class RedisOrderedQueue {
174
181
  push (entry, done) {
175
182
  //////////////////////////////////
176
183
  var pl = {
177
- _id: entry.id || uuid.v4(),
184
+ _id: entry._id || uuid.v4(),
178
185
  payload: entry.payload,
179
186
  tries: entry.tries,
180
187
  hdrs: entry.hdrs || {},
181
188
  mature: (entry.mature || new Date ()).getTime ()
182
189
  };
183
190
 
184
-
185
191
  if (Buffer.isBuffer (pl.payload)) {
186
192
  pl.payload = pl.payload.toString ('base64');
187
193
  pl.type = 'buffer';
188
194
  }
189
195
 
190
- this._rediscl.roq_push (this._name, pl._id, pl.mature, JSON.stringify (pl), done);
196
+ this._rediscl.roq_push (this._name, pl._id, pl.mature, JSON.stringify (pl), (err, res) => {
197
+ if (err) {
198
+ if (err.message.startsWith ('EDUP ')) {
199
+ const e = new Error (`duplicated entry with _id ${pl._id}`)
200
+ e.code = 'EDUP';
201
+ done(e);
202
+ }
203
+ else {
204
+ done (err)
205
+ }
206
+ }
207
+ else {
208
+ done (null, res);
209
+ }
210
+ });
191
211
  }
192
212
 
193
213