chore(workflows-sdk): add idempotencyKey to shared context (#6860)

This commit is contained in:
Carlos R. L. Rodrigues
2024-03-28 17:51:29 +00:00
committed by GitHub
parent 412111e1ea
commit 45c49e89f2
5 changed files with 61 additions and 29 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"@medusajs/workflows-sdk": patch
"@medusajs/types": patch
---
Add workflow idempotencyKey to shared context
@@ -32,7 +32,7 @@ export type WorkflowHandler = Map<
{ invoke: WorkflowStepHandler; compensate?: WorkflowStepHandler }
>
export type WorkflowStepHandler = (args: {
export type WorkflowStepHandlerArguments = {
container: MedusaContainer
payload: unknown
invoke: { [actions: string]: unknown }
@@ -40,7 +40,11 @@ export type WorkflowStepHandler = (args: {
metadata: TransactionMetadata
transaction: DistributedTransaction
context?: Context
}) => unknown
}
export type WorkflowStepHandler = (
args: WorkflowStepHandlerArguments
) => unknown
export class WorkflowManager {
protected static workflows: Map<string, WorkflowDefinition> = new Map()
+5
View File
@@ -76,4 +76,9 @@ export type Context<TManager = unknown> = {
* A string indicating the ID of the current request.
*/
requestId?: string
/**
* A string indicating the idempotencyKey of the current workflow execution.
*/
idempotencyKey?: string
}
@@ -1,6 +1,7 @@
import {
TransactionStepsDefinition,
WorkflowManager,
WorkflowStepHandlerArguments,
} from "@medusajs/orchestration"
import { OrchestrationUtils, deepCopy, isString } from "@medusajs/utils"
import { ulid } from "ulid"
@@ -120,16 +121,20 @@ function applyStep<
}
const handler = {
invoke: async (transactionContext) => {
invoke: async (transactionContext: WorkflowStepHandlerArguments) => {
const metadata = transactionContext.metadata
const idempotencyKey = metadata.idempotency_key
transactionContext.context!.idempotencyKey = idempotencyKey
const executionContext: StepExecutionContext = {
workflowId: transactionContext.model_id,
stepName: transactionContext.action,
workflowId: metadata.model_id,
stepName: metadata.action,
action: "invoke",
idempotencyKey: transactionContext.idempotency_key,
attempt: transactionContext.attempt,
idempotencyKey,
attempt: metadata.attempt,
container: transactionContext.container,
metadata: transactionContext.metadata,
context: transactionContext.context,
metadata,
context: transactionContext.context!,
}
const argInput = input
@@ -151,19 +156,25 @@ function applyStep<
}
},
compensate: compensateFn
? async (transactionContext) => {
? async (transactionContext: WorkflowStepHandlerArguments) => {
const metadata = transactionContext.metadata
const idempotencyKey = metadata.idempotency_key
transactionContext.context!.idempotencyKey = idempotencyKey
const executionContext: StepExecutionContext = {
workflowId: transactionContext.model_id,
stepName: transactionContext.action,
workflowId: metadata.model_id,
stepName: metadata.action,
action: "compensate",
idempotencyKey: transactionContext.idempotency_key,
attempt: transactionContext.attempt,
idempotencyKey,
attempt: metadata.attempt,
container: transactionContext.container,
metadata: transactionContext.metadata,
context: transactionContext.context,
metadata,
context: transactionContext.context!,
}
const stepOutput = transactionContext.invoke[stepName]?.output
const stepOutput = (transactionContext.invoke[stepName] as any)
?.output
const invokeResult =
stepOutput?.__type ===
OrchestrationUtils.SymbolWorkflowStepResponse
@@ -1,4 +1,5 @@
import { OrchestrationUtils } from "@medusajs/utils"
import { WorkflowStepHandlerArguments } from "@medusajs/orchestration"
import { OrchestrationUtils, deepCopy } from "@medusajs/utils"
import { resolveValue } from "./helpers"
import {
CreateWorkflowComposerContext,
@@ -108,22 +109,27 @@ export function hook<TOutput>(
return hookBinder(name, function (context) {
return {
__value: async function (transactionContext) {
__value: async function (
transactionContext: WorkflowStepHandlerArguments
) {
const metadata = transactionContext.metadata
const idempotencyKey = metadata.idempotency_key
transactionContext.context!.idempotencyKey = idempotencyKey
const executionContext: StepExecutionContext = {
workflowId: transactionContext.model_id,
stepName: transactionContext.action,
action: transactionContext.action_type,
idempotencyKey: transactionContext.idempotency_key,
attempt: transactionContext.attempt,
workflowId: metadata.model_id,
stepName: metadata.action,
action: metadata.action_type,
idempotencyKey,
attempt: metadata.attempt,
container: transactionContext.container,
metadata: transactionContext.metadata,
context: transactionContext.context,
metadata,
context: transactionContext.context!,
}
const allValues = await resolveValue(value, transactionContext)
const stepValue = allValues
? JSON.parse(JSON.stringify(allValues))
: allValues
const stepValue = allValues ? deepCopy(allValues) : allValues
let finalResult
const functions = context.hooksCallback_[name]