Skip to content

Commit efc83c9

Browse files
committed
Removed batch idempotency because it gets put on the runs instead
1 parent 151a50a commit efc83c9

File tree

3 files changed

+42
-133
lines changed

3 files changed

+42
-133
lines changed

apps/webapp/app/routes/api.v2.tasks.batch.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { json } from "@remix-run/server-runtime";
22
import {
3-
BatchTriggerTaskV2RequestBody,
4-
BatchTriggerTaskV2Response,
53
BatchTriggerTaskV3RequestBody,
64
BatchTriggerTaskV3Response,
75
generateJWT,
@@ -10,7 +8,6 @@ import { env } from "~/env.server";
108
import { AuthenticatedEnvironment, getOneTimeUseToken } from "~/services/apiAuth.server";
119
import { logger } from "~/services/logger.server";
1210
import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server";
13-
import { resolveIdempotencyKeyTTL } from "~/utils/idempotencyKeys.server";
1411
import { ServiceValidationError } from "~/v3/services/baseService.server";
1512
import {
1613
BatchProcessingStrategy,
@@ -52,8 +49,6 @@ const { action, loader } = createActionApiRoute(
5249
}
5350

5451
const {
55-
"idempotency-key": idempotencyKey,
56-
"idempotency-key-ttl": idempotencyKeyTTL,
5752
"trigger-version": triggerVersion,
5853
"x-trigger-span-parent-as-link": spanParentAsLink,
5954
"x-trigger-worker": isFromWorker,
@@ -67,8 +62,6 @@ const { action, loader } = createActionApiRoute(
6762
const oneTimeUseToken = await getOneTimeUseToken(authentication);
6863

6964
logger.debug("Batch trigger request", {
70-
idempotencyKey,
71-
idempotencyKeyTTL,
7265
triggerVersion,
7366
spanParentAsLink,
7467
isFromWorker,
@@ -83,17 +76,10 @@ const { action, loader } = createActionApiRoute(
8376
? { traceparent, tracestate }
8477
: undefined;
8578

86-
// By default, the idempotency key expires in 30 days
87-
const idempotencyKeyExpiresAt =
88-
resolveIdempotencyKeyTTL(idempotencyKeyTTL) ??
89-
new Date(Date.now() + 24 * 60 * 60 * 1000 * 30);
90-
9179
const service = new BatchTriggerV3Service(batchProcessingStrategy ?? undefined);
9280

9381
try {
9482
const batch = await service.call(authentication.environment, body, {
95-
idempotencyKey: idempotencyKey ?? undefined,
96-
idempotencyKeyExpiresAt,
9783
triggerVersion: triggerVersion ?? undefined,
9884
traceContext,
9985
spanParentAsLink: spanParentAsLink === 1,

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

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { downloadPacketFromObjectStore, uploadPacketToObjectStore } from "../r2.
1818
import { startActiveSpan } from "../tracer.server";
1919
import { ServiceValidationError, WithRunEngine } from "./baseService.server";
2020
import { OutOfEntitlementError, TriggerTaskService } from "./triggerTask.server";
21-
import { guardQueueSizeLimitsForEnv } from "./triggerTaskV2.server";
2221

2322
const PROCESSING_BATCH_SIZE = 50;
2423
const ASYNC_BATCH_PROCESS_SIZE_THRESHOLD = 20;
@@ -40,8 +39,6 @@ export const BatchProcessingOptions = z.object({
4039
export type BatchProcessingOptions = z.infer<typeof BatchProcessingOptions>;
4140

4241
export type BatchTriggerTaskServiceOptions = {
43-
idempotencyKey?: string;
44-
idempotencyKeyExpiresAt?: Date;
4542
triggerVersion?: string;
4643
traceContext?: Record<string, string | undefined>;
4744
spanParentAsLink?: boolean;
@@ -73,61 +70,6 @@ export class BatchTriggerV3Service extends WithRunEngine {
7370
"call()",
7471
environment,
7572
async (span) => {
76-
const existingBatch = options.idempotencyKey
77-
? await this._prisma.batchTaskRun.findUnique({
78-
where: {
79-
runtimeEnvironmentId_idempotencyKey: {
80-
runtimeEnvironmentId: environment.id,
81-
idempotencyKey: options.idempotencyKey,
82-
},
83-
},
84-
})
85-
: undefined;
86-
87-
if (existingBatch) {
88-
if (
89-
existingBatch.idempotencyKeyExpiresAt &&
90-
existingBatch.idempotencyKeyExpiresAt < new Date()
91-
) {
92-
logger.debug("[BatchTriggerV3][call] Idempotency key has expired", {
93-
idempotencyKey: options.idempotencyKey,
94-
batch: {
95-
id: existingBatch.id,
96-
friendlyId: existingBatch.friendlyId,
97-
runCount: existingBatch.runCount,
98-
idempotencyKeyExpiresAt: existingBatch.idempotencyKeyExpiresAt,
99-
idempotencyKey: existingBatch.idempotencyKey,
100-
},
101-
});
102-
103-
// Update the existing batch to remove the idempotency key
104-
await this._prisma.batchTaskRun.update({
105-
where: { id: existingBatch.id },
106-
data: { idempotencyKey: null },
107-
});
108-
109-
// Don't return, just continue with the batch trigger
110-
} else {
111-
span.setAttribute("batchId", existingBatch.friendlyId);
112-
113-
//block the parent with all of the children
114-
if (body.resumeParentOnCompletion && body.parentRunId) {
115-
await this.#blockParentRun({
116-
parentRunId: body.parentRunId,
117-
childFriendlyIds: existingBatch.runIds,
118-
environment,
119-
});
120-
}
121-
122-
return {
123-
id: existingBatch.friendlyId,
124-
idempotencyKey: existingBatch.idempotencyKey ?? undefined,
125-
isCached: true,
126-
runCount: existingBatch.runCount,
127-
};
128-
}
129-
}
130-
13173
const { id, friendlyId } = BatchId.generate();
13274

13375
span.setAttribute("batchId", friendlyId);
@@ -212,8 +154,6 @@ export class BatchTriggerV3Service extends WithRunEngine {
212154
id: BatchId.fromFriendlyId(batchId),
213155
friendlyId: batchId,
214156
runtimeEnvironmentId: environment.id,
215-
idempotencyKey: options.idempotencyKey,
216-
idempotencyKeyExpiresAt: options.idempotencyKeyExpiresAt,
217157
runCount: body.items.length,
218158
runIds: [],
219159
payload: payloadPacket.data,
@@ -305,8 +245,6 @@ export class BatchTriggerV3Service extends WithRunEngine {
305245
id: BatchId.fromFriendlyId(batchId),
306246
friendlyId: batchId,
307247
runtimeEnvironmentId: environment.id,
308-
idempotencyKey: options.idempotencyKey,
309-
idempotencyKeyExpiresAt: options.idempotencyKeyExpiresAt,
310248
runCount: body.items.length,
311249
runIds: [],
312250
payload: payloadPacket.data,
@@ -728,40 +666,4 @@ export class BatchTriggerV3Service extends WithRunEngine {
728666
};
729667
});
730668
}
731-
732-
//todo what if the idempotent batch hasn't finished creating all the runs yet?!
733-
async #blockParentRun({
734-
parentRunId,
735-
childFriendlyIds,
736-
environment,
737-
}: {
738-
parentRunId: string;
739-
childFriendlyIds: string[];
740-
environment: AuthenticatedEnvironment;
741-
}) {
742-
const runsWithAssociatedWaitpoints = await this._prisma.taskRun.findMany({
743-
where: {
744-
id: {
745-
in: childFriendlyIds.map((r) => RunId.fromFriendlyId(r)),
746-
},
747-
},
748-
select: {
749-
associatedWaitpoint: {
750-
select: {
751-
id: true,
752-
},
753-
},
754-
},
755-
});
756-
757-
await this._engine.blockRunWithWaitpoint({
758-
runId: RunId.fromFriendlyId(parentRunId),
759-
waitpoints: runsWithAssociatedWaitpoints.flatMap((r) => {
760-
if (!r.associatedWaitpoint) return [];
761-
return [r.associatedWaitpoint.id];
762-
}),
763-
environmentId: environment.id,
764-
projectId: environment.projectId,
765-
});
766-
}
767669
}

references/hello-world/src/trigger/idempotency.ts

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,51 @@ export const idempotencyBatch = task({
118118
const successfulKey = await idempotencyKeys.create("a", { scope: "global" });
119119
const failureKey = await idempotencyKeys.create("b", { scope: "global" });
120120
const anotherKey = await idempotencyKeys.create("c", { scope: "global" });
121+
const batchKey = await idempotencyKeys.create("batch", { scope: "global" });
122+
123+
const batch1 = await childTask.batchTriggerAndWait(
124+
[
125+
{
126+
payload: { message: "Hello, world!" },
127+
options: { idempotencyKey: successfulKey, idempotencyKeyTTL: "120s" },
128+
},
129+
{
130+
payload: { message: "Hello, world 2!" },
131+
options: { idempotencyKey: failureKey, idempotencyKeyTTL: "120s" },
132+
},
133+
{
134+
payload: { message: "Hello, world 3", duration: 500, failureChance: 0 },
135+
},
136+
],
137+
{
138+
idempotencyKey: batchKey,
139+
idempotencyKeyTTL: "120s",
140+
}
141+
);
142+
logger.log("Batch 1", { batch1 });
121143

122-
const batch1 = await childTask.batchTriggerAndWait([
123-
{
124-
payload: { message: "Hello, world!" },
125-
options: { idempotencyKey: successfulKey, idempotencyKeyTTL: "120s" },
126-
},
127-
{
128-
payload: { message: "Hello, world 2!" },
129-
options: { idempotencyKey: failureKey, idempotencyKeyTTL: "120s" },
130-
},
131-
{
132-
payload: { message: "Hello, world 3", duration: 500, failureChance: 0 },
133-
},
134-
]);
144+
const batch2 = await childTask.batchTriggerAndWait(
145+
[
146+
{
147+
payload: { message: "Hello, world!" },
148+
options: { idempotencyKey: successfulKey, idempotencyKeyTTL: "120s" },
149+
},
150+
{
151+
payload: { message: "Hello, world 2!" },
152+
options: { idempotencyKey: failureKey, idempotencyKeyTTL: "120s" },
153+
},
154+
{
155+
payload: { message: "Hello, world 3", duration: 500, failureChance: 0 },
156+
},
157+
],
158+
{
159+
idempotencyKey: batchKey,
160+
idempotencyKeyTTL: "120s",
161+
}
162+
);
135163
logger.log("Batch 1", { batch1 });
136164

137-
const batch2 = await childTask.batchTriggerAndWait([
165+
await childTask.batchTrigger([
138166
{
139167
payload: { message: "Hello, world!" },
140168
options: { idempotencyKey: successfulKey, idempotencyKeyTTL: "120s" },
@@ -143,21 +171,14 @@ export const idempotencyBatch = task({
143171
payload: { message: "Hello, world 2!" },
144172
options: { idempotencyKey: failureKey, idempotencyKeyTTL: "120s" },
145173
},
146-
{
147-
payload: { message: "Hello, world 3", duration: 500, failureChance: 0 },
148-
options: { idempotencyKey: anotherKey, idempotencyKeyTTL: "120s" },
149-
},
150174
]);
151-
logger.log("Batch 1", { batch1 });
152175

153176
await childTask.batchTrigger([
154177
{
155178
payload: { message: "Hello, world!" },
156-
options: { idempotencyKey: successfulKey, idempotencyKeyTTL: "120s" },
157179
},
158180
{
159181
payload: { message: "Hello, world 2!" },
160-
options: { idempotencyKey: failureKey, idempotencyKeyTTL: "120s" },
161182
},
162183
]);
163184

0 commit comments

Comments
 (0)