bugfix: retrying tasks no longer incorrectly complete successfully
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@trigger.dev/sdk": patch
|
||||
---
|
||||
|
||||
Only use cached tasks if they are completed, otherwise retrying tasks will be considered successful
|
||||
@@ -477,7 +477,9 @@ export class PerformRunExecutionV2Service {
|
||||
}
|
||||
}
|
||||
|
||||
function prepareTasksForRun(tasks: FoundTask[]): CachedTask[] {
|
||||
function prepareTasksForRun(possibleTasks: FoundTask[]): CachedTask[] {
|
||||
const tasks = possibleTasks.filter((task) => task.status === "COMPLETED");
|
||||
|
||||
// We need to limit the cached tasks to not be too large >3.5MB when serialized
|
||||
const TOTAL_CACHED_TASK_BYTE_LIMIT = 3500000;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createExpressServer } from "@trigger.dev/express";
|
||||
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
|
||||
import { z } from "zod";
|
||||
|
||||
export const client = new TriggerClient({
|
||||
id: "job-catalog",
|
||||
@@ -56,4 +57,45 @@ client.defineJob({
|
||||
},
|
||||
});
|
||||
|
||||
client.defineJob({
|
||||
id: "example-job",
|
||||
name: "Example Job: a joke with a delay",
|
||||
version: "0.0.2",
|
||||
trigger: eventTrigger({
|
||||
name: "shayan.event",
|
||||
schema: z.object({
|
||||
userId: z.string(),
|
||||
delay: z.number(),
|
||||
}),
|
||||
}),
|
||||
run: async (payload, io, ctx) => {
|
||||
await io.wait("sleeping", payload.delay);
|
||||
|
||||
await io.runTask("init", { name: "init" }, async () => {
|
||||
console.log("init function ran", payload.userId);
|
||||
});
|
||||
|
||||
await io.runTask("failable", { name: "task-1", retry: { limit: 3 } }, async (task) => {
|
||||
if (task.attempts > 2) {
|
||||
console.log("task succeeded");
|
||||
return {
|
||||
ok: true,
|
||||
};
|
||||
}
|
||||
console.log("task failed");
|
||||
throw new Error(`Task failed on ${task.attempts} attempt(s)`);
|
||||
});
|
||||
|
||||
await io.runTask(
|
||||
"log",
|
||||
{
|
||||
name: "log",
|
||||
},
|
||||
async () => {
|
||||
console.log("hello from the job", payload.userId);
|
||||
}
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
createExpressServer(client);
|
||||
|
||||
@@ -504,8 +504,8 @@ export class IO {
|
||||
|
||||
const cachedTask = this._cachedTasks.get(idempotencyKey);
|
||||
|
||||
if (cachedTask) {
|
||||
this._logger.debug("Using cached task", {
|
||||
if (cachedTask && cachedTask.status === "COMPLETED") {
|
||||
this._logger.debug("Using completed cached task", {
|
||||
idempotencyKey,
|
||||
cachedTask,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user