Skip to content

Completed batch waitpoints when we completed the BatchTaskRun #1945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 17, 2025
Merged
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
11 changes: 0 additions & 11 deletions apps/webapp/app/runEngine/services/batchTrigger.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,17 +571,6 @@ export class RunEngineBatchTriggerService extends WithRunEngine {

//triggered all the runs
if (updatedBatch.runIds.length === updatedBatch.runCount) {
//unblock the parent run from the batch
//this prevents the parent continuing before all the runs are created
if (parentRunId && resumeParentOnCompletion) {
await this._engine.unblockRunForCreatedBatch({
runId: RunId.fromFriendlyId(parentRunId),
batchId: batch.id,
environmentId: environment.id,
projectId: environment.projectId,
});
}

//if all the runs were idempotent, it's possible the batch is already completed
await this._engine.tryCompleteBatch({ batchId: batch.id });
}
Expand Down
38 changes: 1 addition & 37 deletions internal-packages/run-engine/src/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ export class RunEngine {

this.batchSystem = new BatchSystem({
resources,
waitpointSystem: this.waitpointSystem,
});

this.runAttemptSystem = new RunAttemptSystem({
Expand Down Expand Up @@ -905,43 +906,6 @@ export class RunEngine {
}
}

/**
* This is called when all the runs for a batch have been created.
* This does NOT mean that all the runs for the batch are completed.
*/
async unblockRunForCreatedBatch({
runId,
batchId,
tx,
}: {
runId: string;
batchId: string;
environmentId: string;
projectId: string;
tx?: PrismaClientOrTransaction;
}): Promise<void> {
const prisma = tx ?? this.prisma;

const waitpoint = await prisma.waitpoint.findFirst({
where: {
completedByBatchId: batchId,
},
});

if (!waitpoint) {
this.logger.error("RunEngine.unblockRunForBatch(): Waitpoint not found", {
runId,
batchId,
});
throw new ServiceValidationError("Waitpoint not found for batch", 404);
}

await this.completeWaitpoint({
id: waitpoint.id,
output: { value: "Batch waitpoint completed", isError: false },
});
}

async tryCompleteBatch({ batchId }: { batchId: string }): Promise<void> {
return this.batchSystem.scheduleCompleteBatch({ batchId });
}
Expand Down
30 changes: 28 additions & 2 deletions internal-packages/run-engine/src/engine/systems/batchSystem.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { startSpan } from "@internal/tracing";
import { isFinalRunStatus } from "../statuses.js";
import { SystemResources } from "./systems.js";
import { WaitpointSystem } from "./waitpointSystem.js";

export type BatchSystemOptions = {
resources: SystemResources;
waitpointSystem: WaitpointSystem;
};

export class BatchSystem {
private readonly $: SystemResources;
private readonly waitpointSystem: WaitpointSystem;

constructor(private readonly options: BatchSystemOptions) {
this.$ = options.resources;
this.waitpointSystem = options.waitpointSystem;
}

public async scheduleCompleteBatch({ batchId }: { batchId: string }): Promise<void> {
Expand All @@ -19,8 +23,8 @@ export class BatchSystem {
id: `tryCompleteBatch:${batchId}`,
job: "tryCompleteBatch",
payload: { batchId: batchId },
//2s in the future
availableAt: new Date(Date.now() + 2_000),
//200ms in the future
availableAt: new Date(Date.now() + 200),
});
}

Expand Down Expand Up @@ -75,6 +79,28 @@ export class BatchSystem {
status: "COMPLETED",
},
});

//get waitpoint (if there is one)
const waitpoint = await this.$.prisma.waitpoint.findFirst({
where: {
completedByBatchId: batchId,
},
});

if (!waitpoint) {
this.$.logger.debug(
"RunEngine.unblockRunForBatch(): Waitpoint not found. This is ok, because only batchTriggerAndWait has waitpoints",
{
batchId,
}
);
return;
}

await this.waitpointSystem.completeWaitpoint({
id: waitpoint.id,
output: { value: "Batch waitpoint completed", isError: false },
});
} else {
this.$.logger.debug("#tryCompleteBatch: Not all runs are completed", { batchId });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,6 @@ describe("RunEngine batchTriggerAndWait", () => {
expect(batchWaitpoint?.waitpoint.type).toBe("BATCH");
expect(batchWaitpoint?.waitpoint.completedByBatchId).toBe(batch.id);

await engine.unblockRunForCreatedBatch({
runId: parentRun.id,
batchId: batch.id,
environmentId: authenticatedEnvironment.id,
projectId: authenticatedEnvironment.projectId,
});

//dequeue and start the 1st child
const dequeuedChild = await engine.dequeueFromMasterQueue({
consumerId: "test_12345",
Expand Down Expand Up @@ -303,7 +296,7 @@ describe("RunEngine batchTriggerAndWait", () => {
expect(child2WaitpointAfter?.status).toBe("COMPLETED");
expect(child2WaitpointAfter?.output).toBe('{"baz":"qux"}');

await setTimeout(500);
await setTimeout(1_000);

const runWaitpointsAfterSecondChild = await prisma.taskRunWaitpoint.findMany({
where: {
Expand Down Expand Up @@ -497,13 +490,6 @@ describe("RunEngine batchTriggerAndWait", () => {
expect(parentAfterBatchChild.snapshot.executionStatus).toBe("EXECUTING_WITH_WAITPOINTS");
expect(parentAfterBatchChild.batch?.id).toBe(batch.id);

await engine.unblockRunForCreatedBatch({
runId: parentRun.id,
batchId: batch.id,
environmentId: authenticatedEnvironment.id,
projectId: authenticatedEnvironment.projectId,
});

//dequeue and start the batch child
const dequeuedBatchChild = await engine.dequeueFromMasterQueue({
consumerId: "test_12345",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1166,13 +1166,6 @@ describe("RunEngine checkpoints", () => {
expect(batchWaitpoint?.waitpoint.type).toBe("BATCH");
expect(batchWaitpoint?.waitpoint.completedByBatchId).toBe(batch.id);

await engine.unblockRunForCreatedBatch({
runId: parentRun.id,
batchId: batch.id,
environmentId: authenticatedEnvironment.id,
projectId: authenticatedEnvironment.projectId,
});

// Create a checkpoint
const checkpointResult = await engine.createCheckpoint({
runId: parentRun.id,
Expand Down