fix(workflow-engine-*): subscribe response and error (#6869)
This commit is contained in:
1 parent
d97af91a8d
commit
e603726985
6 files changed
+66
-42
No files matched your search
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/workflow-engine-inmemory": patch
|
||||||
|
"@medusajs/workflow-engine-redis": patch
|
||||||
|
"@medusajs/workflows-sdk": patch
|
||||||
|
"@medusajs/orchestration": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix Workflow Engine subscribers response and error
|
||||||
@@ -631,21 +631,30 @@ export class TransactionOrchestrator extends EventEmitter {
|
|||||||
transaction,
|
transaction,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const isAsync = step.isCompensating()
|
||||||
|
? step.definition.compensateAsync
|
||||||
|
: step.definition.async
|
||||||
|
|
||||||
const setStepFailure = async (
|
const setStepFailure = async (
|
||||||
error: Error | any,
|
error: Error | any,
|
||||||
{ endRetry }: { endRetry?: boolean } = {}
|
{ endRetry }: { endRetry?: boolean } = {}
|
||||||
) => {
|
) => {
|
||||||
return TransactionOrchestrator.setStepFailure(
|
const ret = TransactionOrchestrator.setStepFailure(
|
||||||
transaction,
|
transaction,
|
||||||
step,
|
step,
|
||||||
error,
|
error,
|
||||||
endRetry ? 0 : step.definition.maxRetries
|
endRetry ? 0 : step.definition.maxRetries
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (isAsync) {
|
||||||
|
await transaction.scheduleRetry(
|
||||||
|
step,
|
||||||
|
step.definition.retryInterval ?? 0
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const isAsync = step.isCompensating()
|
return ret
|
||||||
? step.definition.compensateAsync
|
}
|
||||||
: step.definition.async
|
|
||||||
|
|
||||||
if (!isAsync) {
|
if (!isAsync) {
|
||||||
hasSyncSteps = true
|
hasSyncSteps = true
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import {
|
|||||||
TransactionStep,
|
TransactionStep,
|
||||||
} from "@medusajs/orchestration"
|
} from "@medusajs/orchestration"
|
||||||
import { ContainerLike, Context, MedusaContainer } from "@medusajs/types"
|
import { ContainerLike, Context, MedusaContainer } from "@medusajs/types"
|
||||||
import { InjectSharedContext, isString, MedusaContext } from "@medusajs/utils"
|
import { InjectSharedContext, MedusaContext, isString } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
type FlowRunOptions,
|
|
||||||
MedusaWorkflow,
|
MedusaWorkflow,
|
||||||
ReturnWorkflow,
|
ReturnWorkflow,
|
||||||
|
resolveValue,
|
||||||
|
type FlowRunOptions,
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
import { ulid } from "ulid"
|
import { ulid } from "ulid"
|
||||||
import { InMemoryDistributedTransactionStorage } from "../utils"
|
import { InMemoryDistributedTransactionStorage } from "../utils"
|
||||||
@@ -462,31 +463,40 @@ export class WorkflowOrchestratorService {
|
|||||||
|
|
||||||
notify({ eventType: "onStepBegin", step })
|
notify({ eventType: "onStepBegin", step })
|
||||||
},
|
},
|
||||||
onStepSuccess: ({ step, transaction }) => {
|
onStepSuccess: async ({ step, transaction }) => {
|
||||||
const response = transaction.getContext().invoke[step.id]
|
const stepName = step.definition.action!
|
||||||
|
const response = await resolveValue(
|
||||||
|
transaction.getContext().invoke[stepName],
|
||||||
|
transaction
|
||||||
|
)
|
||||||
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
|
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
|
||||||
|
|
||||||
notify({ eventType: "onStepSuccess", step, response })
|
notify({ eventType: "onStepSuccess", step, response })
|
||||||
},
|
},
|
||||||
onStepFailure: ({ step, transaction }) => {
|
onStepFailure: ({ step, transaction }) => {
|
||||||
const errors = transaction.getErrors(TransactionHandlerType.INVOKE)[
|
const stepName = step.definition.action!
|
||||||
step.id
|
const errors = transaction
|
||||||
]
|
.getErrors(TransactionHandlerType.INVOKE)
|
||||||
|
.filter((err) => err.action === stepName)
|
||||||
|
|
||||||
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
|
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
|
||||||
|
|
||||||
notify({ eventType: "onStepFailure", step, errors })
|
notify({ eventType: "onStepFailure", step, errors })
|
||||||
},
|
},
|
||||||
|
|
||||||
onCompensateStepSuccess: ({ step, transaction }) => {
|
onCompensateStepSuccess: ({ step, transaction }) => {
|
||||||
const response = transaction.getContext().compensate[step.id]
|
const stepName = step.definition.action!
|
||||||
|
const response = transaction.getContext().compensate[stepName]
|
||||||
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
|
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
|
||||||
|
|
||||||
notify({ eventType: "onCompensateStepSuccess", step, response })
|
notify({ eventType: "onCompensateStepSuccess", step, response })
|
||||||
},
|
},
|
||||||
onCompensateStepFailure: ({ step, transaction }) => {
|
onCompensateStepFailure: ({ step, transaction }) => {
|
||||||
const errors = transaction.getErrors(TransactionHandlerType.COMPENSATE)[
|
const stepName = step.definition.action!
|
||||||
step.id
|
const errors = transaction
|
||||||
]
|
.getErrors(TransactionHandlerType.COMPENSATE)
|
||||||
|
.filter((err) => err.action === stepName)
|
||||||
|
|
||||||
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
|
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
|
||||||
|
|
||||||
notify({ eventType: "onCompensateStepFailure", step, errors })
|
notify({ eventType: "onCompensateStepFailure", step, errors })
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
FlowRunOptions,
|
FlowRunOptions,
|
||||||
MedusaWorkflow,
|
MedusaWorkflow,
|
||||||
ReturnWorkflow,
|
ReturnWorkflow,
|
||||||
|
resolveValue,
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
import Redis from "ioredis"
|
import Redis from "ioredis"
|
||||||
import { ulid } from "ulid"
|
import { ulid } from "ulid"
|
||||||
@@ -512,30 +513,39 @@ export class WorkflowOrchestratorService {
|
|||||||
await notify({ eventType: "onStepBegin", step })
|
await notify({ eventType: "onStepBegin", step })
|
||||||
},
|
},
|
||||||
onStepSuccess: async ({ step, transaction }) => {
|
onStepSuccess: async ({ step, transaction }) => {
|
||||||
const response = transaction.getContext().invoke[step.id]
|
const stepName = step.definition.action!
|
||||||
|
const response = await resolveValue(
|
||||||
|
transaction.getContext().invoke[stepName],
|
||||||
|
transaction
|
||||||
|
)
|
||||||
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
|
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
|
||||||
|
|
||||||
await notify({ eventType: "onStepSuccess", step, response })
|
await notify({ eventType: "onStepSuccess", step, response })
|
||||||
},
|
},
|
||||||
onStepFailure: async ({ step, transaction }) => {
|
onStepFailure: async ({ step, transaction }) => {
|
||||||
const errors = transaction.getErrors(TransactionHandlerType.INVOKE)[
|
const stepName = step.definition.action!
|
||||||
step.id
|
const errors = transaction
|
||||||
]
|
.getErrors(TransactionHandlerType.INVOKE)
|
||||||
|
.filter((err) => err.action === stepName)
|
||||||
|
|
||||||
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
|
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
|
||||||
|
|
||||||
await notify({ eventType: "onStepFailure", step, errors })
|
await notify({ eventType: "onStepFailure", step, errors })
|
||||||
},
|
},
|
||||||
|
|
||||||
onCompensateStepSuccess: async ({ step, transaction }) => {
|
onCompensateStepSuccess: async ({ step, transaction }) => {
|
||||||
const response = transaction.getContext().compensate[step.id]
|
const stepName = step.definition.action!
|
||||||
|
const response = transaction.getContext().compensate[stepName]
|
||||||
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
|
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
|
||||||
|
|
||||||
await notify({ eventType: "onCompensateStepSuccess", step, response })
|
await notify({ eventType: "onCompensateStepSuccess", step, response })
|
||||||
},
|
},
|
||||||
onCompensateStepFailure: async ({ step, transaction }) => {
|
onCompensateStepFailure: async ({ step, transaction }) => {
|
||||||
const errors = transaction.getErrors(TransactionHandlerType.COMPENSATE)[
|
const stepName = step.definition.action!
|
||||||
step.id
|
const errors = transaction
|
||||||
]
|
.getErrors(TransactionHandlerType.COMPENSATE)
|
||||||
|
.filter((err) => err.action === stepName)
|
||||||
|
|
||||||
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
|
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
|
||||||
|
|
||||||
await notify({ eventType: "onCompensateStepFailure", step, errors })
|
await notify({ eventType: "onCompensateStepFailure", step, errors })
|
||||||
|
|||||||
@@ -99,22 +99,6 @@ export class RedisDistributedTransactionStorage extends DistributedTransactionSt
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/*private stringifyWithSymbol(key, value) {
|
|
||||||
if (key === "__type" && typeof value === "symbol") {
|
|
||||||
return Symbol.keyFor(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
private jsonWithSymbol(key, value) {
|
|
||||||
if (key === "__type" && typeof value === "string") {
|
|
||||||
return Symbol.for(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}*/
|
|
||||||
|
|
||||||
async get(key: string): Promise<TransactionCheckpoint | undefined> {
|
async get(key: string): Promise<TransactionCheckpoint | undefined> {
|
||||||
const data = await this.redisClient.get(key)
|
const data = await this.redisClient.get(key)
|
||||||
|
|
||||||
@@ -276,7 +260,7 @@ export class RedisDistributedTransactionStorage extends DistributedTransactionSt
|
|||||||
const key = [type, transaction.modelId, transaction.transactionId]
|
const key = [type, transaction.modelId, transaction.transactionId]
|
||||||
|
|
||||||
if (step) {
|
if (step) {
|
||||||
key.push(step.id)
|
key.push(step.id, step.attempts + "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return key.join(":")
|
return key.join(":")
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ async function resolveProperty(property, transactionContext) {
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export async function resolveValue(input, transactionContext) {
|
export async function resolveValue(input, transactionContext) {
|
||||||
const copiedInput = deepCopy(input)
|
|
||||||
|
|
||||||
const unwrapInput = async (
|
const unwrapInput = async (
|
||||||
inputTOUnwrap: Record<string, unknown>,
|
inputTOUnwrap: Record<string, unknown>,
|
||||||
parentRef: any
|
parentRef: any
|
||||||
@@ -66,6 +64,11 @@ export async function resolveValue(input, transactionContext) {
|
|||||||
return parentRef
|
return parentRef
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const copiedInput =
|
||||||
|
input?.__type === OrchestrationUtils.SymbolWorkflowWorkflowData
|
||||||
|
? deepCopy(input.output)
|
||||||
|
: deepCopy(input)
|
||||||
|
|
||||||
const result = copiedInput?.__type
|
const result = copiedInput?.__type
|
||||||
? await resolveProperty(copiedInput, transactionContext)
|
? await resolveProperty(copiedInput, transactionContext)
|
||||||
: await unwrapInput(copiedInput, {})
|
: await unwrapInput(copiedInput, {})
|
||||||
|
|||||||
Reference in new issue
Block a user