From fdd825c9cf7aadd680fe0dc9ed96f1c951214a70 Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Mon, 10 Jun 2024 15:06:03 +0100 Subject: [PATCH] Add back in the v2 timeout task thing --- .../runs/performRunExecutionV3.server.ts | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/apps/webapp/app/services/runs/performRunExecutionV3.server.ts b/apps/webapp/app/services/runs/performRunExecutionV3.server.ts index fb0aa7279..7589177a7 100644 --- a/apps/webapp/app/services/runs/performRunExecutionV3.server.ts +++ b/apps/webapp/app/services/runs/performRunExecutionV3.server.ts @@ -229,6 +229,7 @@ export class PerformRunExecutionV3Service { }); } + const taskCount = await getTaskCountForRun(this.#prismaClient, run.id); const tasks = await getCompletedTasksForRun(this.#prismaClient, run.id); const sourceContext = RunSourceContextSchema.safeParse(run.event.sourceContext); @@ -428,7 +429,8 @@ export class PerformRunExecutionV3Service { this.#prismaClient, run, input, - durationInMs + durationInMs, + taskCount ); } else { return await this.#failRunExecutionWithRetry( @@ -1075,7 +1077,8 @@ export class PerformRunExecutionV3Service { prisma: PrismaClientOrTransaction, run: FoundRun, input: PerformRunExecutionV3Input, - durationInMs: number + durationInMs: number, + existingTaskCount: number ) { await $transaction(prisma, async (tx) => { const executionDuration = run.executionDuration + durationInMs; @@ -1096,6 +1099,47 @@ export class PerformRunExecutionV3Service { return; } + const newTaskCount = await getTaskCountForRun(tx, run.id); + + if (newTaskCount === existingTaskCount) { + const latestTask = await tx.task.findFirst({ + select: { + id: true, + name: true, + status: true, + displayKey: true, + }, + where: { + runId: run.id, + status: "RUNNING", + }, + orderBy: { + createdAt: "desc", + }, + take: 1, + }); + + const cause = + latestTask?.status === "RUNNING" + ? `This is likely caused by task "${ + latestTask.displayKey ?? latestTask.name + }" execution exceeding the function timeout` + : "This is likely caused by executing code outside of a task that exceeded the function timeout"; + + await this.#failRunExecution( + tx, + run, + { + message: `Function timeout detected in ${ + durationInMs / 1000.0 + }s without any task creation. This is unexpected behavior and could lead to an infinite execution error because the run will never finish. ${cause}`, + }, + "TIMED_OUT", + durationInMs + ); + return; + } + await tx.jobRun.update({ where: { id: run.id, @@ -1217,6 +1261,14 @@ function prepareNoOpTasksBloomFilter(possibleTasks: FoundTask[]): string { return filter.serialize(); } +async function getTaskCountForRun(prisma: PrismaClientOrTransaction, runId: string) { + return await prisma.task.count({ + where: { + runId, + }, + }); +} + async function getCompletedTasksForRun(prisma: PrismaClientOrTransaction, runId: string) { return await prisma.task.findMany({ where: {