It’s recommended to place all logging statements inside workflow steps.
Otherwise, they may be executed multiple times.
Our workflow engine runs each step through a separate request to your endpoint.
This isolates the execution of each step and enables the features the engine provides like retries.
However, it also means the route handler may run multiple times, skipping previously completed steps on each request.
import { serve } from "@upstash/workflow/nextjs";
export const { POST } = serve(
async (context) => {
await context.run("save-user-to-db", async () => {
// 👇 This log will appear only once
console.log("Succesfully created user in the database")
})
// 👇 This log will appear multiple times
console.log("Succesfully created user in the database")
// ...
}
);
export const { POST } = serve((context) => {
})
A built-in solution for this is on our roadmap. We plan to add context.log(), which will guarantee that a message is logged only once, even when used outside of steps.
Until then, the pattern above is the recommended workaround.