fix(workflows-sdk): name for when/then step (#10459)

This commit is contained in:
Carlos R. L. Rodrigues
2024-12-05 15:47:42 -03:00
committed by GitHub
parent 7ff3f15d6d
commit 90ae187e09
14 changed files with 234 additions and 50 deletions

View File

@@ -529,8 +529,6 @@ function attachOnFinishReleaseEvents(
)
}
await onFinish?.(args)
const eventBusService = (
flow.container as MedusaContainer
).resolve<IEventBusModuleService>(Modules.EVENT_BUS, {
@@ -538,6 +536,7 @@ function attachOnFinishReleaseEvents(
})
if (!eventBusService || !flowEventGroupId) {
await onFinish?.(args)
return
}
@@ -553,14 +552,19 @@ function attachOnFinishReleaseEvents(
})
}
await eventBusService.releaseGroupedEvents(flowEventGroupId).catch((e) => {
logger.error(
`Failed to release grouped events for eventGroupId: ${flowEventGroupId}`,
e
)
await eventBusService
.releaseGroupedEvents(flowEventGroupId)
.then(async () => {
await onFinish?.(args)
})
.catch((e) => {
logger.error(
`Failed to release grouped events for eventGroupId: ${flowEventGroupId}`,
e
)
return flow.cancel(transaction)
})
return flow.cancel(transaction)
})
}
events.onFinish = wrappedOnFinish

View File

@@ -243,8 +243,9 @@ describe("Workflow composer", () => {
return new StepResponse({ result: input })
})
const wfId = getNewWorkflowId()
const subWorkflow = createWorkflow(
getNewWorkflowId(),
wfId,
function (input: WorkflowData<string>) {
childWorkflowStep1()
return new WorkflowResponse(childWorkflowStep2(input))
@@ -269,7 +270,9 @@ describe("Workflow composer", () => {
expect(result).toEqual({ result: "hi from outside" })
expect(parentContext.transactionId).toEqual(expect.any(String))
expect(parentContext.transactionId).toEqual(childContext.transactionId)
expect(childContext.transactionId).toEqual(
wfId + "-as-step-" + parentContext.transactionId
)
expect(parentContext.eventGroupId).toEqual("eventGroupId")
expect(parentContext.eventGroupId).toEqual(childContext.eventGroupId)
@@ -293,8 +296,9 @@ describe("Workflow composer", () => {
return new StepResponse({ result: input })
})
const wfId = getNewWorkflowId()
const subWorkflow = createWorkflow(
getNewWorkflowId(),
wfId,
function (input: WorkflowData<string>) {
childWorkflowStep1()
return new WorkflowResponse(childWorkflowStep2(input))
@@ -315,7 +319,9 @@ describe("Workflow composer", () => {
expect(result).toEqual({ result: "hi from outside" })
expect(parentContext.transactionId).toBeTruthy()
expect(parentContext.transactionId).toEqual(childContext.transactionId)
expect(childContext.transactionId).toEqual(
wfId + "-as-step-" + parentContext.transactionId
)
expect(parentContext.eventGroupId).toBeTruthy()
expect(parentContext.eventGroupId).toEqual(childContext.eventGroupId)

View File

@@ -194,8 +194,9 @@ export function createWorkflow<TData, TResult, THooks extends any[]>(
input: stepInput as any,
container,
context: {
transactionId: ulid(),
...sharedContext,
transactionId:
step.__step__ + "-" + (stepContext.transactionId ?? ulid()),
parentStepIdempotencyKey: stepContext.idempotencyKey,
},
})

View File

@@ -1,4 +1,4 @@
import { OrchestrationUtils } from "@medusajs/utils"
import { isDefined, OrchestrationUtils } from "@medusajs/utils"
import { ulid } from "ulid"
import { createStep } from "./create-step"
import { StepResponse } from "./helpers/step-response"
@@ -26,7 +26,26 @@ export function when<T extends object | WorkflowData, Then extends Function>(
then: ThenFunc
}
export function when(input, condition) {
export function when<T extends object | WorkflowData, Then extends Function>(
name: string,
values: T,
condition: ConditionFunction<T>
): {
then: ThenFunc
}
export function when(...args) {
let [name, input, condition] = args
if (args.length === 2) {
condition = input
input = name
name = undefined
}
if (typeof condition !== "function") {
throw new Error(`"when condition" must be a function`)
}
global[OrchestrationUtils.SymbolMedusaWorkflowComposerCondition] = {
input,
condition,
@@ -49,9 +68,23 @@ export function when(input, condition) {
const applyCondition =
global[OrchestrationUtils.SymbolMedusaWorkflowComposerCondition].steps
if (ret?.__type !== OrchestrationUtils.SymbolWorkflowStep) {
if (
isDefined(ret) &&
ret?.__type !== OrchestrationUtils.SymbolWorkflowStep
) {
if (!isDefined(name)) {
name = "when-then-" + ulid()
const context =
global[OrchestrationUtils.SymbolMedusaWorkflowComposerContext]
console.warn(
`${context.workflowId}: "when" name should be defined. A random one will be assigned to it, which is not recommended for production.\n`,
condition.toString()
)
}
const retStep = createStep(
"when-then-" + ulid(),
name,
({ input }: { input: any }) => new StepResponse(input)
)
returnStep = retStep({ input: ret })