@@ -27,6 +27,7 @@ import { OutOfEntitlementError, TriggerTaskServiceOptions } from "./triggerTask.
2727import { Prisma } from "@trigger.dev/database" ;
2828import { resolveIdempotencyKeyTTL } from "~/utils/idempotencyKeys.server" ;
2929import { clampMaxDuration } from "../utils/maxDuration" ;
30+ import { RunEngine } from "@internal/run-engine" ;
3031
3132/** @deprecated Use TriggerTaskService in `triggerTask.server.ts` instead. */
3233export class TriggerTaskServiceV2 extends WithRunEngine {
@@ -117,7 +118,7 @@ export class TriggerTaskServiceV2 extends WithRunEngine {
117118 }
118119
119120 if ( ! options . skipChecks ) {
120- const queueSizeGuard = await this . # guardQueueSizeLimitsForEnv( environment ) ;
121+ const queueSizeGuard = await guardQueueSizeLimitsForEnv ( this . _engine , environment ) ;
121122
122123 logger . debug ( "Queue size guard result" , {
123124 queueSizeGuard,
@@ -465,22 +466,6 @@ export class TriggerTaskServiceV2 extends WithRunEngine {
465466
466467 return { dataType : payloadType } ;
467468 }
468-
469- async #guardQueueSizeLimitsForEnv( environment : AuthenticatedEnvironment ) {
470- const maximumSize = getMaximumSizeForEnvironment ( environment ) ;
471-
472- if ( typeof maximumSize === "undefined" ) {
473- return { isWithinLimits : true } ;
474- }
475-
476- const queueSize = await this . _engine . lengthOfEnvQueue ( environment ) ;
477-
478- return {
479- isWithinLimits : queueSize < maximumSize ,
480- maximumSize,
481- queueSize,
482- } ;
483- }
484469}
485470
486471function getMaximumSizeForEnvironment ( environment : AuthenticatedEnvironment ) : number | undefined {
@@ -490,3 +475,24 @@ function getMaximumSizeForEnvironment(environment: AuthenticatedEnvironment): nu
490475 return environment . organization . maximumDeployedQueueSize ?? env . MAXIMUM_DEPLOYED_QUEUE_SIZE ;
491476 }
492477}
478+
479+ export async function guardQueueSizeLimitsForEnv (
480+ engine : RunEngine ,
481+ environment : AuthenticatedEnvironment ,
482+ itemsToAdd : number = 1
483+ ) {
484+ const maximumSize = getMaximumSizeForEnvironment ( environment ) ;
485+
486+ if ( typeof maximumSize === "undefined" ) {
487+ return { isWithinLimits : true } ;
488+ }
489+
490+ const queueSize = await engine . lengthOfEnvQueue ( environment ) ;
491+ const projectedSize = queueSize + itemsToAdd ;
492+
493+ return {
494+ isWithinLimits : projectedSize <= maximumSize ,
495+ maximumSize,
496+ queueSize,
497+ } ;
498+ }
0 commit comments