- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
4.8.11. Long-Running Workflows
In this chapter, you’ll learn what a long-running workflow is and how to configure it.
What is a Long-Running Workflow?#
When you execute a workflow, you wait until the workflow finishes execution to receive the output.
A long-running workflow is a workflow that continues its execution in the background. You don’t receive its output immediately. Instead, you subscribe to the workflow execution to listen to status changes and receive its result once the execution is finished.
Why use Long-Running Workflows?#
Long-running workflows are useful if:
- A task takes too long. For example, you're importing data from a CSV file.
- The workflow's steps wait for an external action to finish before resuming execution. For example, before you import the data from the CSV file, you wait until the import is confirmed by the user.
Configure Long-Running Workflows#
A workflow is considered long-running if at least one step has its async
configuration set to true
and doesn't return a step response.
For example, consider the following workflow and steps:
10})11 12const step2 = createStep(13 {14 name: "step-2",15 async: true,16 },17 async () => {18 console.log("Waiting to be successful...")19 }20)21 22const step3 = createStep("step-3", async () => {23 return new StepResponse("Finished three steps")24})25 26const myWorkflow = createWorkflow(27 "hello-world", 28 function () {29 step1()30 step2()31 const message = step3()32 33 return new WorkflowResponse({34 message,35 })36})37 38export default myWorkflow
The second step has in its configuration object async
set to true
and it doesn't return a step response. This indicates that this step is an asynchronous step.
So, when you execute the hello-world
workflow, it continues its execution in the background once it reaches the second step.
Change Step Status#
Once the workflow's execution reaches an async step, it'll wait in the background for the step to succeed or fail before it moves to the next step.
To fail or succeed a step, use the Workflow Engine Module's main service that is registered in the Medusa Container under the Modules.WORKFLOW_ENGINE
(or workflowsModuleService
) key.
Retrieve Transaction ID#
Before changing the status of a workflow execution's async step, you must have the execution's transaction ID.
When you execute the workflow, the object returned has a transaction
property, which is an object that holds the details of the workflow execution's transaction. Use its transactionId
to later change async steps' statuses:
Change Step Status to Successful#
The Workflow Engine Module's main service has a setStepSuccess
method to set a step's status to successful. If you use it on a workflow execution's async step, the workflow continues execution to the next step.
For example, consider the following step:
8} from "@medusajs/framework/workflows-sdk"9 10type SetStepSuccessStepInput = {11 transactionId: string12};13 14export const setStepSuccessStep = createStep(15 "set-step-success-step",16 async function (17 { transactionId }: SetStepSuccessStepInput,18 { container }19 ) {20 const workflowEngineService = container.resolve(21 Modules.WORKFLOW_ENGINE22 )23 24 await workflowEngineService.setStepSuccess({25 idempotencyKey: {26 action: TransactionHandlerType.INVOKE,27 transactionId,28 stepId: "step-2",29 workflowId: "hello-world",30 },31 stepResponse: new StepResponse("Done!"),32 options: {33 container,