577f35eebe
## Summary The trigger hot path's mollifier integration: - `mollifyTrigger`: when the gate trips, write the engine.trigger snapshot to the buffer and return a synthesised QUEUED response. Postgres write is deferred to drainer-replay (next PR in the stack). - Pre-gate idempotency-key claim: same-key triggers serialise through Redis so a burst lands in PG / buffer exactly once. - Read-fallback extensions: `findRunByIdWithMollifierFallback` for the trigger-time idempotency lookup that must see buffered runs. - Gate bypasses: `debounce`, `oneTimeUseToken`, `parentTaskRunId`/`triggerAndWait` skip the mollify path entirely. - `triggerTask` + `IdempotencyKeyConcern` wired to the above. All behaviour gated by the master `TRIGGER_MOLLIFIER_ENABLED` switch; off-state hot path is unchanged (the gate is not even consulted). Stacked on the buffer extensions PR. ## Test plan - [x] \`pnpm run typecheck --filter webapp\` passes - [x] \`pnpm run test --filter webapp test/mollifierMollify.test.ts\` passes - [x] \`pnpm run test --filter webapp test/mollifierIdempotencyClaim.test.ts\` passes - [x] \`pnpm run test --filter webapp test/mollifierReadFallback.test.ts\` passes - [x] \`pnpm run test --filter webapp test/mollifierGate.test.ts\` passes - [x] \`pnpm run test --filter webapp test/engine/triggerTask.test.ts\` passes --- ## Ship-gate follow-up fixes - **Batch items bypass the mollifier gate** — fixes `BatchTaskRunItem_taskRunId_fkey` FK violation on batch triggers when the gate trips. End-state is a drainer-side `BatchTaskRunItem` create-on-materialise; batch traffic passes through the gate until that lands. - **IdempotencyKeyConcern honours buffered-run TTL on expiry** — buffered path now clears expired idempotency claims (read-side) and resets the buffer's `mollifier:idempotency:*` SETNX binding (write-side) so a re-trigger past the customer's TTL lands as a fresh run instead of echoing the stale buffered runId. --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
131 lines
4.4 KiB
TypeScript
131 lines
4.4 KiB
TypeScript
import { TriggerTaskRequestBody } from "@trigger.dev/core/v3";
|
|
import { RunEngineVersion, TaskRun } from "@trigger.dev/database";
|
|
import { env } from "~/env.server";
|
|
import { IdempotencyKeyConcern } from "~/runEngine/concerns/idempotencyKeys.server";
|
|
import { DefaultPayloadProcessor } from "~/runEngine/concerns/payloads.server";
|
|
import { DefaultQueueManager } from "~/runEngine/concerns/queues.server";
|
|
import { DefaultTraceEventsConcern } from "~/runEngine/concerns/traceEvents.server";
|
|
import { RunEngineTriggerTaskService } from "~/runEngine/services/triggerTask.server";
|
|
import { DefaultTriggerTaskValidator } from "~/runEngine/validators/triggerTaskValidator";
|
|
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
|
|
import { determineEngineVersion } from "../engineVersion.server";
|
|
import { tracer } from "../tracer.server";
|
|
import { WithRunEngine } from "./baseService.server";
|
|
import { TriggerTaskServiceV1 } from "./triggerTaskV1.server";
|
|
|
|
export type TriggerTaskServiceOptions = {
|
|
idempotencyKey?: string;
|
|
idempotencyKeyExpiresAt?: Date;
|
|
triggerVersion?: string;
|
|
traceContext?: Record<string, unknown>;
|
|
spanParentAsLink?: boolean;
|
|
parentAsLinkType?: "replay" | "trigger";
|
|
batchId?: string;
|
|
batchIndex?: number;
|
|
customIcon?: string;
|
|
runFriendlyId?: string;
|
|
skipChecks?: boolean;
|
|
oneTimeUseToken?: string;
|
|
scheduleId?: string;
|
|
scheduleInstanceId?: string;
|
|
queueTimestamp?: Date;
|
|
overrideCreatedAt?: Date;
|
|
replayedFromTaskRunFriendlyId?: string;
|
|
planType?: string;
|
|
realtimeStreamsVersion?: "v1" | "v2";
|
|
triggerSource?: string;
|
|
triggerAction?: string;
|
|
};
|
|
|
|
export class OutOfEntitlementError extends Error {
|
|
constructor() {
|
|
super("You can't trigger a task because you have run out of credits.");
|
|
}
|
|
}
|
|
|
|
export type TriggerTaskServiceResult = {
|
|
run: TaskRun;
|
|
isCached: boolean;
|
|
// True when the mollifier gate diverted the trigger to the Redis
|
|
// buffer and `run` is a synthesised record (no PG row exists yet).
|
|
// The trigger route reads this to skip `saveRequestIdempotency` —
|
|
// caching the synth runId would mean a lost-response SDK retry hits
|
|
// a PG-miss in `handleRequestIdempotency` and falls through to a
|
|
// fresh trigger, producing a duplicate buffer entry for trigger
|
|
// calls that don't carry a task-level idempotency key.
|
|
isMollified?: boolean;
|
|
};
|
|
|
|
export const MAX_ATTEMPTS = 2;
|
|
|
|
export class TriggerTaskService extends WithRunEngine {
|
|
public async call(
|
|
taskId: string,
|
|
environment: AuthenticatedEnvironment,
|
|
body: TriggerTaskRequestBody,
|
|
options: TriggerTaskServiceOptions = {},
|
|
version?: RunEngineVersion
|
|
): Promise<TriggerTaskServiceResult | undefined> {
|
|
return await this.traceWithEnv("call()", environment, async (span) => {
|
|
span.setAttribute("taskId", taskId);
|
|
|
|
const v = await determineEngineVersion({
|
|
environment,
|
|
workerVersion: body.options?.lockToVersion,
|
|
engineVersion: version,
|
|
});
|
|
|
|
switch (v) {
|
|
case "V1": {
|
|
return await this.callV1(taskId, environment, body, options);
|
|
}
|
|
case "V2": {
|
|
return await this.callV2(taskId, environment, body, options);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private async callV1(
|
|
taskId: string,
|
|
environment: AuthenticatedEnvironment,
|
|
body: TriggerTaskRequestBody,
|
|
options: TriggerTaskServiceOptions = {}
|
|
): Promise<TriggerTaskServiceResult | undefined> {
|
|
const service = new TriggerTaskServiceV1(this._prisma);
|
|
return await service.call(taskId, environment, body, options);
|
|
}
|
|
|
|
private async callV2(
|
|
taskId: string,
|
|
environment: AuthenticatedEnvironment,
|
|
body: TriggerTaskRequestBody,
|
|
options: TriggerTaskServiceOptions = {}
|
|
): Promise<TriggerTaskServiceResult | undefined> {
|
|
const traceEventConcern = new DefaultTraceEventsConcern();
|
|
|
|
const service = new RunEngineTriggerTaskService({
|
|
prisma: this._prisma,
|
|
engine: this._engine,
|
|
queueConcern: new DefaultQueueManager(this._prisma, this._engine, this._replica),
|
|
validator: new DefaultTriggerTaskValidator(),
|
|
payloadProcessor: new DefaultPayloadProcessor(),
|
|
idempotencyKeyConcern: new IdempotencyKeyConcern(
|
|
this._prisma,
|
|
this._engine,
|
|
traceEventConcern
|
|
),
|
|
traceEventConcern,
|
|
tracer: tracer,
|
|
metadataMaximumSize: env.TASK_RUN_METADATA_MAXIMUM_SIZE,
|
|
});
|
|
|
|
return await service.call({
|
|
taskId,
|
|
environment,
|
|
body,
|
|
options,
|
|
});
|
|
}
|
|
}
|