@trigger.dev/redis-worker 4.3.3 → 4.4.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/dist/index.cjs CHANGED
@@ -11807,6 +11807,24 @@ var ConcurrencyManager = class {
11807
11807
  }
11808
11808
  await pipeline.exec();
11809
11809
  }
11810
+ /**
11811
+ * Release concurrency slots for multiple messages in a single pipeline.
11812
+ * More efficient than calling release() multiple times.
11813
+ */
11814
+ async releaseBatch(messages) {
11815
+ if (messages.length === 0) {
11816
+ return;
11817
+ }
11818
+ const pipeline = this.redis.pipeline();
11819
+ for (const { queue, messageId } of messages) {
11820
+ for (const group of this.groups) {
11821
+ const groupId = group.extractGroupId(queue);
11822
+ const key = this.keys.concurrencyKey(group.name, groupId);
11823
+ pipeline.srem(key, messageId);
11824
+ }
11825
+ }
11826
+ await pipeline.exec();
11827
+ }
11810
11828
  /**
11811
11829
  * Get current concurrency for a specific group.
11812
11830
  */
@@ -12901,7 +12919,7 @@ var VisibilityManager = class {
12901
12919
  *
12902
12920
  * @param shardId - The shard to check
12903
12921
  * @param getQueueKeys - Function to get queue keys for a queue ID
12904
- * @returns Number of messages reclaimed
12922
+ * @returns Array of reclaimed message info for concurrency release
12905
12923
  */
12906
12924
  async reclaimTimedOut(shardId, getQueueKeys) {
12907
12925
  const inflightKey = this.keys.inflightKey(shardId);
@@ -12917,17 +12935,25 @@ var VisibilityManager = class {
12917
12935
  100
12918
12936
  // Process in batches
12919
12937
  );
12920
- let reclaimed = 0;
12938
+ const reclaimedMessages = [];
12921
12939
  for (let i = 0; i < timedOut.length; i += 2) {
12922
12940
  const member = timedOut[i];
12923
- const originalScore = timedOut[i + 1];
12924
- if (!member || !originalScore) {
12941
+ const _deadlineScore = timedOut[i + 1];
12942
+ if (!member || !_deadlineScore) {
12925
12943
  continue;
12926
12944
  }
12927
12945
  const { messageId, queueId } = this.#parseMember(member);
12928
12946
  const { queueKey, queueItemsKey, masterQueueKey } = getQueueKeys(queueId);
12929
12947
  try {
12930
- const score = parseFloat(originalScore) || now;
12948
+ const dataJson = await this.redis.hget(inflightDataKey, messageId);
12949
+ let storedMessage = null;
12950
+ if (dataJson) {
12951
+ try {
12952
+ storedMessage = JSON.parse(dataJson);
12953
+ } catch {
12954
+ }
12955
+ }
12956
+ const score = storedMessage?.timestamp ?? now;
12931
12957
  await this.redis.releaseMessage(
12932
12958
  inflightKey,
12933
12959
  inflightDataKey,
@@ -12939,11 +12965,29 @@ var VisibilityManager = class {
12939
12965
  score.toString(),
12940
12966
  queueId
12941
12967
  );
12942
- reclaimed++;
12968
+ if (storedMessage) {
12969
+ reclaimedMessages.push({
12970
+ messageId,
12971
+ queueId,
12972
+ tenantId: storedMessage.tenantId,
12973
+ metadata: storedMessage.metadata
12974
+ });
12975
+ } else {
12976
+ this.logger.error("Missing or corrupted message data during reclaim, using fallback", {
12977
+ messageId,
12978
+ queueId
12979
+ });
12980
+ reclaimedMessages.push({
12981
+ messageId,
12982
+ queueId,
12983
+ tenantId: this.keys.extractTenantId(queueId),
12984
+ metadata: {}
12985
+ });
12986
+ }
12943
12987
  this.logger.debug("Reclaimed timed-out message", {
12944
12988
  messageId,
12945
12989
  queueId,
12946
- originalScore
12990
+ deadline: _deadlineScore
12947
12991
  });
12948
12992
  } catch (error) {
12949
12993
  this.logger.error("Failed to reclaim message", {
@@ -12953,7 +12997,7 @@ var VisibilityManager = class {
12953
12997
  });
12954
12998
  }
12955
12999
  }
12956
- return reclaimed;
13000
+ return reclaimedMessages;
12957
13001
  }
12958
13002
  /**
12959
13003
  * Get all in-flight messages for a shard.
@@ -15356,12 +15400,31 @@ var FairQueue = class {
15356
15400
  async #reclaimTimedOutMessages() {
15357
15401
  let totalReclaimed = 0;
15358
15402
  for (let shardId = 0; shardId < this.shardCount; shardId++) {
15359
- const reclaimed = await this.visibilityManager.reclaimTimedOut(shardId, (queueId) => ({
15403
+ const reclaimedMessages = await this.visibilityManager.reclaimTimedOut(shardId, (queueId) => ({
15360
15404
  queueKey: this.keys.queueKey(queueId),
15361
15405
  queueItemsKey: this.keys.queueItemsKey(queueId),
15362
15406
  masterQueueKey: this.keys.masterQueueKey(this.masterQueue.getShardForQueue(queueId))
15363
15407
  }));
15364
- totalReclaimed += reclaimed;
15408
+ if (this.concurrencyManager && reclaimedMessages.length > 0) {
15409
+ try {
15410
+ await this.concurrencyManager.releaseBatch(
15411
+ reclaimedMessages.map((msg) => ({
15412
+ queue: {
15413
+ id: msg.queueId,
15414
+ tenantId: msg.tenantId,
15415
+ metadata: msg.metadata ?? {}
15416
+ },
15417
+ messageId: msg.messageId
15418
+ }))
15419
+ );
15420
+ } catch (error) {
15421
+ this.logger.error("Failed to release concurrency for reclaimed messages", {
15422
+ count: reclaimedMessages.length,
15423
+ error: error instanceof Error ? error.message : String(error)
15424
+ });
15425
+ }
15426
+ }
15427
+ totalReclaimed += reclaimedMessages.length;
15365
15428
  }
15366
15429
  if (totalReclaimed > 0) {
15367
15430
  this.logger.info("Reclaimed timed-out messages", { count: totalReclaimed });