power-queues 2.1.2 → 2.1.4

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/dist/index.cjs CHANGED
@@ -275,7 +275,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
275
275
  this.selectStuckTimeout = 6e4;
276
276
  this.selectStuckMaxTimeout = 80;
277
277
  this.selectCount = 200;
278
- this.selectTimeout = 5e3;
278
+ this.selectTimeout = 3e3;
279
279
  this.buildBatchCount = 800;
280
280
  this.buildBatchMaxCount = 1e4;
281
281
  this.retryCount = 1;
@@ -283,7 +283,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
283
283
  this.idemLockTimeout = 18e4;
284
284
  this.idemDoneTimeout = 6e4;
285
285
  this.logStatus = false;
286
- this.logStatusTimeout = 3e5;
286
+ this.logStatusTimeout = 18e5;
287
287
  this.approveCount = 2e3;
288
288
  this.removeOnExecuted = true;
289
289
  }
@@ -316,7 +316,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
316
316
  } catch (err) {
317
317
  }
318
318
  if (!(0, import_full_utils.isArrFilled)(tasks)) {
319
- await (0, import_full_utils.wait)(400);
319
+ await (0, import_full_utils.wait)(300);
320
320
  continue;
321
321
  }
322
322
  try {
@@ -327,7 +327,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
327
327
  await this.approve(queueName, tasks.map((task) => task[0]));
328
328
  } catch {
329
329
  }
330
- await (0, import_full_utils.wait)(400);
330
+ await (0, import_full_utils.wait)(300);
331
331
  }
332
332
  }
333
333
  }
@@ -339,17 +339,26 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
339
339
  if (!filtered[key]) {
340
340
  filtered[key] = [];
341
341
  }
342
- filtered[key].push(tasks[index]);
342
+ filtered[key].push(tasks[index][1]);
343
343
  });
344
344
  for (let key in filtered) {
345
345
  const filteredTasks = filtered[key];
346
346
  const keySplit = key.split(":");
347
347
  const attempt = Number(keySplit[0]);
348
- await this.addTasks(queueName + (attempt + 1 >= this.retryCount ? ":dlq" : ""), filteredTasks.map((task) => ({ ...task[1] })), {
349
- createdAt: Number(keySplit[1]),
350
- job: keySplit[2],
351
- attempt: attempt + 1
352
- });
348
+ const job = String(keySplit[2]);
349
+ if (!(attempt >= this.retryCount - 1)) {
350
+ await this.addTasks(queueName, filteredTasks, {
351
+ job,
352
+ attempt: attempt + 1
353
+ });
354
+ } else if (this.logStatus) {
355
+ const statusKey = `${queueName}:${job}:`;
356
+ await this.setOne(statusKey + "err", Number(await this.getOne(statusKey + "err") || 0) + filteredTasks.length, this.logStatusTimeout);
357
+ await this.setOne(statusKey + "ready", Number(await this.getOne(statusKey + "ready") || 0) + filteredTasks.length, this.logStatusTimeout);
358
+ await this.addTasks(queueName + ":dlq", filteredTasks, {
359
+ job
360
+ });
361
+ }
353
362
  }
354
363
  } catch (err2) {
355
364
  throw new Error(`Batch error. ${err2.message}`);
@@ -452,14 +461,14 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
452
461
  let contended = 0, promises = [];
453
462
  for (const [id, payload, createdAt, job, idemKey, attempt] of tasks) {
454
463
  if (!this.executeSync) {
455
- promises.push((async () => {
464
+ promises.push(async () => {
456
465
  const r = await this.executeProcess(queueName, { id, payload, createdAt, job, idemKey, attempt });
457
466
  if (r.id) {
458
467
  result.push(r);
459
468
  } else if (r.contended) {
460
469
  contended++;
461
470
  }
462
- })());
471
+ });
463
472
  } else {
464
473
  const r = await this.executeProcess(queueName, { id, payload, createdAt, job, idemKey, attempt });
465
474
  if (r.id) {
@@ -469,10 +478,11 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
469
478
  }
470
479
  }
471
480
  }
481
+ let start = Date.now();
472
482
  if (!this.executeSync && promises.length > 0) {
473
- await Promise.all(promises);
483
+ await Promise.all(promises.map((item) => item()));
474
484
  }
475
- await this.onBatchSuccess(queueName, result);
485
+ await this.onBatchReady(queueName, result);
476
486
  if (!(0, import_full_utils.isArrFilled)(result) && contended > tasks.length >> 1) {
477
487
  await this.waitAbortable(15 + Math.floor(Math.random() * 35) + Math.min(250, 15 * contended + Math.floor(Math.random() * 40)));
478
488
  }
@@ -482,7 +492,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
482
492
  const keys = this.idempotencyKeys(queueName, String(task.idemKey));
483
493
  const allow = await this.idempotencyAllow(keys);
484
494
  if (allow === 1) {
485
- return { id: task.id };
495
+ return { contended: true };
486
496
  } else if (allow === 0) {
487
497
  let ttl = -2;
488
498
  try {
@@ -503,13 +513,8 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
503
513
  return await this.success(queueName, processed);
504
514
  } catch (err) {
505
515
  try {
506
- task.attempt = task.attempt + 1;
507
- await this.error(err, queueName, task);
508
- if (task.attempt >= this.retryCount) {
509
- await this.idempotencyFree(keys);
510
- return task;
511
- }
512
516
  await this.idempotencyFree(keys);
517
+ return await this.error(err, queueName, task);
513
518
  } catch (err2) {
514
519
  }
515
520
  } finally {
@@ -554,28 +559,26 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
554
559
  return await this.onSuccess(queueName, task);
555
560
  }
556
561
  async error(err, queueName, task) {
557
- const dlqKey = queueName + ":dlq";
558
562
  const taskP = { ...task };
559
- if (task.attempt >= this.retryCount) {
560
- const statusKey = `${queueName}:${task.job}:`;
561
- await this.addTasks(dlqKey, [{ ...taskP.payload }], {
562
- createdAt: task.createdAt,
563
- job: task.job,
564
- attempt: task.attempt
565
- });
566
- if (this.logStatus) {
567
- await this.incr(statusKey + "err", this.logStatusTimeout);
568
- await this.incr(statusKey + "ready", this.logStatusTimeout);
569
- }
570
- } else {
571
- await this.onRetry(err, queueName, task);
563
+ if (!(taskP.attempt >= this.retryCount - 1)) {
564
+ await this.onRetry(err, queueName, taskP);
572
565
  await this.addTasks(queueName, [{ ...taskP.payload }], {
573
- createdAt: task.createdAt,
574
- job: task.job,
575
- attempt: task.attempt
566
+ createdAt: taskP.createdAt,
567
+ job: taskP.job,
568
+ attempt: (taskP.attempt || 0) + 1
569
+ });
570
+ } else if (this.logStatus) {
571
+ const dlqKey = queueName + ":dlq";
572
+ const statusKey = `${queueName}:${taskP.job}:`;
573
+ await this.incr(statusKey + "err", this.logStatusTimeout);
574
+ await this.incr(statusKey + "ready", this.logStatusTimeout);
575
+ await this.addTasks(dlqKey, [{ ...taskP.payload }], {
576
+ createdAt: taskP.createdAt,
577
+ job: taskP.job,
578
+ attempt: taskP.attempt
576
579
  });
577
580
  }
578
- await this.onError(err, queueName, task);
581
+ return await this.onError(err, queueName, { ...taskP, attempt: (taskP.attempt || 0) + 1 });
579
582
  }
580
583
  async waitAbortable(ttl) {
581
584
  return new Promise((resolve) => {
@@ -585,7 +588,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
585
588
  }
586
589
  let delay;
587
590
  if (ttl > 0) {
588
- const base = Math.max(25, Math.min(ttl, 5e3));
591
+ const base = Math.max(25, Math.min(ttl, 4e3));
589
592
  const jitter = Math.floor(Math.min(base, 200) * Math.random());
590
593
  delay = base + jitter;
591
594
  } else {
@@ -837,12 +840,13 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
837
840
  async onExecute(queueName, task) {
838
841
  return task;
839
842
  }
840
- async onBatchSuccess(queueName, tasks) {
843
+ async onBatchReady(queueName, tasks) {
841
844
  }
842
845
  async onSuccess(queueName, task) {
843
846
  return task;
844
847
  }
845
848
  async onError(err, queueName, task) {
849
+ return task;
846
850
  }
847
851
  async onBatchError(err, queueName, tasks) {
848
852
  }
package/dist/index.d.cts CHANGED
@@ -81,12 +81,12 @@ declare class PowerQueues extends PowerRedis {
81
81
  private buildBatches;
82
82
  private keysLength;
83
83
  private payloadBatch;
84
- beforeExecute(queueName: string, tasks: Array<[string, any[], number, string, string, number]>): Promise<[string, any[], number, string, string, number][]>;
84
+ beforeExecute(queueName: string, tasks: Array<[string, any, number, string, string, number]>): Promise<[string, any, number, string, string, number][]>;
85
85
  onExecute(queueName: string, task: Task): Promise<Task>;
86
- onBatchSuccess(queueName: string, tasks: Array<Task>): Promise<void>;
86
+ onBatchReady(queueName: string, tasks: Task[]): Promise<void>;
87
87
  onSuccess(queueName: string, task: Task): Promise<Task>;
88
- onError(err: any, queueName: string, task: Task): Promise<void>;
89
- onBatchError(err: any, queueName: string, tasks: Array<[string, any[], number, string, string, number]>): Promise<void>;
88
+ onError(err: any, queueName: string, task: Task): Promise<Task>;
89
+ onBatchError(err: any, queueName: string, tasks: Array<[string, any, number, string, string, number]>): Promise<void>;
90
90
  onRetry(err: any, queueName: string, task: Task): Promise<void>;
91
91
  }
92
92
 
package/dist/index.d.ts CHANGED
@@ -81,12 +81,12 @@ declare class PowerQueues extends PowerRedis {
81
81
  private buildBatches;
82
82
  private keysLength;
83
83
  private payloadBatch;
84
- beforeExecute(queueName: string, tasks: Array<[string, any[], number, string, string, number]>): Promise<[string, any[], number, string, string, number][]>;
84
+ beforeExecute(queueName: string, tasks: Array<[string, any, number, string, string, number]>): Promise<[string, any, number, string, string, number][]>;
85
85
  onExecute(queueName: string, task: Task): Promise<Task>;
86
- onBatchSuccess(queueName: string, tasks: Array<Task>): Promise<void>;
86
+ onBatchReady(queueName: string, tasks: Task[]): Promise<void>;
87
87
  onSuccess(queueName: string, task: Task): Promise<Task>;
88
- onError(err: any, queueName: string, task: Task): Promise<void>;
89
- onBatchError(err: any, queueName: string, tasks: Array<[string, any[], number, string, string, number]>): Promise<void>;
88
+ onError(err: any, queueName: string, task: Task): Promise<Task>;
89
+ onBatchError(err: any, queueName: string, tasks: Array<[string, any, number, string, string, number]>): Promise<void>;
90
90
  onRetry(err: any, queueName: string, task: Task): Promise<void>;
91
91
  }
92
92
 
package/dist/index.js CHANGED
@@ -258,7 +258,7 @@ var PowerQueues = class extends PowerRedis {
258
258
  this.selectStuckTimeout = 6e4;
259
259
  this.selectStuckMaxTimeout = 80;
260
260
  this.selectCount = 200;
261
- this.selectTimeout = 5e3;
261
+ this.selectTimeout = 3e3;
262
262
  this.buildBatchCount = 800;
263
263
  this.buildBatchMaxCount = 1e4;
264
264
  this.retryCount = 1;
@@ -266,7 +266,7 @@ var PowerQueues = class extends PowerRedis {
266
266
  this.idemLockTimeout = 18e4;
267
267
  this.idemDoneTimeout = 6e4;
268
268
  this.logStatus = false;
269
- this.logStatusTimeout = 3e5;
269
+ this.logStatusTimeout = 18e5;
270
270
  this.approveCount = 2e3;
271
271
  this.removeOnExecuted = true;
272
272
  }
@@ -299,7 +299,7 @@ var PowerQueues = class extends PowerRedis {
299
299
  } catch (err) {
300
300
  }
301
301
  if (!isArrFilled(tasks)) {
302
- await wait(400);
302
+ await wait(300);
303
303
  continue;
304
304
  }
305
305
  try {
@@ -310,7 +310,7 @@ var PowerQueues = class extends PowerRedis {
310
310
  await this.approve(queueName, tasks.map((task) => task[0]));
311
311
  } catch {
312
312
  }
313
- await wait(400);
313
+ await wait(300);
314
314
  }
315
315
  }
316
316
  }
@@ -322,17 +322,26 @@ var PowerQueues = class extends PowerRedis {
322
322
  if (!filtered[key]) {
323
323
  filtered[key] = [];
324
324
  }
325
- filtered[key].push(tasks[index]);
325
+ filtered[key].push(tasks[index][1]);
326
326
  });
327
327
  for (let key in filtered) {
328
328
  const filteredTasks = filtered[key];
329
329
  const keySplit = key.split(":");
330
330
  const attempt = Number(keySplit[0]);
331
- await this.addTasks(queueName + (attempt + 1 >= this.retryCount ? ":dlq" : ""), filteredTasks.map((task) => ({ ...task[1] })), {
332
- createdAt: Number(keySplit[1]),
333
- job: keySplit[2],
334
- attempt: attempt + 1
335
- });
331
+ const job = String(keySplit[2]);
332
+ if (!(attempt >= this.retryCount - 1)) {
333
+ await this.addTasks(queueName, filteredTasks, {
334
+ job,
335
+ attempt: attempt + 1
336
+ });
337
+ } else if (this.logStatus) {
338
+ const statusKey = `${queueName}:${job}:`;
339
+ await this.setOne(statusKey + "err", Number(await this.getOne(statusKey + "err") || 0) + filteredTasks.length, this.logStatusTimeout);
340
+ await this.setOne(statusKey + "ready", Number(await this.getOne(statusKey + "ready") || 0) + filteredTasks.length, this.logStatusTimeout);
341
+ await this.addTasks(queueName + ":dlq", filteredTasks, {
342
+ job
343
+ });
344
+ }
336
345
  }
337
346
  } catch (err2) {
338
347
  throw new Error(`Batch error. ${err2.message}`);
@@ -435,14 +444,14 @@ var PowerQueues = class extends PowerRedis {
435
444
  let contended = 0, promises = [];
436
445
  for (const [id, payload, createdAt, job, idemKey, attempt] of tasks) {
437
446
  if (!this.executeSync) {
438
- promises.push((async () => {
447
+ promises.push(async () => {
439
448
  const r = await this.executeProcess(queueName, { id, payload, createdAt, job, idemKey, attempt });
440
449
  if (r.id) {
441
450
  result.push(r);
442
451
  } else if (r.contended) {
443
452
  contended++;
444
453
  }
445
- })());
454
+ });
446
455
  } else {
447
456
  const r = await this.executeProcess(queueName, { id, payload, createdAt, job, idemKey, attempt });
448
457
  if (r.id) {
@@ -452,10 +461,11 @@ var PowerQueues = class extends PowerRedis {
452
461
  }
453
462
  }
454
463
  }
464
+ let start = Date.now();
455
465
  if (!this.executeSync && promises.length > 0) {
456
- await Promise.all(promises);
466
+ await Promise.all(promises.map((item) => item()));
457
467
  }
458
- await this.onBatchSuccess(queueName, result);
468
+ await this.onBatchReady(queueName, result);
459
469
  if (!isArrFilled(result) && contended > tasks.length >> 1) {
460
470
  await this.waitAbortable(15 + Math.floor(Math.random() * 35) + Math.min(250, 15 * contended + Math.floor(Math.random() * 40)));
461
471
  }
@@ -465,7 +475,7 @@ var PowerQueues = class extends PowerRedis {
465
475
  const keys = this.idempotencyKeys(queueName, String(task.idemKey));
466
476
  const allow = await this.idempotencyAllow(keys);
467
477
  if (allow === 1) {
468
- return { id: task.id };
478
+ return { contended: true };
469
479
  } else if (allow === 0) {
470
480
  let ttl = -2;
471
481
  try {
@@ -486,13 +496,8 @@ var PowerQueues = class extends PowerRedis {
486
496
  return await this.success(queueName, processed);
487
497
  } catch (err) {
488
498
  try {
489
- task.attempt = task.attempt + 1;
490
- await this.error(err, queueName, task);
491
- if (task.attempt >= this.retryCount) {
492
- await this.idempotencyFree(keys);
493
- return task;
494
- }
495
499
  await this.idempotencyFree(keys);
500
+ return await this.error(err, queueName, task);
496
501
  } catch (err2) {
497
502
  }
498
503
  } finally {
@@ -537,28 +542,26 @@ var PowerQueues = class extends PowerRedis {
537
542
  return await this.onSuccess(queueName, task);
538
543
  }
539
544
  async error(err, queueName, task) {
540
- const dlqKey = queueName + ":dlq";
541
545
  const taskP = { ...task };
542
- if (task.attempt >= this.retryCount) {
543
- const statusKey = `${queueName}:${task.job}:`;
544
- await this.addTasks(dlqKey, [{ ...taskP.payload }], {
545
- createdAt: task.createdAt,
546
- job: task.job,
547
- attempt: task.attempt
548
- });
549
- if (this.logStatus) {
550
- await this.incr(statusKey + "err", this.logStatusTimeout);
551
- await this.incr(statusKey + "ready", this.logStatusTimeout);
552
- }
553
- } else {
554
- await this.onRetry(err, queueName, task);
546
+ if (!(taskP.attempt >= this.retryCount - 1)) {
547
+ await this.onRetry(err, queueName, taskP);
555
548
  await this.addTasks(queueName, [{ ...taskP.payload }], {
556
- createdAt: task.createdAt,
557
- job: task.job,
558
- attempt: task.attempt
549
+ createdAt: taskP.createdAt,
550
+ job: taskP.job,
551
+ attempt: (taskP.attempt || 0) + 1
552
+ });
553
+ } else if (this.logStatus) {
554
+ const dlqKey = queueName + ":dlq";
555
+ const statusKey = `${queueName}:${taskP.job}:`;
556
+ await this.incr(statusKey + "err", this.logStatusTimeout);
557
+ await this.incr(statusKey + "ready", this.logStatusTimeout);
558
+ await this.addTasks(dlqKey, [{ ...taskP.payload }], {
559
+ createdAt: taskP.createdAt,
560
+ job: taskP.job,
561
+ attempt: taskP.attempt
559
562
  });
560
563
  }
561
- await this.onError(err, queueName, task);
564
+ return await this.onError(err, queueName, { ...taskP, attempt: (taskP.attempt || 0) + 1 });
562
565
  }
563
566
  async waitAbortable(ttl) {
564
567
  return new Promise((resolve) => {
@@ -568,7 +571,7 @@ var PowerQueues = class extends PowerRedis {
568
571
  }
569
572
  let delay;
570
573
  if (ttl > 0) {
571
- const base = Math.max(25, Math.min(ttl, 5e3));
574
+ const base = Math.max(25, Math.min(ttl, 4e3));
572
575
  const jitter = Math.floor(Math.min(base, 200) * Math.random());
573
576
  delay = base + jitter;
574
577
  } else {
@@ -820,12 +823,13 @@ var PowerQueues = class extends PowerRedis {
820
823
  async onExecute(queueName, task) {
821
824
  return task;
822
825
  }
823
- async onBatchSuccess(queueName, tasks) {
826
+ async onBatchReady(queueName, tasks) {
824
827
  }
825
828
  async onSuccess(queueName, task) {
826
829
  return task;
827
830
  }
828
831
  async onError(err, queueName, task) {
832
+ return task;
829
833
  }
830
834
  async onBatchError(err, queueName, tasks) {
831
835
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "power-queues",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "High-performance Redis Streams queue for Node.js with Lua-powered bulk XADD, idempotent workers, heartbeat locks, stuck-task recovery, retries, DLQ, and distributed processing.",
5
5
  "author": "ihor-bielchenko",
6
6
  "license": "MIT",