@trigger.dev/redis-worker 0.0.0-prerelease-20250329172200 → 0.0.0-prerelease-20250430152137

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
@@ -9335,7 +9335,7 @@ var defaultOptions = {
9335
9335
  const delay = Math.min(times * 50, 1e3);
9336
9336
  return delay;
9337
9337
  },
9338
- maxRetriesPerRequest: process.env.GITHUB_ACTIONS ? 50 : process.env.VITEST ? 1 : 20
9338
+ maxRetriesPerRequest: process.env.GITHUB_ACTIONS ? 50 : process.env.VITEST ? 5 : 20
9339
9339
  };
9340
9340
  var logger = new logger$1.Logger("Redis", "debug");
9341
9341
  function createRedisClient(options, handlers) {
@@ -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`,
@@ -9496,15 +9498,14 @@ var SimpleQueue = class {
9496
9498
  continue;
9497
9499
  }
9498
9500
  const visibilityTimeoutMs = parsedItem.visibilityTimeoutMs;
9499
- const invisibleUntil = now + visibilityTimeoutMs;
9500
- await this.redis.zadd(`queue`, invisibleUntil, id);
9501
9501
  dequeuedItems.push({
9502
9502
  id,
9503
9503
  job: parsedItem.job,
9504
9504
  item: validatedItem.data,
9505
9505
  visibilityTimeoutMs,
9506
9506
  attempt: parsedItem.attempt ?? 0,
9507
- timestamp
9507
+ timestamp,
9508
+ deduplicationKey: parsedItem.deduplicationKey
9508
9509
  });
9509
9510
  }
9510
9511
  return dequeuedItems;
@@ -9517,14 +9518,26 @@ var SimpleQueue = class {
9517
9518
  throw e;
9518
9519
  }
9519
9520
  }
9520
- async ack(id) {
9521
+ async ack(id, deduplicationKey) {
9521
9522
  try {
9522
- await this.redis.ackItem(`queue`, `items`, id);
9523
+ const result = await this.redis.ackItem(`queue`, `items`, id, deduplicationKey ?? "");
9524
+ if (result !== 1) {
9525
+ this.logger.debug(
9526
+ `SimpleQueue ${this.name}.ack(): ack operation returned ${result}. This means it was not removed from the queue.`,
9527
+ {
9528
+ queue: this.name,
9529
+ id,
9530
+ deduplicationKey,
9531
+ result
9532
+ }
9533
+ );
9534
+ }
9523
9535
  } catch (e) {
9524
9536
  this.logger.error(`SimpleQueue ${this.name}.ack(): error acknowledging item`, {
9525
9537
  queue: this.name,
9526
9538
  error: e,
9527
- id
9539
+ id,
9540
+ deduplicationKey
9528
9541
  });
9529
9542
  throw e;
9530
9543
  }
@@ -9662,6 +9675,9 @@ var SimpleQueue = class {
9662
9675
 
9663
9676
  redis.call('ZADD', queue, invisibleUntil, id)
9664
9677
  table.insert(dequeued, {id, serializedItem, score})
9678
+ else
9679
+ -- Remove the orphaned queue entry if no corresponding item exists
9680
+ redis.call('ZREM', queue, id)
9665
9681
  end
9666
9682
  end
9667
9683
 
@@ -9671,15 +9687,32 @@ var SimpleQueue = class {
9671
9687
  this.redis.defineCommand("ackItem", {
9672
9688
  numberOfKeys: 2,
9673
9689
  lua: `
9674
- local queue = KEYS[1]
9675
- local items = KEYS[2]
9690
+ local queueKey = KEYS[1]
9691
+ local itemsKey = KEYS[2]
9676
9692
  local id = ARGV[1]
9693
+ local deduplicationKey = ARGV[2]
9677
9694
 
9678
- redis.call('ZREM', queue, id)
9679
- redis.call('HDEL', items, id)
9695
+ -- Get the item from the hash
9696
+ local item = redis.call('HGET', itemsKey, id)
9697
+ if not item then
9698
+ return -1
9699
+ end
9680
9700
 
9701
+ -- Only check deduplicationKey if a non-empty one was passed in
9702
+ if deduplicationKey and deduplicationKey ~= "" then
9703
+ local success, parsed = pcall(cjson.decode, item)
9704
+ if success then
9705
+ if parsed.deduplicationKey and parsed.deduplicationKey ~= deduplicationKey then
9706
+ return 0
9707
+ end
9708
+ end
9709
+ end
9710
+
9711
+ -- Remove from sorted set and hash
9712
+ redis.call('ZREM', queueKey, id)
9713
+ redis.call('HDEL', itemsKey, id)
9681
9714
  return 1
9682
- `
9715
+ `
9683
9716
  });
9684
9717
  this.redis.defineCommand("moveToDeadLetterQueue", {
9685
9718
  numberOfKeys: 4,
@@ -10894,7 +10927,7 @@ var Worker = class _Worker {
10894
10927
  /**
10895
10928
  * Processes a single item.
10896
10929
  */
10897
- async processItem({ id, job, item, visibilityTimeoutMs, attempt, timestamp }, batchSize, workerId) {
10930
+ async processItem({ id, job, item, visibilityTimeoutMs, attempt, timestamp, deduplicationKey }, batchSize, workerId) {
10898
10931
  const catalogItem = this.options.catalog[job];
10899
10932
  const handler = this.jobs[job];
10900
10933
  if (!handler) {
@@ -10907,7 +10940,7 @@ var Worker = class _Worker {
10907
10940
  async () => {
10908
10941
  await this.withHistogram(
10909
10942
  this.metrics.jobDuration,
10910
- handler({ id, payload: item, visibilityTimeoutMs, attempt }),
10943
+ handler({ id, payload: item, visibilityTimeoutMs, attempt, deduplicationKey }),
10911
10944
  {
10912
10945
  worker_id: workerId,
10913
10946
  batch_size: batchSize,
@@ -10915,7 +10948,7 @@ var Worker = class _Worker {
10915
10948
  attempt
10916
10949
  }
10917
10950
  );
10918
- await this.queue.ack(id);
10951
+ await this.queue.ack(id, deduplicationKey);
10919
10952
  },
10920
10953
  {
10921
10954
  kind: SpanKind.CONSUMER,