feat(orchestration,workflows-sdk): Skip step (#8334)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-29 14:26:47 -03:00
committed by GitHub
parent e98012a858
commit 24c105f288
17 changed files with 272 additions and 33 deletions

View File

@@ -1,20 +1,20 @@
import { WorkflowManager, WorkflowScheduler } from "@medusajs/orchestration"
import {
ModuleRegistrationName,
composeMessage,
createMedusaContainer,
ModuleRegistrationName,
promiseAll,
} from "@medusajs/utils"
import { asValue } from "awilix"
import {
StepResponse,
createStep,
createWorkflow,
hook,
parallelize,
StepResponse,
transform,
} from ".."
import { MedusaWorkflow } from "../../../medusa-workflow"
import { asValue } from "awilix"
import { IDistributedSchedulerStorage, SchedulerOptions } from "../../dist"
jest.setTimeout(30000)
@@ -1113,6 +1113,82 @@ describe("Workflow composer", function () {
})
})
it("should compose a new workflow and skip steps depending on the input", async () => {
const mockStep1Fn = jest.fn().mockImplementation((input) => {
if (input === 1) {
return StepResponse.skip()
} else {
return new StepResponse({ obj: "return from 1" })
}
})
const mockStep2Fn = jest.fn().mockImplementation((input) => {
if (!input) {
return StepResponse.skip()
}
})
const mockStep3Fn = jest.fn().mockImplementation((inputs) => {
return new StepResponse({
inputs,
obj: "return from 3",
})
})
const step1 = createStep("step1", mockStep1Fn)
const step2 = createStep("step2", mockStep2Fn)
const step3 = createStep("step3", mockStep3Fn)
const workflow = createWorkflow("workflow1", function (input) {
const returnStep1 = step1(input)
const ret2 = step2(returnStep1)
return step3({ one: returnStep1, two: ret2, input })
})
const { result: workflowResult, transaction } = await workflow().run({
input: 1,
})
expect(transaction.getFlow().hasSkippedSteps).toBe(true)
expect(mockStep3Fn.mock.calls[0][0]).toEqual({
input: 1,
})
expect(workflowResult).toEqual({
inputs: {
input: 1,
},
obj: "return from 3",
})
const { result: workflowResultTwo, transaction: transactionTwo } =
await workflow().run({
input: "none",
})
expect(transactionTwo.getFlow().hasSkippedSteps).toBe(false)
expect(mockStep3Fn.mock.calls[1][0]).toEqual({
one: {
obj: "return from 1",
},
two: {
__type: "Symbol(WorkflowWorkflowData)",
},
input: "none",
})
expect(workflowResultTwo).toEqual({
inputs: {
one: {
obj: "return from 1",
},
two: {
__type: "Symbol(WorkflowWorkflowData)",
},
input: "none",
},
obj: "return from 3",
})
})
it("should compose two new workflows sequentially and execute them sequentially", async () => {
const mockStep1Fn = jest.fn().mockImplementation((input, context) => {
return new StepResponse({ inputs: [input], obj: "return from 1" })

View File

@@ -4,9 +4,9 @@ import {
WorkflowStepHandler,
WorkflowStepHandlerArguments,
} from "@medusajs/orchestration"
import { deepCopy, isString, OrchestrationUtils } from "@medusajs/utils"
import { OrchestrationUtils, deepCopy, isString } from "@medusajs/utils"
import { ulid } from "ulid"
import { resolveValue, StepResponse } from "./helpers"
import { StepResponse, resolveValue } from "./helpers"
import { proxify } from "./helpers/proxy"
import {
CreateWorkflowComposerContext,
@@ -353,7 +353,7 @@ function wrapConditionalStep(
}
if (!canContinue) {
return
return StepResponse.skip()
}
return await originalInvoke(stepArguments)

View File

@@ -1,5 +1,8 @@
import { PermanentStepFailureError } from "@medusajs/orchestration"
import { isDefined, OrchestrationUtils } from "@medusajs/utils"
import {
PermanentStepFailureError,
SkipStepResponse,
} from "@medusajs/orchestration"
import { OrchestrationUtils, isDefined } from "@medusajs/utils"
/**
* This class is used to create the response returned by a step. A step return its data by returning an instance of `StepResponse`.
@@ -116,6 +119,10 @@ export class StepResponse<TOutput, TCompensateInput = TOutput> {
throw new PermanentStepFailureError(message)
}
static skip(): SkipStepResponse {
return new SkipStepResponse()
}
/**
* @internal
*/

View File

@@ -50,6 +50,7 @@ export function when(input, condition) {
}
delete global[OrchestrationUtils.SymbolMedusaWorkflowComposerCondition]
return ret
},
}