Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/webapp/app/routes/api.v1.tasks.$taskId.trigger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EngineServiceValidationError } from "@internal/run-engine";
import { json } from "@remix-run/server-runtime";
import {
generateJWT as internal_generateJWT,
Expand All @@ -8,7 +9,6 @@ import { TaskRun } from "@trigger.dev/database";
import { z } from "zod";
import { prisma } from "~/db.server";
import { env } from "~/env.server";
import { EngineServiceValidationError } from "~/runEngine/concerns/errors";
import { ApiAuthenticationResultSuccess, getOneTimeUseToken } from "~/services/apiAuth.server";
import { logger } from "~/services/logger.server";
import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server";
Expand Down
6 changes: 0 additions & 6 deletions apps/webapp/app/runEngine/concerns/errors.ts

This file was deleted.

7 changes: 2 additions & 5 deletions apps/webapp/app/runEngine/concerns/payloads.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PayloadProcessor, TriggerTaskRequest } from "../types";
import { env } from "~/env.server";
import { startActiveSpan } from "~/v3/tracer.server";
import { uploadPacketToObjectStore } from "~/v3/r2.server";
import { EngineServiceValidationError } from "./errors";
import { ServiceValidationError } from "~/v3/services/common.server";

export class DefaultPayloadProcessor implements PayloadProcessor {
async process(request: TriggerTaskRequest): Promise<IOPacket> {
Expand Down Expand Up @@ -36,10 +36,7 @@ export class DefaultPayloadProcessor implements PayloadProcessor {
);

if (uploadError) {
throw new EngineServiceValidationError(
"Failed to upload large payload to object store",
500
); // This is retryable
throw new ServiceValidationError("Failed to upload large payload to object store", 500); // This is retryable
}

return {
Expand Down
14 changes: 7 additions & 7 deletions apps/webapp/app/runEngine/concerns/queues.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
import { WorkerGroupService } from "~/v3/services/worker/workerGroupService.server";
import type { RunEngine } from "~/v3/runEngine.server";
import { env } from "~/env.server";
import { EngineServiceValidationError } from "./errors";
import { tryCatch } from "@trigger.dev/core/v3";
import { ServiceValidationError } from "~/v3/services/common.server";

export class DefaultQueueManager implements QueueManager {
constructor(
Expand Down Expand Up @@ -45,7 +45,7 @@ export class DefaultQueueManager implements QueueManager {
});

if (!specifiedQueue) {
throw new EngineServiceValidationError(
throw new ServiceValidationError(
`Specified queue '${specifiedQueueName}' not found or not associated with locked version '${
lockedBackgroundWorker.version ?? "<unknown>"
}'.`
Expand All @@ -68,7 +68,7 @@ export class DefaultQueueManager implements QueueManager {
});

if (!lockedTask) {
throw new EngineServiceValidationError(
throw new ServiceValidationError(
`Task '${request.taskId}' not found on locked version '${
lockedBackgroundWorker.version ?? "<unknown>"
}'.`
Expand All @@ -83,7 +83,7 @@ export class DefaultQueueManager implements QueueManager {
workerId: lockedBackgroundWorker.id,
version: lockedBackgroundWorker.version,
});
throw new EngineServiceValidationError(
throw new ServiceValidationError(
`Default queue configuration for task '${request.taskId}' missing on locked version '${
lockedBackgroundWorker.version ?? "<unknown>"
}'.`
Expand All @@ -97,7 +97,7 @@ export class DefaultQueueManager implements QueueManager {
// Task is not locked to a specific version, use regular logic
if (request.body.options?.lockToVersion) {
// This should only happen if the findFirst failed, indicating the version doesn't exist
throw new EngineServiceValidationError(
throw new ServiceValidationError(
`Task locked to version '${request.body.options.lockToVersion}', but no worker found with that version.`
);
}
Expand Down Expand Up @@ -221,11 +221,11 @@ export class DefaultQueueManager implements QueueManager {
);

if (error) {
throw new EngineServiceValidationError(error.message);
throw new ServiceValidationError(error.message);
}

if (!workerGroup) {
throw new EngineServiceValidationError("No worker group found");
throw new ServiceValidationError("No worker group found");
}

return workerGroup.masterQueue;
Expand Down
10 changes: 5 additions & 5 deletions apps/webapp/app/runEngine/services/triggerTask.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import type {
} from "../../v3/services/triggerTask.server";
import { getTaskEventStore } from "../../v3/taskEventStore.server";
import { clampMaxDuration } from "../../v3/utils/maxDuration";
import { EngineServiceValidationError } from "../concerns/errors";
import { IdempotencyKeyConcern } from "../concerns/idempotencyKeys.server";
import type {
PayloadProcessor,
Expand All @@ -41,6 +40,7 @@ import type {
TriggerTaskRequest,
TriggerTaskValidator,
} from "../types";
import { ServiceValidationError } from "~/v3/services/common.server";

export class RunEngineTriggerTaskService {
private readonly queueConcern: QueueManager;
Expand Down Expand Up @@ -157,7 +157,7 @@ export class RunEngineTriggerTaskService {
const [parseDelayError, delayUntil] = await tryCatch(parseDelay(body.options?.delay));

if (parseDelayError) {
throw new EngineServiceValidationError(`Invalid delay ${body.options?.delay}`);
throw new ServiceValidationError(`Invalid delay ${body.options?.delay}`);
}

const ttl =
Expand Down Expand Up @@ -210,7 +210,7 @@ export class RunEngineTriggerTaskService {
});

if (!queueSizeGuard.ok) {
throw new EngineServiceValidationError(
throw new ServiceValidationError(
`Cannot trigger ${taskId} as the queue size limit for this environment has been reached. The maximum size is ${queueSizeGuard.maximumSize}`
);
}
Expand Down Expand Up @@ -351,7 +351,7 @@ export class RunEngineTriggerTaskService {
);

if (result?.error) {
throw new EngineServiceValidationError(
throw new ServiceValidationError(
taskRunErrorToString(taskRunErrorEnhancer(result.error))
);
}
Expand All @@ -365,7 +365,7 @@ export class RunEngineTriggerTaskService {
}

if (error instanceof RunOneTimeUseTokenError) {
throw new EngineServiceValidationError(
throw new ServiceValidationError(
`Cannot trigger ${taskId} with a one-time use token as it has already been used.`
);
}
Expand Down
8 changes: 4 additions & 4 deletions apps/webapp/app/runEngine/validators/triggerTaskValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { logger } from "~/services/logger.server";
import { getEntitlement } from "~/services/platform.v3.server";
import { MAX_ATTEMPTS, OutOfEntitlementError } from "~/v3/services/triggerTask.server";
import { isFinalRunStatus } from "~/v3/taskStatus";
import { EngineServiceValidationError } from "../concerns/errors";
import type {
EntitlementValidationParams,
EntitlementValidationResult,
Expand All @@ -13,6 +12,7 @@ import type {
TriggerTaskValidator,
ValidationResult,
} from "../types";
import { ServiceValidationError } from "~/v3/services/common.server";

export class DefaultTriggerTaskValidator implements TriggerTaskValidator {
validateTags(params: TagValidationParams): ValidationResult {
Expand All @@ -29,7 +29,7 @@ export class DefaultTriggerTaskValidator implements TriggerTaskValidator {
if (tags.length > MAX_TAGS_PER_RUN) {
return {
ok: false,
error: new EngineServiceValidationError(
error: new ServiceValidationError(
`Runs can only have ${MAX_TAGS_PER_RUN} tags, you're trying to set ${tags.length}.`
),
};
Expand Down Expand Up @@ -65,7 +65,7 @@ export class DefaultTriggerTaskValidator implements TriggerTaskValidator {
if (attempt > MAX_ATTEMPTS) {
return {
ok: false,
error: new EngineServiceValidationError(
error: new ServiceValidationError(
`Failed to trigger ${taskId} after ${MAX_ATTEMPTS} attempts.`
),
};
Expand Down Expand Up @@ -95,7 +95,7 @@ export class DefaultTriggerTaskValidator implements TriggerTaskValidator {

return {
ok: false,
error: new EngineServiceValidationError(
error: new ServiceValidationError(
`Cannot trigger ${taskId} as the parent run has a status of ${parentRun.status}`
),
};
Expand Down
11 changes: 10 additions & 1 deletion apps/webapp/app/services/routeBuilders/apiBuilder.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
} from "~/v3/services/worker/workerGroupTokenService.server";
import { API_VERSIONS, getApiVersion } from "~/api/versions";
import { WORKER_HEADERS } from "@trigger.dev/core/v3/runEngineWorker";
import { ServiceValidationError } from "~/v3/services/common.server";
import { EngineServiceValidationError } from "@internal/run-engine";

type AnyZodSchema = z.ZodFirstPartySchemaTypes | z.ZodDiscriminatedUnion<any, any>;

Expand Down Expand Up @@ -1040,11 +1042,18 @@ export function createActionWorkerApiRoute<
});
return result;
} catch (error) {
console.error("Error in API route:", error);
if (error instanceof Response) {
return error;
}

if (error instanceof EngineServiceValidationError) {
return json({ error: error.message }, { status: error.status ?? 422 });
}

if (error instanceof ServiceValidationError) {
return json({ error: error.message }, { status: error.status ?? 422 });
}

logger.error("Error in action", {
error:
error instanceof Error
Expand Down
2 changes: 1 addition & 1 deletion internal-packages/run-engine/src/engine/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BillingCache } from "./billingCache.js";
import { createRedisClient, Redis } from "@internal/redis";
import { getMeter, Meter, startSpan, trace, Tracer } from "@internal/tracing";
import { Logger } from "@trigger.dev/core/logger";
Expand Down Expand Up @@ -30,6 +29,7 @@ import { FairQueueSelectionStrategy } from "../run-queue/fairQueueSelectionStrat
import { RunQueue } from "../run-queue/index.js";
import { RunQueueFullKeyProducer } from "../run-queue/keyProducer.js";
import { MinimalAuthenticatedEnvironment } from "../shared/index.js";
import { BillingCache } from "./billingCache.js";
import { NotImplementedError, RunDuplicateIdempotencyKeyError } from "./errors.js";
import { EventBus, EventBusEvents } from "./eventBus.js";
import { RunLocker } from "./locking.js";
Expand Down
6 changes: 6 additions & 0 deletions internal-packages/run-engine/src/engine/locking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Attributes,
Histogram,
} from "@internal/tracing";
import { ServiceValidationError } from "./errors.js";

const SemanticAttributes = {
LOCK_TYPE: "run_engine.lock.type",
Expand Down Expand Up @@ -174,6 +175,10 @@ export class RunLocker {
);

if (error) {
if (error instanceof ServiceValidationError) {
throw error;
}

// Record failed lock acquisition
const lockDuration = performance.now() - lockStartTime;
this.lockDurationHistogram.record(lockDuration, {
Expand All @@ -186,6 +191,7 @@ export class RunLocker {
resources,
duration: this.duration,
});

throw error;
}

Expand Down
6 changes: 5 additions & 1 deletion internal-packages/run-engine/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export { RunEngine } from "./engine/index.js";
export { RunDuplicateIdempotencyKeyError, RunOneTimeUseTokenError } from "./engine/errors.js";
export {
RunDuplicateIdempotencyKeyError,
RunOneTimeUseTokenError,
ServiceValidationError as EngineServiceValidationError,
} from "./engine/errors.js";
export type { EventBusEventArgs, EventBusEvents } from "./engine/eventBus.js";
export type { AuthenticatedEnvironment } from "./shared/index.js";
Loading