How to Use the Workflow Engine Module

In this document, you’ll learn about the different methods in the Workflow Engine Module's service and how to use them.


Resolve Workflow Engine Module's Service#

In your workflow's step, you can resolve the Workflow Engine Module's service from the Medusa container:

Code
1import { Modules } from "@medusajs/framework/utils"2import { createStep } from "@medusajs/framework/workflows-sdk"3
4const step1 = createStep(5  "step-1",6  async ({}, { container }) => {7    const workflowEngineModuleService = container.resolve(8      Modules.WORKFLOW_ENGINE9    )10    11    // TODO use workflowEngineModuleService12  } 13)

This will resolve the service of the configured Workflow Engine Module, which is the In-Memory Workflow Engine Module by default.

You can then use the Workflow Engine Module's service's methods in the step. The rest of this guide details these methods.


setStepSuccess#

This method sets an async step in a currently-executing long-running workflow as successful. The workflow will then continue to the next step.

Example#

Code
1// other imports...2import {3  TransactionHandlerType,4} from "@medusajs/framework/utils"5
6await workflowEngineModuleService.setStepSuccess({7  idempotencyKey: {8    action: TransactionHandlerType.INVOKE,9    transactionId,10    stepId: "step-2",11    workflowId: "hello-world",12  },13  stepResponse: new StepResponse("Done!"),14  options: {15    container,16  },17})

Parameters#

idempotencyKeyobject
The details of the step to set as successful.
stepResponseStepResponse
Set the response of the step. This is similar to the response you return in a step's definition, but since the async step doesn't have a response, you set its response when changing its status.
optionsobject
Options to pass to the step.

setStepFailure#

This method sets an async step in a currently-executing long-running workflow as failed. The workflow will then stop executing and the compensation functions of the workflow's steps will be executed.

Example#

Code
1// other imports...2import {3  TransactionHandlerType,4} from "@medusajs/framework/utils"5
6await workflowEngineModuleService.setStepFailure({7  idempotencyKey: {8    action: TransactionHandlerType.INVOKE,9    transactionId,10    stepId: "step-2",11    workflowId: "hello-world",12  },13  stepResponse: new StepResponse("Failed!"),14  options: {15    container,16  },17})

Parameters#

idempotencyKeyobject
The details of the step to set as failed.
stepResponseStepResponse
Set the response of the step. This is similar to the response you return in a step's definition, but since the async step doesn't have a response, you set its response when changing its status.
optionsobject
Options to pass to the step.

subscribe#

This method subscribes to a workflow's events. You can use this method to listen to a long-running workflow's events and retrieve its result once it's done executing.

Refer to the Long-Running Workflows documentation to learn more.

Example#

Code
1const { transaction } = await helloWorldWorkflow(container).run()2
3const subscriptionOptions = {4  workflowId: "hello-world",5  transactionId: transaction.transactionId,6  subscriberId: "hello-world-subscriber",7}8
9await workflowEngineModuleService.subscribe({10  ...subscriptionOptions,11  subscriber: async (data) => {12    if (data.eventType === "onFinish") {13      console.log("Finished execution", data.result)14      // unsubscribe15      await workflowEngineModuleService.unsubscribe({16        ...subscriptionOptions,17        subscriberOrId: subscriptionOptions.subscriberId,18      })19    } else if (data.eventType === "onStepFailure") {20      console.log("Workflow failed", data.step)21    }22  },23})

Parameters#

subscriptionOptionsobject
The options for the subscription.

unsubscribe#

This method unsubscribes from a workflow's events. You can use this method to stop listening to a long-running workflow's events after you've received the result.

Example#

Code
1await workflowEngineModuleService.unsubscribe({2  workflowId: "hello-world",3  transactionId: "transaction-id",4  subscriberOrId: "hello-world-subscriber",5})

Parameters#

workflowIdstring
The ID of the workflow to unsubscribe from. This is the first parameter passed to createWorkflow when creating the workflow.
transactionIdstring
The ID of the workflow execution's transaction. This is returned when you execute a workflow.
subscriberOrIdstring
The subscriber ID or the subscriber function to unsubscribe from the workflow's events.
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break