Skip to content

Commit fae98c0

Browse files
committed
start using new entitlement response
1 parent 19283e2 commit fae98c0

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

apps/webapp/app/runEngine/services/triggerTask.server.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,35 @@ export class RunEngineTriggerTaskService {
123123
throw tagValidation.error;
124124
}
125125

126-
// Validate entitlement
127-
const entitlementValidation = await this.validator.validateEntitlement({
128-
environment,
129-
});
126+
// Validate entitlement (unless skipChecks is enabled)
127+
let planType: string | undefined;
128+
129+
if (!options.skipChecks) {
130+
const entitlementValidation = await this.validator.validateEntitlement({
131+
environment,
132+
});
130133

131-
if (!entitlementValidation.ok) {
132-
throw entitlementValidation.error;
134+
if (!entitlementValidation.ok) {
135+
throw entitlementValidation.error;
136+
}
137+
138+
// Extract plan type from entitlement response
139+
planType = entitlementValidation.plan?.type;
140+
} else {
141+
// When skipChecks is enabled, planType should be passed via options
142+
planType = options.planType;
143+
144+
if (!planType) {
145+
logger.warn("Plan type not set but skipChecks is enabled", {
146+
taskId,
147+
environment: {
148+
id: environment.id,
149+
type: environment.type,
150+
projectId: environment.projectId,
151+
organizationId: environment.organizationId,
152+
},
153+
});
154+
}
133155
}
134156

135157
const [parseDelayError, delayUntil] = await tryCatch(parseDelay(body.options?.delay));
@@ -313,6 +335,7 @@ export class RunEngineTriggerTaskService {
313335
scheduleInstanceId: options.scheduleInstanceId,
314336
createdAt: options.overrideCreatedAt,
315337
bulkActionId: body.options?.bulkActionId,
338+
planType,
316339
},
317340
this.prisma
318341
);

apps/webapp/app/runEngine/types.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import { BackgroundWorker, TaskRun } from "@trigger.dev/database";
2-
3-
import {
4-
IOPacket,
5-
RunChainState,
6-
TaskRunError,
7-
TriggerTaskRequestBody,
8-
} from "@trigger.dev/core/v3";
9-
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
1+
import type { BackgroundWorker, TaskRun } from "@trigger.dev/database";
2+
import type { IOPacket, TaskRunError, TriggerTaskRequestBody } from "@trigger.dev/core/v3";
3+
import type { AuthenticatedEnvironment } from "~/services/apiAuth.server";
4+
import type { ReportUsagePlan } from "@trigger.dev/platform";
105

116
export type TriggerTaskServiceOptions = {
127
idempotencyKey?: string;
@@ -22,6 +17,7 @@ export type TriggerTaskServiceOptions = {
2217
skipChecks?: boolean;
2318
oneTimeUseToken?: string;
2419
overrideCreatedAt?: Date;
20+
planType?: string;
2521
};
2622

2723
// domain/triggerTask.ts
@@ -112,9 +108,19 @@ export type ValidationResult =
112108
error: Error;
113109
};
114110

111+
export type EntitlementValidationResult =
112+
| {
113+
ok: true;
114+
plan?: ReportUsagePlan;
115+
}
116+
| {
117+
ok: false;
118+
error: Error;
119+
};
120+
115121
export interface TriggerTaskValidator {
116122
validateTags(params: TagValidationParams): ValidationResult;
117-
validateEntitlement(params: EntitlementValidationParams): Promise<ValidationResult>;
123+
validateEntitlement(params: EntitlementValidationParams): Promise<EntitlementValidationResult>;
118124
validateMaxAttempts(params: MaxAttemptsValidationParams): ValidationResult;
119125
validateParentRun(params: ParentRunValidationParams): ValidationResult;
120126
}

apps/webapp/app/runEngine/validators/triggerTaskValidator.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { getEntitlement } from "~/services/platform.v3.server";
44
import { MAX_ATTEMPTS, OutOfEntitlementError } from "~/v3/services/triggerTask.server";
55
import { isFinalRunStatus } from "~/v3/taskStatus";
66
import { EngineServiceValidationError } from "../concerns/errors";
7-
import {
7+
import type {
88
EntitlementValidationParams,
9+
EntitlementValidationResult,
910
MaxAttemptsValidationParams,
1011
ParentRunValidationParams,
1112
TagValidationParams,
@@ -37,7 +38,9 @@ export class DefaultTriggerTaskValidator implements TriggerTaskValidator {
3738
return { ok: true };
3839
}
3940

40-
async validateEntitlement(params: EntitlementValidationParams): Promise<ValidationResult> {
41+
async validateEntitlement(
42+
params: EntitlementValidationParams
43+
): Promise<EntitlementValidationResult> {
4144
const { environment } = params;
4245

4346
if (environment.type === "DEVELOPMENT") {
@@ -53,7 +56,7 @@ export class DefaultTriggerTaskValidator implements TriggerTaskValidator {
5356
};
5457
}
5558

56-
return { ok: true };
59+
return { ok: true, plan: result?.plan };
5760
}
5861

5962
validateMaxAttempts(params: MaxAttemptsValidationParams): ValidationResult {

apps/webapp/app/services/platform.v3.server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
type MachineCode,
1111
type UpdateBillingAlertsRequest,
1212
type BillingAlertsResult,
13+
type ReportUsageResult,
14+
type ReportUsagePlan,
1315
} from "@trigger.dev/platform";
1416
import { createCache, DefaultStatefulContext, Namespace } from "@unkey/cache";
1517
import { MemoryStore } from "@unkey/cache/stores";
@@ -432,7 +434,9 @@ export async function reportComputeUsage(request: Request) {
432434
});
433435
}
434436

435-
export async function getEntitlement(organizationId: string) {
437+
export async function getEntitlement(
438+
organizationId: string
439+
): Promise<ReportUsageResult | undefined> {
436440
if (!client) return undefined;
437441

438442
try {

apps/webapp/app/v3/services/triggerTask.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type TriggerTaskServiceOptions = {
3333
queueTimestamp?: Date;
3434
overrideCreatedAt?: Date;
3535
replayedFromTaskRunFriendlyId?: string;
36+
planType?: string;
3637
};
3738

3839
export class OutOfEntitlementError extends Error {

0 commit comments

Comments
 (0)