Skip to content

Commit 2a07ea4

Browse files
authored
Optionally trigger batched items sequentially to preserve order (#1536)
* Optionally trigger batched items sequentially to preserve order * Fix infinite v3.processBatchTaskRun enqueuings by checking the attemptCount
1 parent 91afa5e commit 2a07ea4

File tree

12 files changed

+219
-78
lines changed

12 files changed

+219
-78
lines changed

.changeset/sweet-suits-kick.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
"@trigger.dev/core": patch
4+
---
5+
6+
Add option to trigger batched items sequentially, and default to parallel triggering which is faster

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ import { env } from "~/env.server";
99
import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server";
1010
import { HeadersSchema } from "./api.v1.tasks.$taskId.trigger";
1111
import { resolveIdempotencyKeyTTL } from "~/utils/idempotencyKeys.server";
12-
import { BatchTriggerV2Service } from "~/v3/services/batchTriggerV2.server";
12+
import {
13+
BatchProcessingStrategy,
14+
BatchTriggerV2Service,
15+
} from "~/v3/services/batchTriggerV2.server";
1316
import { ServiceValidationError } from "~/v3/services/baseService.server";
1417
import { OutOfEntitlementError } from "~/v3/services/triggerTask.server";
1518
import { AuthenticatedEnvironment, getOneTimeUseToken } from "~/services/apiAuth.server";
1619
import { logger } from "~/services/logger.server";
20+
import { z } from "zod";
1721

1822
const { action, loader } = createActionApiRoute(
1923
{
20-
headers: HeadersSchema,
24+
headers: HeadersSchema.extend({
25+
"batch-processing-strategy": BatchProcessingStrategy.nullish(),
26+
}),
2127
body: BatchTriggerTaskV2RequestBody,
2228
allowJWT: true,
2329
maxContentLength: env.BATCH_TASK_PAYLOAD_MAXIMUM_SIZE,
@@ -52,6 +58,7 @@ const { action, loader } = createActionApiRoute(
5258
"x-trigger-span-parent-as-link": spanParentAsLink,
5359
"x-trigger-worker": isFromWorker,
5460
"x-trigger-client": triggerClient,
61+
"batch-processing-strategy": batchProcessingStrategy,
5562
traceparent,
5663
tracestate,
5764
} = headers;
@@ -67,6 +74,7 @@ const { action, loader } = createActionApiRoute(
6774
triggerClient,
6875
traceparent,
6976
tracestate,
77+
batchProcessingStrategy,
7078
});
7179

7280
const traceContext =
@@ -79,7 +87,7 @@ const { action, loader } = createActionApiRoute(
7987
resolveIdempotencyKeyTTL(idempotencyKeyTTL) ??
8088
new Date(Date.now() + 24 * 60 * 60 * 1000 * 30);
8189

82-
const service = new BatchTriggerV2Service();
90+
const service = new BatchTriggerV2Service(batchProcessingStrategy ?? undefined);
8391

8492
try {
8593
const batch = await service.call(authentication.environment, body, {

apps/webapp/app/routes/api.v3.runs.$runId.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const loader = createLoaderApiRoute(
1515
findResource: (params, auth) => {
1616
return ApiRetrieveRunPresenter.findRun(params.runId, auth.environment);
1717
},
18+
shouldRetryNotFound: true,
1819
authorization: {
1920
action: "read",
2021
resource: (run) => ({

apps/webapp/app/services/routeBuilders/apiBuilder.server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ApiKeyRouteBuilderOptions<
3333
params: TParamsSchema extends z.AnyZodObject ? z.infer<TParamsSchema> : undefined,
3434
authentication: ApiAuthenticationResultSuccess
3535
) => Promise<TResource | undefined>;
36+
shouldRetryNotFound?: boolean;
3637
authorization?: {
3738
action: AuthorizationAction;
3839
resource: (
@@ -81,6 +82,7 @@ export function createLoaderApiRoute<
8182
corsStrategy = "none",
8283
authorization,
8384
findResource,
85+
shouldRetryNotFound,
8486
} = options;
8587

8688
if (corsStrategy !== "none" && request.method.toUpperCase() === "OPTIONS") {
@@ -162,7 +164,10 @@ export function createLoaderApiRoute<
162164
if (!resource) {
163165
return await wrapResponse(
164166
request,
165-
json({ error: "Not found" }, { status: 404 }),
167+
json(
168+
{ error: "Not found" },
169+
{ status: 404, headers: { "x-should-retry": shouldRetryNotFound ? "true" : "false" } }
170+
),
166171
corsStrategy !== "none"
167172
);
168173
}

apps/webapp/app/services/worker.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ function getWorkerQueue() {
733733
priority: 0,
734734
maxAttempts: 5,
735735
handler: async (payload, job) => {
736-
const service = new BatchTriggerV2Service();
736+
const service = new BatchTriggerV2Service(payload.strategy);
737737

738738
await service.processBatchTaskRun(payload);
739739
},

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
parsePacket,
77
} from "@trigger.dev/core/v3";
88
import { BatchTaskRun, Prisma, TaskRunAttempt } from "@trigger.dev/database";
9-
import { $transaction, PrismaClientOrTransaction } from "~/db.server";
9+
import { $transaction, prisma, PrismaClientOrTransaction } from "~/db.server";
1010
import { env } from "~/env.server";
1111
import { batchTaskRunItemStatusForRunStatus } from "~/models/taskRun.server";
1212
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
@@ -25,12 +25,10 @@ import { z } from "zod";
2525

2626
const PROCESSING_BATCH_SIZE = 50;
2727
const ASYNC_BATCH_PROCESS_SIZE_THRESHOLD = 20;
28+
const MAX_ATTEMPTS = 10;
2829

29-
const BatchProcessingStrategy = z.enum(["sequential", "parallel"]);
30-
31-
type BatchProcessingStrategy = z.infer<typeof BatchProcessingStrategy>;
32-
33-
const CURRENT_STRATEGY: BatchProcessingStrategy = "parallel";
30+
export const BatchProcessingStrategy = z.enum(["sequential", "parallel"]);
31+
export type BatchProcessingStrategy = z.infer<typeof BatchProcessingStrategy>;
3432

3533
export const BatchProcessingOptions = z.object({
3634
batchId: z.string(),
@@ -52,6 +50,17 @@ export type BatchTriggerTaskServiceOptions = {
5250
};
5351

5452
export class BatchTriggerV2Service extends BaseService {
53+
private _batchProcessingStrategy: BatchProcessingStrategy;
54+
55+
constructor(
56+
batchProcessingStrategy?: BatchProcessingStrategy,
57+
protected readonly _prisma: PrismaClientOrTransaction = prisma
58+
) {
59+
super(_prisma);
60+
61+
this._batchProcessingStrategy = batchProcessingStrategy ?? "parallel";
62+
}
63+
5564
public async call(
5665
environment: AuthenticatedEnvironment,
5766
body: BatchTriggerTaskV2RequestBody,
@@ -452,14 +461,14 @@ export class BatchTriggerV2Service extends BaseService {
452461
},
453462
});
454463

455-
switch (CURRENT_STRATEGY) {
464+
switch (this._batchProcessingStrategy) {
456465
case "sequential": {
457466
await this.#enqueueBatchTaskRun({
458467
batchId: batch.id,
459468
processingId: batchId,
460469
range: { start: 0, count: PROCESSING_BATCH_SIZE },
461470
attemptCount: 0,
462-
strategy: CURRENT_STRATEGY,
471+
strategy: this._batchProcessingStrategy,
463472
});
464473

465474
break;
@@ -480,7 +489,7 @@ export class BatchTriggerV2Service extends BaseService {
480489
processingId: `${index}`,
481490
range,
482491
attemptCount: 0,
483-
strategy: CURRENT_STRATEGY,
492+
strategy: this._batchProcessingStrategy,
484493
},
485494
tx
486495
)
@@ -539,6 +548,16 @@ export class BatchTriggerV2Service extends BaseService {
539548

540549
const $attemptCount = options.attemptCount + 1;
541550

551+
// Add early return if max attempts reached
552+
if ($attemptCount > MAX_ATTEMPTS) {
553+
logger.error("[BatchTriggerV2][processBatchTaskRun] Max attempts reached", {
554+
options,
555+
attemptCount: $attemptCount,
556+
});
557+
// You might want to update the batch status to failed here
558+
return;
559+
}
560+
542561
const batch = await this._prisma.batchTaskRun.findFirst({
543562
where: { id: options.batchId },
544563
include: {

docker/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ services:
6161
- 6379:6379
6262

6363
electric:
64-
image: electricsql/electric:0.8.1
64+
image: electricsql/electric:0.9.4
6565
restart: always
6666
environment:
6767
DATABASE_URL: postgresql://postgres:postgres@database:5432/postgres?sslmode=disable

internal-packages/testcontainers/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export async function createElectricContainer(
5555
network.getName()
5656
)}:5432/${postgresContainer.getDatabase()}?sslmode=disable`;
5757

58-
const container = await new GenericContainer("electricsql/electric:0.8.1")
58+
const container = await new GenericContainer("electricsql/electric:0.9.4")
5959
.withExposedPorts(3000)
6060
.withNetwork(network)
6161
.withEnvironment({

packages/core/src/v3/apiClient/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export type ClientTriggerOptions = {
7474
export type ClientBatchTriggerOptions = ClientTriggerOptions & {
7575
idempotencyKey?: string;
7676
idempotencyKeyTTL?: string;
77+
processingStrategy?: "parallel" | "sequential";
7778
};
7879

7980
export type TriggerRequestOptions = ZodFetchOptions & {
@@ -239,6 +240,7 @@ export class ApiClient {
239240
headers: this.#getHeaders(clientOptions?.spanParentAsLink ?? false, {
240241
"idempotency-key": clientOptions?.idempotencyKey,
241242
"idempotency-key-ttl": clientOptions?.idempotencyKeyTTL,
243+
"batch-processing-strategy": clientOptions?.processingStrategy,
242244
}),
243245
body: JSON.stringify(body),
244246
},

packages/core/src/v3/types/tasks.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ export interface Task<TIdentifier extends string, TInput = void, TOutput = any>
592592
* ```
593593
*/
594594
batchTriggerAndWait: (
595-
items: Array<BatchTriggerAndWaitItem<TInput>>
595+
items: Array<BatchTriggerAndWaitItem<TInput>>,
596+
options?: BatchTriggerAndWaitOptions
596597
) => Promise<BatchResult<TIdentifier, TOutput>>;
597598
}
598599

@@ -781,6 +782,32 @@ export type TriggerAndWaitOptions = Omit<TriggerOptions, "idempotencyKey" | "ide
781782
export type BatchTriggerOptions = {
782783
idempotencyKey?: IdempotencyKey | string | string[];
783784
idempotencyKeyTTL?: string;
785+
786+
/**
787+
* When true, triggers tasks sequentially in batch order. This ensures ordering but may be slower,
788+
* especially for large batches.
789+
*
790+
* When false (default), triggers tasks in parallel for better performance, but order is not guaranteed.
791+
*
792+
* Note: This only affects the order of run creation, not the actual task execution.
793+
*
794+
* @default false
795+
*/
796+
triggerSequentially?: boolean;
797+
};
798+
799+
export type BatchTriggerAndWaitOptions = {
800+
/**
801+
* When true, triggers tasks sequentially in batch order. This ensures ordering but may be slower,
802+
* especially for large batches.
803+
*
804+
* When false (default), triggers tasks in parallel for better performance, but order is not guaranteed.
805+
*
806+
* Note: This only affects the order of run creation, not the actual task execution.
807+
*
808+
* @default false
809+
*/
810+
triggerSequentially?: boolean;
784811
};
785812

786813
export type TaskMetadataWithFunctions = TaskMetadata & {

0 commit comments

Comments
 (0)