@trigger.dev/redis-worker 0.0.0-process-keep-alive-20250618184624 → 0.0.0-process-keep-alive-20250623153513

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
@@ -9645,6 +9645,23 @@ var SimpleQueue = class {
9645
9645
  throw e;
9646
9646
  }
9647
9647
  }
9648
+ async getJob(id) {
9649
+ const result = await this.redis.getJob(`queue`, `items`, id);
9650
+ if (!result) {
9651
+ return null;
9652
+ }
9653
+ const [_, score, serializedItem] = result;
9654
+ const item = JSON.parse(serializedItem);
9655
+ return {
9656
+ id,
9657
+ job: item.job,
9658
+ item: item.item,
9659
+ visibilityTimeoutMs: item.visibilityTimeoutMs,
9660
+ attempt: item.attempt ?? 0,
9661
+ timestamp: new Date(Number(score)),
9662
+ deduplicationKey: item.deduplicationKey ?? void 0
9663
+ };
9664
+ }
9648
9665
  async moveToDeadLetterQueue(id, errorMessage) {
9649
9666
  try {
9650
9667
  const result = await this.redis.moveToDeadLetterQueue(
@@ -9767,6 +9784,25 @@ var SimpleQueue = class {
9767
9784
  return dequeued
9768
9785
  `
9769
9786
  });
9787
+ this.redis.defineCommand("getJob", {
9788
+ numberOfKeys: 2,
9789
+ lua: `
9790
+ local queue = KEYS[1]
9791
+ local items = KEYS[2]
9792
+ local jobId = ARGV[1]
9793
+
9794
+ local serializedItem = redis.call('HGET', items, jobId)
9795
+
9796
+ if serializedItem == false then
9797
+ return nil
9798
+ end
9799
+
9800
+ -- get the score from the queue sorted set
9801
+ local score = redis.call('ZSCORE', queue, jobId)
9802
+
9803
+ return { jobId, score, serializedItem }
9804
+ `
9805
+ });
9770
9806
  this.redis.defineCommand("ackItem", {
9771
9807
  numberOfKeys: 2,
9772
9808
  lua: `
@@ -11115,7 +11151,7 @@ var Worker = class _Worker {
11115
11151
  start() {
11116
11152
  const { workers, tasksPerWorker } = this.concurrency;
11117
11153
  for (let i = 0; i < workers; i++) {
11118
- this.workerLoops.push(this.runWorkerLoop(`worker-${nanoid(12)}`, tasksPerWorker));
11154
+ this.workerLoops.push(this.runWorkerLoop(`worker-${nanoid(12)}`, tasksPerWorker, i, workers));
11119
11155
  }
11120
11156
  this.setupShutdownHandlers();
11121
11157
  this.subscriber = createRedisClient(this.options.redisOptions, {
@@ -11265,15 +11301,37 @@ var Worker = class _Worker {
11265
11301
  }
11266
11302
  );
11267
11303
  }
11304
+ async getJob(id) {
11305
+ return this.queue.getJob(id);
11306
+ }
11268
11307
  /**
11269
11308
  * The main loop that each worker runs. It repeatedly polls for items,
11270
11309
  * processes them, and then waits before the next iteration.
11271
11310
  */
11272
- async runWorkerLoop(workerId, taskCount) {
11311
+ async runWorkerLoop(workerId, taskCount, workerIndex, totalWorkers) {
11273
11312
  const pollIntervalMs = this.options.pollIntervalMs ?? 1e3;
11274
11313
  const immediatePollIntervalMs = this.options.immediatePollIntervalMs ?? 100;
11314
+ const delayBetweenWorkers = this.options.pollIntervalMs ?? 1e3;
11315
+ const delay = delayBetweenWorkers * (totalWorkers - workerIndex);
11316
+ await _Worker.delay(delay);
11317
+ this.logger.info("Starting worker loop", {
11318
+ workerIndex,
11319
+ totalWorkers,
11320
+ delay,
11321
+ workerId,
11322
+ taskCount,
11323
+ pollIntervalMs,
11324
+ immediatePollIntervalMs,
11325
+ concurrencyOptions: this.concurrency
11326
+ });
11275
11327
  while (!this.isShuttingDown) {
11276
11328
  if (this.limiter.activeCount + this.limiter.pendingCount >= this.concurrency.limit) {
11329
+ this.logger.debug("Worker at capacity, waiting", {
11330
+ workerId,
11331
+ concurrencyOptions: this.concurrency,
11332
+ activeCount: this.limiter.activeCount,
11333
+ pendingCount: this.limiter.pendingCount
11334
+ });
11277
11335
  await _Worker.delay(pollIntervalMs);
11278
11336
  continue;
11279
11337
  }
@@ -11287,9 +11345,22 @@ var Worker = class _Worker {
11287
11345
  }
11288
11346
  );
11289
11347
  if (items.length === 0) {
11348
+ this.logger.debug("No items to dequeue", {
11349
+ workerId,
11350
+ concurrencyOptions: this.concurrency,
11351
+ activeCount: this.limiter.activeCount,
11352
+ pendingCount: this.limiter.pendingCount
11353
+ });
11290
11354
  await _Worker.delay(pollIntervalMs);
11291
11355
  continue;
11292
11356
  }
11357
+ this.logger.info("Dequeued items", {
11358
+ workerId,
11359
+ itemCount: items.length,
11360
+ concurrencyOptions: this.concurrency,
11361
+ activeCount: this.limiter.activeCount,
11362
+ pendingCount: this.limiter.pendingCount
11363
+ });
11293
11364
  for (const item of items) {
11294
11365
  this.limiter(() => this.processItem(item, items.length, workerId)).catch(
11295
11366
  (err) => {
@@ -11304,6 +11375,7 @@ var Worker = class _Worker {
11304
11375
  }
11305
11376
  await _Worker.delay(immediatePollIntervalMs);
11306
11377
  }
11378
+ this.logger.info("Worker loop finished", { workerId });
11307
11379
  }
11308
11380
  /**
11309
11381
  * Processes a single item.