@trigger.dev/redis-worker 0.0.0-prerelease-20250703115146 → 0.0.0-prerelease-20250715205134
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 +37 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +37 -26
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -11084,7 +11084,6 @@ var Worker = class _Worker {
|
|
|
11084
11084
|
this.jobs = options.jobs;
|
|
11085
11085
|
const { workers = 1, tasksPerWorker = 1, limit = 10 } = options.concurrency ?? {};
|
|
11086
11086
|
this.concurrency = { workers, tasksPerWorker, limit };
|
|
11087
|
-
this.limiter = pLimit(this.concurrency.limit);
|
|
11088
11087
|
const masterQueueObservableGauge = this.meter.createObservableGauge("redis_worker.queue.size", {
|
|
11089
11088
|
description: "The number of items in the queue",
|
|
11090
11089
|
unit: "items",
|
|
@@ -11135,7 +11134,7 @@ var Worker = class _Worker {
|
|
|
11135
11134
|
concurrency;
|
|
11136
11135
|
shutdownTimeoutMs;
|
|
11137
11136
|
// The p-limit limiter to control overall concurrency.
|
|
11138
|
-
|
|
11137
|
+
limiters = {};
|
|
11139
11138
|
async #updateQueueSizeMetric(observableResult) {
|
|
11140
11139
|
const queueSize = await this.queue.size();
|
|
11141
11140
|
observableResult.observe(queueSize, {
|
|
@@ -11149,14 +11148,20 @@ var Worker = class _Worker {
|
|
|
11149
11148
|
});
|
|
11150
11149
|
}
|
|
11151
11150
|
async #updateConcurrencyLimitActiveMetric(observableResult) {
|
|
11152
|
-
|
|
11153
|
-
|
|
11154
|
-
|
|
11151
|
+
for (const [workerId, limiter] of Object.entries(this.limiters)) {
|
|
11152
|
+
observableResult.observe(limiter.activeCount, {
|
|
11153
|
+
worker_name: this.options.name,
|
|
11154
|
+
worker_id: workerId
|
|
11155
|
+
});
|
|
11156
|
+
}
|
|
11155
11157
|
}
|
|
11156
11158
|
async #updateConcurrencyLimitPendingMetric(observableResult) {
|
|
11157
|
-
|
|
11158
|
-
|
|
11159
|
-
|
|
11159
|
+
for (const [workerId, limiter] of Object.entries(this.limiters)) {
|
|
11160
|
+
observableResult.observe(limiter.pendingCount, {
|
|
11161
|
+
worker_name: this.options.name,
|
|
11162
|
+
worker_id: workerId
|
|
11163
|
+
});
|
|
11164
|
+
}
|
|
11160
11165
|
}
|
|
11161
11166
|
start() {
|
|
11162
11167
|
const { workers, tasksPerWorker } = this.concurrency;
|
|
@@ -11325,6 +11330,8 @@ var Worker = class _Worker {
|
|
|
11325
11330
|
* processes them, and then waits before the next iteration.
|
|
11326
11331
|
*/
|
|
11327
11332
|
async runWorkerLoop(workerId, taskCount, workerIndex, totalWorkers) {
|
|
11333
|
+
const limiter = pLimit(this.concurrency.limit);
|
|
11334
|
+
this.limiters[workerId] = limiter;
|
|
11328
11335
|
const pollIntervalMs = this.options.pollIntervalMs ?? 1e3;
|
|
11329
11336
|
const immediatePollIntervalMs = this.options.immediatePollIntervalMs ?? 100;
|
|
11330
11337
|
const delayBetweenWorkers = this.options.pollIntervalMs ?? 1e3;
|
|
@@ -11341,31 +11348,35 @@ var Worker = class _Worker {
|
|
|
11341
11348
|
concurrencyOptions: this.concurrency
|
|
11342
11349
|
});
|
|
11343
11350
|
while (!this.isShuttingDown) {
|
|
11344
|
-
if (
|
|
11351
|
+
if (limiter.activeCount + limiter.pendingCount >= this.concurrency.limit) {
|
|
11345
11352
|
this.logger.debug("Worker at capacity, waiting", {
|
|
11346
11353
|
workerId,
|
|
11347
11354
|
concurrencyOptions: this.concurrency,
|
|
11348
|
-
activeCount:
|
|
11349
|
-
pendingCount:
|
|
11355
|
+
activeCount: limiter.activeCount,
|
|
11356
|
+
pendingCount: limiter.pendingCount
|
|
11350
11357
|
});
|
|
11351
11358
|
await _Worker.delay(pollIntervalMs);
|
|
11352
11359
|
continue;
|
|
11353
11360
|
}
|
|
11361
|
+
const $taskCount = Math.min(
|
|
11362
|
+
taskCount,
|
|
11363
|
+
this.concurrency.limit - limiter.activeCount - limiter.pendingCount
|
|
11364
|
+
);
|
|
11354
11365
|
try {
|
|
11355
11366
|
const items = await this.withHistogram(
|
|
11356
11367
|
this.metrics.dequeueDuration,
|
|
11357
|
-
this.queue.dequeue(taskCount),
|
|
11368
|
+
this.queue.dequeue($taskCount),
|
|
11358
11369
|
{
|
|
11359
11370
|
worker_id: workerId,
|
|
11360
|
-
task_count: taskCount
|
|
11371
|
+
task_count: $taskCount
|
|
11361
11372
|
}
|
|
11362
11373
|
);
|
|
11363
11374
|
if (items.length === 0) {
|
|
11364
11375
|
this.logger.debug("No items to dequeue", {
|
|
11365
11376
|
workerId,
|
|
11366
11377
|
concurrencyOptions: this.concurrency,
|
|
11367
|
-
activeCount:
|
|
11368
|
-
pendingCount:
|
|
11378
|
+
activeCount: limiter.activeCount,
|
|
11379
|
+
pendingCount: limiter.pendingCount
|
|
11369
11380
|
});
|
|
11370
11381
|
await _Worker.delay(pollIntervalMs);
|
|
11371
11382
|
continue;
|
|
@@ -11374,15 +11385,15 @@ var Worker = class _Worker {
|
|
|
11374
11385
|
workerId,
|
|
11375
11386
|
itemCount: items.length,
|
|
11376
11387
|
concurrencyOptions: this.concurrency,
|
|
11377
|
-
activeCount:
|
|
11378
|
-
pendingCount:
|
|
11388
|
+
activeCount: limiter.activeCount,
|
|
11389
|
+
pendingCount: limiter.pendingCount
|
|
11379
11390
|
});
|
|
11380
11391
|
for (const item of items) {
|
|
11381
|
-
|
|
11382
|
-
(
|
|
11383
|
-
|
|
11384
|
-
}
|
|
11385
|
-
);
|
|
11392
|
+
limiter(
|
|
11393
|
+
() => this.processItem(item, items.length, workerId, limiter)
|
|
11394
|
+
).catch((err) => {
|
|
11395
|
+
this.logger.error("Unhandled error in processItem:", { error: err, workerId, item });
|
|
11396
|
+
});
|
|
11386
11397
|
}
|
|
11387
11398
|
} catch (error) {
|
|
11388
11399
|
this.logger.error("Error dequeuing items:", { name: this.options.name, error });
|
|
@@ -11396,7 +11407,7 @@ var Worker = class _Worker {
|
|
|
11396
11407
|
/**
|
|
11397
11408
|
* Processes a single item.
|
|
11398
11409
|
*/
|
|
11399
|
-
async processItem({ id, job, item, visibilityTimeoutMs, attempt, timestamp, deduplicationKey }, batchSize, workerId) {
|
|
11410
|
+
async processItem({ id, job, item, visibilityTimeoutMs, attempt, timestamp, deduplicationKey }, batchSize, workerId, limiter) {
|
|
11400
11411
|
const catalogItem = this.options.catalog[job];
|
|
11401
11412
|
const handler = this.jobs[job];
|
|
11402
11413
|
if (!handler) {
|
|
@@ -11435,9 +11446,9 @@ var Worker = class _Worker {
|
|
|
11435
11446
|
job_timestamp: timestamp.getTime(),
|
|
11436
11447
|
job_age_in_ms: Date.now() - timestamp.getTime(),
|
|
11437
11448
|
worker_id: workerId,
|
|
11438
|
-
worker_limit_concurrency:
|
|
11439
|
-
worker_limit_active:
|
|
11440
|
-
worker_limit_pending:
|
|
11449
|
+
worker_limit_concurrency: limiter.concurrency,
|
|
11450
|
+
worker_limit_active: limiter.activeCount,
|
|
11451
|
+
worker_limit_pending: limiter.pendingCount,
|
|
11441
11452
|
worker_name: this.options.name,
|
|
11442
11453
|
batch_size: batchSize
|
|
11443
11454
|
}
|