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:
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#
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#
idempotencyKey
objectThe details of the step to set as successful.
idempotencyKey
objectstepResponse
StepResponseoptions
objectOptions to pass to the step.
options
objectsetStepFailure#
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#
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#
idempotencyKey
objectThe details of the step to set as failed.
idempotencyKey
objectstepResponse
StepResponseoptions
objectOptions to pass to the step.
options
objectsubscribe#
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#
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#
subscriptionOptions
objectThe options for the subscription.
subscriptionOptions
objectunsubscribe#
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#
Parameters#
workflowId
stringcreateWorkflow
when creating the workflow.transactionId
stringsubscriberOrId
string