@trigger.dev/redis-worker 4.0.0-v4-beta.5 → 4.0.0-v4-beta.7

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
@@ -9431,11 +9431,13 @@ var SimpleQueue = class {
9431
9431
  }) {
9432
9432
  try {
9433
9433
  const score = availableAt ? availableAt.getTime() : Date.now();
9434
+ const deduplicationKey = nanoid();
9434
9435
  const serializedItem = JSON.stringify({
9435
9436
  job,
9436
9437
  item,
9437
9438
  visibilityTimeoutMs,
9438
- attempt
9439
+ attempt,
9440
+ deduplicationKey
9439
9441
  });
9440
9442
  const result = await this.redis.enqueueItem(
9441
9443
  `queue`,
@@ -9504,7 +9506,8 @@ var SimpleQueue = class {
9504
9506
  item: validatedItem.data,
9505
9507
  visibilityTimeoutMs,
9506
9508
  attempt: parsedItem.attempt ?? 0,
9507
- timestamp
9509
+ timestamp,
9510
+ deduplicationKey: parsedItem.deduplicationKey
9508
9511
  });
9509
9512
  }
9510
9513
  return dequeuedItems;
@@ -9517,14 +9520,26 @@ var SimpleQueue = class {
9517
9520
  throw e;
9518
9521
  }
9519
9522
  }
9520
- async ack(id) {
9523
+ async ack(id, deduplicationKey) {
9521
9524
  try {
9522
- await this.redis.ackItem(`queue`, `items`, id);
9525
+ const result = await this.redis.ackItem(`queue`, `items`, id, deduplicationKey ?? "");
9526
+ if (result !== 1) {
9527
+ this.logger.debug(
9528
+ `SimpleQueue ${this.name}.ack(): ack operation returned ${result}. This means it was not removed from the queue.`,
9529
+ {
9530
+ queue: this.name,
9531
+ id,
9532
+ deduplicationKey,
9533
+ result
9534
+ }
9535
+ );
9536
+ }
9523
9537
  } catch (e) {
9524
9538
  this.logger.error(`SimpleQueue ${this.name}.ack(): error acknowledging item`, {
9525
9539
  queue: this.name,
9526
9540
  error: e,
9527
- id
9541
+ id,
9542
+ deduplicationKey
9528
9543
  });
9529
9544
  throw e;
9530
9545
  }
@@ -9671,15 +9686,32 @@ var SimpleQueue = class {
9671
9686
  this.redis.defineCommand("ackItem", {
9672
9687
  numberOfKeys: 2,
9673
9688
  lua: `
9674
- local queue = KEYS[1]
9675
- local items = KEYS[2]
9689
+ local queueKey = KEYS[1]
9690
+ local itemsKey = KEYS[2]
9676
9691
  local id = ARGV[1]
9692
+ local deduplicationKey = ARGV[2]
9677
9693
 
9678
- redis.call('ZREM', queue, id)
9679
- redis.call('HDEL', items, id)
9694
+ -- Get the item from the hash
9695
+ local item = redis.call('HGET', itemsKey, id)
9696
+ if not item then
9697
+ return -1
9698
+ end
9680
9699
 
9700
+ -- Only check deduplicationKey if a non-empty one was passed in
9701
+ if deduplicationKey and deduplicationKey ~= "" then
9702
+ local success, parsed = pcall(cjson.decode, item)
9703
+ if success then
9704
+ if parsed.deduplicationKey and parsed.deduplicationKey ~= deduplicationKey then
9705
+ return 0
9706
+ end
9707
+ end
9708
+ end
9709
+
9710
+ -- Remove from sorted set and hash
9711
+ redis.call('ZREM', queueKey, id)
9712
+ redis.call('HDEL', itemsKey, id)
9681
9713
  return 1
9682
- `
9714
+ `
9683
9715
  });
9684
9716
  this.redis.defineCommand("moveToDeadLetterQueue", {
9685
9717
  numberOfKeys: 4,
@@ -10894,7 +10926,7 @@ var Worker = class _Worker {
10894
10926
  /**
10895
10927
  * Processes a single item.
10896
10928
  */
10897
- async processItem({ id, job, item, visibilityTimeoutMs, attempt, timestamp }, batchSize, workerId) {
10929
+ async processItem({ id, job, item, visibilityTimeoutMs, attempt, timestamp, deduplicationKey }, batchSize, workerId) {
10898
10930
  const catalogItem = this.options.catalog[job];
10899
10931
  const handler = this.jobs[job];
10900
10932
  if (!handler) {
@@ -10907,7 +10939,7 @@ var Worker = class _Worker {
10907
10939
  async () => {
10908
10940
  await this.withHistogram(
10909
10941
  this.metrics.jobDuration,
10910
- handler({ id, payload: item, visibilityTimeoutMs, attempt }),
10942
+ handler({ id, payload: item, visibilityTimeoutMs, attempt, deduplicationKey }),
10911
10943
  {
10912
10944
  worker_id: workerId,
10913
10945
  batch_size: batchSize,
@@ -10915,7 +10947,7 @@ var Worker = class _Worker {
10915
10947
  attempt
10916
10948
  }
10917
10949
  );
10918
- await this.queue.ack(id);
10950
+ await this.queue.ack(id, deduplicationKey);
10919
10951
  },
10920
10952
  {
10921
10953
  kind: SpanKind.CONSUMER,