Files
Eric Allam 14dcc76f93 feat: run.ctx tidying and additions (#2322)
* Cleanup context and execution creation, cache stuff, add parent and root task run ids

* more efficient by using friendly IDs instead of doing joins

* metadata.root/parent now reference current run when run has no root/parent

* Adding changeset

* try to make test less flaky

* Clean imports

* Another attempt to fix the flaky test

* Fix usage by still passing durationMs and costInCents to the execution, just not the run.ctx
2025-07-30 13:47:12 +01:00

36 lines
814 B
TypeScript

import { logger, task, wait, usage } from "@trigger.dev/sdk";
import { setTimeout } from "timers/promises";
export const usageExampleTask = task({
id: "usage-example",
retry: {
maxAttempts: 3,
minTimeoutInMs: 500,
maxTimeoutInMs: 1000,
factor: 1.5,
},
run: async (payload: { throwError: boolean }, { ctx }) => {
logger.info("run.ctx", { ctx });
await setTimeout(1000);
const currentUsage = usage.getCurrent();
logger.info("currentUsage", { currentUsage });
if (payload.throwError && ctx.attempt.number === 1) {
throw new Error("Forced error to cause a retry");
}
await setTimeout(5000);
const currentUsage2 = usage.getCurrent();
logger.info("currentUsage2", { currentUsage2 });
return {
message: "Hello, world!",
};
},
});