Skip to content

Commit 916a353

Browse files
committed
bugfix: retrying tasks no longer incorrectly complete successfully
1 parent 699878a commit 916a353

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

.changeset/dry-spoons-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
Only use cached tasks if they are completed, otherwise retrying tasks will be considered successful

apps/webapp/app/services/runs/performRunExecutionV2.server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ export class PerformRunExecutionV2Service {
477477
}
478478
}
479479

480-
function prepareTasksForRun(tasks: FoundTask[]): CachedTask[] {
480+
function prepareTasksForRun(possibleTasks: FoundTask[]): CachedTask[] {
481+
const tasks = possibleTasks.filter((task) => task.status === "COMPLETED");
482+
481483
// We need to limit the cached tasks to not be too large >3.5MB when serialized
482484
const TOTAL_CACHED_TASK_BYTE_LIMIT = 3500000;
483485

examples/job-catalog/src/events.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createExpressServer } from "@trigger.dev/express";
22
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
3+
import { z } from "zod";
34

45
export const client = new TriggerClient({
56
id: "job-catalog",
@@ -56,4 +57,45 @@ client.defineJob({
5657
},
5758
});
5859

60+
client.defineJob({
61+
id: "example-job",
62+
name: "Example Job: a joke with a delay",
63+
version: "0.0.2",
64+
trigger: eventTrigger({
65+
name: "shayan.event",
66+
schema: z.object({
67+
userId: z.string(),
68+
delay: z.number(),
69+
}),
70+
}),
71+
run: async (payload, io, ctx) => {
72+
await io.wait("sleeping", payload.delay);
73+
74+
await io.runTask("init", { name: "init" }, async () => {
75+
console.log("init function ran", payload.userId);
76+
});
77+
78+
await io.runTask("failable", { name: "task-1", retry: { limit: 3 } }, async (task) => {
79+
if (task.attempts > 2) {
80+
console.log("task succeeded");
81+
return {
82+
ok: true,
83+
};
84+
}
85+
console.log("task failed");
86+
throw new Error(`Task failed on ${task.attempts} attempt(s)`);
87+
});
88+
89+
await io.runTask(
90+
"log",
91+
{
92+
name: "log",
93+
},
94+
async () => {
95+
console.log("hello from the job", payload.userId);
96+
}
97+
);
98+
},
99+
});
100+
59101
createExpressServer(client);

packages/trigger-sdk/src/io.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ export class IO {
504504

505505
const cachedTask = this._cachedTasks.get(idempotencyKey);
506506

507-
if (cachedTask) {
508-
this._logger.debug("Using cached task", {
507+
if (cachedTask && cachedTask.status === "COMPLETED") {
508+
this._logger.debug("Using completed cached task", {
509509
idempotencyKey,
510510
cachedTask,
511511
});

0 commit comments

Comments
 (0)