power-queues 2.1.0 → 2.1.2
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 +17 -15
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +17 -15
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -285,7 +285,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
285
285
|
this.logStatus = false;
|
|
286
286
|
this.logStatusTimeout = 3e5;
|
|
287
287
|
this.approveCount = 2e3;
|
|
288
|
-
this.removeOnExecuted =
|
|
288
|
+
this.removeOnExecuted = true;
|
|
289
289
|
}
|
|
290
290
|
signal() {
|
|
291
291
|
return this.abort.signal;
|
|
@@ -360,15 +360,15 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
360
360
|
throw new Error(`Batch error. ${err2.message}`);
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
|
-
async approve(queueName,
|
|
364
|
-
if (!(0, import_full_utils.isArrFilled)(
|
|
363
|
+
async approve(queueName, tasks) {
|
|
364
|
+
if (!(0, import_full_utils.isArrFilled)(tasks)) {
|
|
365
365
|
return 0;
|
|
366
366
|
}
|
|
367
367
|
const approveCount = Math.max(500, Math.min(4e3, this.approveCount));
|
|
368
368
|
let total = 0, i = 0;
|
|
369
|
-
while (i <
|
|
370
|
-
const room = Math.min(approveCount,
|
|
371
|
-
const part =
|
|
369
|
+
while (i < tasks.length) {
|
|
370
|
+
const room = Math.min(approveCount, tasks.length - i);
|
|
371
|
+
const part = tasks.slice(i, i + room).map((item) => String(item.id || ""));
|
|
372
372
|
const approved = await this.runScript("Approve", [queueName], [this.group, this.removeOnExecuted ? "1" : "0", ...part], Approve);
|
|
373
373
|
total += Number(approved || 0);
|
|
374
374
|
i += room;
|
|
@@ -455,7 +455,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
455
455
|
promises.push((async () => {
|
|
456
456
|
const r = await this.executeProcess(queueName, { id, payload, createdAt, job, idemKey, attempt });
|
|
457
457
|
if (r.id) {
|
|
458
|
-
result.push(
|
|
458
|
+
result.push(r);
|
|
459
459
|
} else if (r.contended) {
|
|
460
460
|
contended++;
|
|
461
461
|
}
|
|
@@ -463,7 +463,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
463
463
|
} else {
|
|
464
464
|
const r = await this.executeProcess(queueName, { id, payload, createdAt, job, idemKey, attempt });
|
|
465
465
|
if (r.id) {
|
|
466
|
-
result.push(
|
|
466
|
+
result.push(r);
|
|
467
467
|
} else if (r.contended) {
|
|
468
468
|
contended++;
|
|
469
469
|
}
|
|
@@ -472,7 +472,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
472
472
|
if (!this.executeSync && promises.length > 0) {
|
|
473
473
|
await Promise.all(promises);
|
|
474
474
|
}
|
|
475
|
-
await this.onBatchSuccess(queueName,
|
|
475
|
+
await this.onBatchSuccess(queueName, result);
|
|
476
476
|
if (!(0, import_full_utils.isArrFilled)(result) && contended > tasks.length >> 1) {
|
|
477
477
|
await this.waitAbortable(15 + Math.floor(Math.random() * 35) + Math.min(250, 15 * contended + Math.floor(Math.random() * 40)));
|
|
478
478
|
}
|
|
@@ -498,17 +498,16 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
498
498
|
const heartbeat = this.heartbeat(keys) || (() => {
|
|
499
499
|
});
|
|
500
500
|
try {
|
|
501
|
-
await this.onExecute(queueName, task);
|
|
501
|
+
const processed = await this.onExecute(queueName, task);
|
|
502
502
|
await this.idempotencyDone(keys);
|
|
503
|
-
await this.success(queueName,
|
|
504
|
-
return { id: task.id };
|
|
503
|
+
return await this.success(queueName, processed);
|
|
505
504
|
} catch (err) {
|
|
506
505
|
try {
|
|
507
506
|
task.attempt = task.attempt + 1;
|
|
508
507
|
await this.error(err, queueName, task);
|
|
509
508
|
if (task.attempt >= this.retryCount) {
|
|
510
509
|
await this.idempotencyFree(keys);
|
|
511
|
-
return
|
|
510
|
+
return task;
|
|
512
511
|
}
|
|
513
512
|
await this.idempotencyFree(keys);
|
|
514
513
|
} catch (err2) {
|
|
@@ -552,7 +551,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
552
551
|
await this.incr(statusKey + "ok", this.logStatusTimeout);
|
|
553
552
|
await this.incr(statusKey + "ready", this.logStatusTimeout);
|
|
554
553
|
}
|
|
555
|
-
await this.onSuccess(queueName, task);
|
|
554
|
+
return await this.onSuccess(queueName, task);
|
|
556
555
|
}
|
|
557
556
|
async error(err, queueName, task) {
|
|
558
557
|
const dlqKey = queueName + ":dlq";
|
|
@@ -724,6 +723,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
724
723
|
if (!(0, import_full_utils.isStrFilled)(queueName)) {
|
|
725
724
|
throw new Error("Queue name is required.");
|
|
726
725
|
}
|
|
726
|
+
opts.job = opts.job ?? (0, import_uuid.v4)();
|
|
727
727
|
const batches = this.buildBatches(data, opts);
|
|
728
728
|
const result = new Array(data.length);
|
|
729
729
|
const promises = [];
|
|
@@ -750,7 +750,7 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
750
750
|
});
|
|
751
751
|
if (opts.status) {
|
|
752
752
|
await this.redis.set(`${queueName}:${opts.job}:total`, data.length);
|
|
753
|
-
await this.redis.pexpire(`${queueName}:${opts.job}:total`,
|
|
753
|
+
await this.redis.pexpire(`${queueName}:${opts.job}:total`, this.logStatusTimeout);
|
|
754
754
|
}
|
|
755
755
|
await Promise.all(runners);
|
|
756
756
|
return result;
|
|
@@ -835,10 +835,12 @@ var PowerQueues = class extends import_power_redis.PowerRedis {
|
|
|
835
835
|
return tasks;
|
|
836
836
|
}
|
|
837
837
|
async onExecute(queueName, task) {
|
|
838
|
+
return task;
|
|
838
839
|
}
|
|
839
840
|
async onBatchSuccess(queueName, tasks) {
|
|
840
841
|
}
|
|
841
842
|
async onSuccess(queueName, task) {
|
|
843
|
+
return task;
|
|
842
844
|
}
|
|
843
845
|
async onError(err, queueName, task) {
|
|
844
846
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -82,9 +82,9 @@ declare class PowerQueues extends PowerRedis {
|
|
|
82
82
|
private keysLength;
|
|
83
83
|
private payloadBatch;
|
|
84
84
|
beforeExecute(queueName: string, tasks: Array<[string, any[], number, string, string, number]>): Promise<[string, any[], number, string, string, number][]>;
|
|
85
|
-
onExecute(queueName: string, task: Task): Promise<
|
|
86
|
-
onBatchSuccess(queueName: string, tasks: Array<
|
|
87
|
-
onSuccess(queueName: string, task: Task): Promise<
|
|
85
|
+
onExecute(queueName: string, task: Task): Promise<Task>;
|
|
86
|
+
onBatchSuccess(queueName: string, tasks: Array<Task>): Promise<void>;
|
|
87
|
+
onSuccess(queueName: string, task: Task): Promise<Task>;
|
|
88
88
|
onError(err: any, queueName: string, task: Task): Promise<void>;
|
|
89
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>;
|
package/dist/index.d.ts
CHANGED
|
@@ -82,9 +82,9 @@ declare class PowerQueues extends PowerRedis {
|
|
|
82
82
|
private keysLength;
|
|
83
83
|
private payloadBatch;
|
|
84
84
|
beforeExecute(queueName: string, tasks: Array<[string, any[], number, string, string, number]>): Promise<[string, any[], number, string, string, number][]>;
|
|
85
|
-
onExecute(queueName: string, task: Task): Promise<
|
|
86
|
-
onBatchSuccess(queueName: string, tasks: Array<
|
|
87
|
-
onSuccess(queueName: string, task: Task): Promise<
|
|
85
|
+
onExecute(queueName: string, task: Task): Promise<Task>;
|
|
86
|
+
onBatchSuccess(queueName: string, tasks: Array<Task>): Promise<void>;
|
|
87
|
+
onSuccess(queueName: string, task: Task): Promise<Task>;
|
|
88
88
|
onError(err: any, queueName: string, task: Task): Promise<void>;
|
|
89
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>;
|
package/dist/index.js
CHANGED
|
@@ -268,7 +268,7 @@ var PowerQueues = class extends PowerRedis {
|
|
|
268
268
|
this.logStatus = false;
|
|
269
269
|
this.logStatusTimeout = 3e5;
|
|
270
270
|
this.approveCount = 2e3;
|
|
271
|
-
this.removeOnExecuted =
|
|
271
|
+
this.removeOnExecuted = true;
|
|
272
272
|
}
|
|
273
273
|
signal() {
|
|
274
274
|
return this.abort.signal;
|
|
@@ -343,15 +343,15 @@ var PowerQueues = class extends PowerRedis {
|
|
|
343
343
|
throw new Error(`Batch error. ${err2.message}`);
|
|
344
344
|
}
|
|
345
345
|
}
|
|
346
|
-
async approve(queueName,
|
|
347
|
-
if (!isArrFilled(
|
|
346
|
+
async approve(queueName, tasks) {
|
|
347
|
+
if (!isArrFilled(tasks)) {
|
|
348
348
|
return 0;
|
|
349
349
|
}
|
|
350
350
|
const approveCount = Math.max(500, Math.min(4e3, this.approveCount));
|
|
351
351
|
let total = 0, i = 0;
|
|
352
|
-
while (i <
|
|
353
|
-
const room = Math.min(approveCount,
|
|
354
|
-
const part =
|
|
352
|
+
while (i < tasks.length) {
|
|
353
|
+
const room = Math.min(approveCount, tasks.length - i);
|
|
354
|
+
const part = tasks.slice(i, i + room).map((item) => String(item.id || ""));
|
|
355
355
|
const approved = await this.runScript("Approve", [queueName], [this.group, this.removeOnExecuted ? "1" : "0", ...part], Approve);
|
|
356
356
|
total += Number(approved || 0);
|
|
357
357
|
i += room;
|
|
@@ -438,7 +438,7 @@ var PowerQueues = class extends PowerRedis {
|
|
|
438
438
|
promises.push((async () => {
|
|
439
439
|
const r = await this.executeProcess(queueName, { id, payload, createdAt, job, idemKey, attempt });
|
|
440
440
|
if (r.id) {
|
|
441
|
-
result.push(
|
|
441
|
+
result.push(r);
|
|
442
442
|
} else if (r.contended) {
|
|
443
443
|
contended++;
|
|
444
444
|
}
|
|
@@ -446,7 +446,7 @@ var PowerQueues = class extends PowerRedis {
|
|
|
446
446
|
} else {
|
|
447
447
|
const r = await this.executeProcess(queueName, { id, payload, createdAt, job, idemKey, attempt });
|
|
448
448
|
if (r.id) {
|
|
449
|
-
result.push(
|
|
449
|
+
result.push(r);
|
|
450
450
|
} else if (r.contended) {
|
|
451
451
|
contended++;
|
|
452
452
|
}
|
|
@@ -455,7 +455,7 @@ var PowerQueues = class extends PowerRedis {
|
|
|
455
455
|
if (!this.executeSync && promises.length > 0) {
|
|
456
456
|
await Promise.all(promises);
|
|
457
457
|
}
|
|
458
|
-
await this.onBatchSuccess(queueName,
|
|
458
|
+
await this.onBatchSuccess(queueName, result);
|
|
459
459
|
if (!isArrFilled(result) && contended > tasks.length >> 1) {
|
|
460
460
|
await this.waitAbortable(15 + Math.floor(Math.random() * 35) + Math.min(250, 15 * contended + Math.floor(Math.random() * 40)));
|
|
461
461
|
}
|
|
@@ -481,17 +481,16 @@ var PowerQueues = class extends PowerRedis {
|
|
|
481
481
|
const heartbeat = this.heartbeat(keys) || (() => {
|
|
482
482
|
});
|
|
483
483
|
try {
|
|
484
|
-
await this.onExecute(queueName, task);
|
|
484
|
+
const processed = await this.onExecute(queueName, task);
|
|
485
485
|
await this.idempotencyDone(keys);
|
|
486
|
-
await this.success(queueName,
|
|
487
|
-
return { id: task.id };
|
|
486
|
+
return await this.success(queueName, processed);
|
|
488
487
|
} catch (err) {
|
|
489
488
|
try {
|
|
490
489
|
task.attempt = task.attempt + 1;
|
|
491
490
|
await this.error(err, queueName, task);
|
|
492
491
|
if (task.attempt >= this.retryCount) {
|
|
493
492
|
await this.idempotencyFree(keys);
|
|
494
|
-
return
|
|
493
|
+
return task;
|
|
495
494
|
}
|
|
496
495
|
await this.idempotencyFree(keys);
|
|
497
496
|
} catch (err2) {
|
|
@@ -535,7 +534,7 @@ var PowerQueues = class extends PowerRedis {
|
|
|
535
534
|
await this.incr(statusKey + "ok", this.logStatusTimeout);
|
|
536
535
|
await this.incr(statusKey + "ready", this.logStatusTimeout);
|
|
537
536
|
}
|
|
538
|
-
await this.onSuccess(queueName, task);
|
|
537
|
+
return await this.onSuccess(queueName, task);
|
|
539
538
|
}
|
|
540
539
|
async error(err, queueName, task) {
|
|
541
540
|
const dlqKey = queueName + ":dlq";
|
|
@@ -707,6 +706,7 @@ var PowerQueues = class extends PowerRedis {
|
|
|
707
706
|
if (!isStrFilled(queueName)) {
|
|
708
707
|
throw new Error("Queue name is required.");
|
|
709
708
|
}
|
|
709
|
+
opts.job = opts.job ?? uuid();
|
|
710
710
|
const batches = this.buildBatches(data, opts);
|
|
711
711
|
const result = new Array(data.length);
|
|
712
712
|
const promises = [];
|
|
@@ -733,7 +733,7 @@ var PowerQueues = class extends PowerRedis {
|
|
|
733
733
|
});
|
|
734
734
|
if (opts.status) {
|
|
735
735
|
await this.redis.set(`${queueName}:${opts.job}:total`, data.length);
|
|
736
|
-
await this.redis.pexpire(`${queueName}:${opts.job}:total`,
|
|
736
|
+
await this.redis.pexpire(`${queueName}:${opts.job}:total`, this.logStatusTimeout);
|
|
737
737
|
}
|
|
738
738
|
await Promise.all(runners);
|
|
739
739
|
return result;
|
|
@@ -818,10 +818,12 @@ var PowerQueues = class extends PowerRedis {
|
|
|
818
818
|
return tasks;
|
|
819
819
|
}
|
|
820
820
|
async onExecute(queueName, task) {
|
|
821
|
+
return task;
|
|
821
822
|
}
|
|
822
823
|
async onBatchSuccess(queueName, tasks) {
|
|
823
824
|
}
|
|
824
825
|
async onSuccess(queueName, task) {
|
|
826
|
+
return task;
|
|
825
827
|
}
|
|
826
828
|
async onError(err, queueName, task) {
|
|
827
829
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "power-queues",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.2",
|
|
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",
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"power-redis"
|
|
82
82
|
],
|
|
83
83
|
"dependencies": {
|
|
84
|
-
"full-utils": "^3.0.
|
|
84
|
+
"full-utils": "^3.0.9",
|
|
85
85
|
"power-redis": "^2.0.23",
|
|
86
86
|
"uuid": "^13.0.0"
|
|
87
87
|
}
|