forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: document workflow retention (medusajs#11138)
- Loading branch information
1 parent
c1ae373
commit cdfde0d
Showing
4 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
www/apps/book/app/learn/fundamentals/workflows/store-executions/page.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { Prerequisites } from "docs-ui" | ||
|
||
export const metadata = { | ||
title: `${pageNumber} Store Workflow Executions`, | ||
} | ||
|
||
# {metadata.title} | ||
|
||
In this chapter, you'll learn how to store workflow executions in the database and access them later. | ||
|
||
## Workflow Execution Retention | ||
|
||
Medusa doesn't store your workflow's execution details by default. However, you can configure a workflow to keep its execution details stored in the database. | ||
|
||
This is useful for auditing and debugging purposes. When you store a workflow's execution, you can view details around its steps, their states and their output. You can also check whether the workflow or any of its steps failed. | ||
|
||
<Note title="Tip"> | ||
|
||
You can view stored workflow executions from the Medusa Admin dashboard by going to Settings -> Workflows. | ||
|
||
</Note> | ||
|
||
--- | ||
|
||
## How to Store Workflow's Executions? | ||
|
||
<Prerequisites | ||
items={[ | ||
{ | ||
text: "Redis Workflow Engine must be installed and configured.", | ||
link: "!resources!/architectural-modules/workflow-engine/redis" | ||
} | ||
]} | ||
/> | ||
|
||
`createWorkflow` from the Workflows SDK can accept an object as a first parameter to set the workflow's configuration. To enable storing a workflow's executions: | ||
|
||
- Enable the `store` option. If your workflow is a [Long-Running Workflow](../long-running-workflow/page.mdx), this option is enabled by default. | ||
- Set the `retentionTime` option to the number of milliseconds that the workflow execution should be stored in the database. | ||
|
||
For example: | ||
|
||
export const highlights = [ | ||
["15", "retentionTime", "The number of milliseconds that the workflow's executions should be stored in the database."], | ||
["16", "store", "Enable storing the workflow's executions in the database."], | ||
] | ||
|
||
```ts highlights={highlights} | ||
import { createStep, createWorkflow } from "@medusajs/framework/workflows-sdk" | ||
|
||
const step1 = createStep( | ||
{ | ||
name: "step-1" | ||
}, | ||
async () => { | ||
console.log("Hello from step 1") | ||
} | ||
) | ||
|
||
export const helloWorkflow = createWorkflow( | ||
{ | ||
name: "hello-workflow", | ||
retentionTime: 99999, | ||
store: true | ||
}, | ||
() => { | ||
step1() | ||
} | ||
) | ||
``` | ||
|
||
Whenever you execute the `helloWorkflow` now, its execution details will be stored in the database. | ||
|
||
--- | ||
|
||
## Retrieve Workflow Executions | ||
|
||
<Note title="Tip"> | ||
|
||
You can view stored workflow executions from the Medusa Admin dashboard by going to Settings -> Workflows. | ||
|
||
</Note> | ||
|
||
When you execute a workflow, the returned object has a `transaction` property containing the workflow execution's transaction details: | ||
|
||
```ts | ||
const { transaction } = await helloWorkflow(container).run() | ||
``` | ||
|
||
To retrieve a workflow's execution details from the database, resolve the Workflow Engine Module from the container and use its `listWorkflowExecutions` method. | ||
|
||
For example, you can create a `GET` API Route at `src/workflows/[id]/route.ts` that retrieves a workflow execution for the specified transaction ID: | ||
|
||
export const retrieveHighlights = [ | ||
["8", "transaction_id", "Get the transaction ID from a path parameter."], | ||
["10", "workflowEngineService", "Resolve the Workflow Engine Module from the container."], | ||
["14", "workflowExecution", "Retrieve the workflow execution for the specified transaction ID."], | ||
] | ||
|
||
```ts title="src/workflows/[id]/route.ts" highlights={retrieveHighlights} | ||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework"; | ||
import { Modules } from "@medusajs/framework/utils"; | ||
|
||
export async function GET( | ||
req: MedusaRequest, | ||
res: MedusaResponse | ||
) { | ||
const { transaction_id } = req.params | ||
|
||
const workflowEngineService = req.scope.resolve( | ||
Modules.WORKFLOW_ENGINE | ||
) | ||
|
||
const [workflowExecution] = await workflowEngineService.listWorkflowExecutions({ | ||
transaction_id: transaction_id | ||
}) | ||
|
||
res.json({ | ||
workflowExecution | ||
}) | ||
} | ||
``` | ||
|
||
In the above example, you resolve the Workflow Engine Module from the container and use its `listWorkflowExecutions` method, passing the `transaction_id` as a filter to retrieve its workflow execution details. | ||
|
||
A workflow execution object will be similar to the following: | ||
|
||
```json | ||
{ | ||
"workflow_id": "hello-workflow", | ||
"transaction_id": "01JJC2T6AVJCQ3N4BRD1EB88SP", | ||
"id": "wf_exec_01JJC2T6B3P76JD35F12QTTA78", | ||
"execution": { | ||
"state": "done", | ||
"steps": {}, | ||
"modelId": "hello-workflow", | ||
"options": {}, | ||
"metadata": {}, | ||
"startedAt": 1737719880027, | ||
"definition": {}, | ||
"timedOutAt": null, | ||
"hasAsyncSteps": false, | ||
"transactionId": "01JJC2T6AVJCQ3N4BRD1EB88SP", | ||
"hasFailedSteps": false, | ||
"hasSkippedSteps": false, | ||
"hasWaitingSteps": false, | ||
"hasRevertedSteps": false, | ||
"hasSkippedOnFailureSteps": false | ||
}, | ||
"context": { | ||
"data": {}, | ||
"errors": [] | ||
}, | ||
"state": "done", | ||
"created_at": "2025-01-24T09:58:00.036Z", | ||
"updated_at": "2025-01-24T09:58:00.046Z", | ||
"deleted_at": null | ||
} | ||
``` | ||
|
||
### Example: Check if Stored Workflow Execution Failed | ||
|
||
To check if a stored workflow execution failed, you can check its `state` property: | ||
|
||
```ts | ||
if (workflowExecution.state === "failed") { | ||
return res.status(500).json({ | ||
error: "Workflow failed" | ||
}) | ||
} | ||
``` | ||
|
||
Other state values include `done`, `invoking`, and `compensating`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters