Only retry runs when the response is a non-400
This commit is contained in:
@@ -285,6 +285,7 @@ async function safeFetch(url: string, options: RequestInit) {
|
||||
} catch (error) {
|
||||
logger.debug("Error while trying to connect to endpoint", {
|
||||
url,
|
||||
error,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import { safeJsonZodParse } from "~/utils/json";
|
||||
import { EndpointApi } from "../endpointApi.server";
|
||||
import { workerQueue } from "../worker.server";
|
||||
import { formatError } from "~/utils/formatErrors.server";
|
||||
import { logger } from "../logger.server";
|
||||
|
||||
type FoundRunExecution = NonNullable<
|
||||
Awaited<ReturnType<typeof findRunExecution>>
|
||||
@@ -290,22 +291,47 @@ export class PerformRunExecutionService {
|
||||
|
||||
if (!response) {
|
||||
return await this.#failRunExecutionWithRetry(execution, {
|
||||
message: "Could not connect to the endpoint",
|
||||
message: `Connection could not be established to the endpoint (${run.endpoint.url})`,
|
||||
});
|
||||
}
|
||||
|
||||
const rawBody = await response.text();
|
||||
|
||||
if (!response.ok) {
|
||||
logger.debug("Endpoint responded with non-200 status code", {
|
||||
status: response.status,
|
||||
runId: run.id,
|
||||
endpoint: run.endpoint.url,
|
||||
});
|
||||
|
||||
const errorBody = safeJsonZodParse(errorParser, rawBody);
|
||||
|
||||
if (errorBody && errorBody.success) {
|
||||
return await this.#failRunExecutionWithRetry(execution, errorBody.data);
|
||||
// Only retry if the error isn't a 4xx
|
||||
if (response.status >= 400 && response.status <= 499) {
|
||||
return await this.#failRunExecution(
|
||||
this.#prismaClient,
|
||||
execution,
|
||||
errorBody.data
|
||||
);
|
||||
} else {
|
||||
return await this.#failRunExecutionWithRetry(
|
||||
execution,
|
||||
errorBody.data
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return await this.#failRunExecutionWithRetry(execution, {
|
||||
message: `Endpoint responded with ${response.status} status code`,
|
||||
});
|
||||
// Only retry if the error isn't a 4xx
|
||||
if (response.status >= 400 && response.status <= 499) {
|
||||
return await this.#failRunExecution(this.#prismaClient, execution, {
|
||||
message: `Endpoint responded with ${response.status} status code`,
|
||||
});
|
||||
} else {
|
||||
return await this.#failRunExecutionWithRetry(execution, {
|
||||
message: `Endpoint responded with ${response.status} status code`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const safeBody = safeJsonZodParse(parser, rawBody);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { client } from "@/trigger";
|
||||
import { Job, eventTrigger } from "@trigger.dev/sdk";
|
||||
import { z } from "zod";
|
||||
|
||||
new Job(client, {
|
||||
id: "test-long-running-cpu",
|
||||
name: "Test long running CPU",
|
||||
version: "0.0.1",
|
||||
trigger: eventTrigger({
|
||||
name: "test.cpu",
|
||||
schema: z.object({
|
||||
iterations: z.number(),
|
||||
sleepDuration: z.number(),
|
||||
}),
|
||||
}),
|
||||
run: async (payload, io, ctx) => {
|
||||
console.log(`Running run ${ctx.run.id} at ${new Date().toISOString()}`);
|
||||
|
||||
for (let i = 0; i < payload.iterations ?? 1; i++) {
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, payload.sleepDuration ?? 1000)
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`Finishing run ${ctx.run.id} at ${new Date().toISOString()}`);
|
||||
},
|
||||
});
|
||||
@@ -10,6 +10,7 @@ import "@/jobs/resend";
|
||||
import "@/jobs/schedules";
|
||||
import "@/jobs/slack";
|
||||
import "@/jobs/typeform";
|
||||
import "@/jobs/edgeCases";
|
||||
|
||||
import { createPagesRoute } from "@trigger.dev/nextjs";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user