feat: run nested async workflows (#9119)

This commit is contained in:
Carlos R. L. Rodrigues authored and GitHub committed 2024-09-16 13:06:45 +00:00
1 parent 0bcdcccbe2
commit ef8dc4087e
23 files changed
+295 -100

No files matched your search

@@ -165,6 +165,8 @@ export const workflowEngineTestSuite = (
acknowledgement: { acknowledgement: {
transactionId: "trx_123", transactionId: "trx_123",
workflowId: "my-workflow-name", workflowId: "my-workflow-name",
hasFailed: false,
hasFinished: false,
}, },
}) })
@@ -11,11 +11,11 @@ import {
ProductVariantWorkflowEvents, ProductVariantWorkflowEvents,
} from "@medusajs/utils" } from "@medusajs/utils"
import { import {
WorkflowData,
WorkflowResponse,
createHook, createHook,
createWorkflow, createWorkflow,
transform, transform,
WorkflowData,
WorkflowResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { emitEventStep } from "../../common" import { emitEventStep } from "../../common"
import { createLinksWorkflow } from "../../common/workflows/create-links" import { createLinksWorkflow } from "../../common/workflows/create-links"
@@ -97,7 +97,7 @@ const buildLinksToCreate = (data: {
const linksToCreate: LinkDefinition[] = [] const linksToCreate: LinkDefinition[] = []
validateVariantsDuplicateInventoryItemIds( validateVariantsDuplicateInventoryItemIds(
data.createdVariants.map((variant, index) => { (data.createdVariants ?? []).map((variant, index) => {
const variantInput = data.input.product_variants[index] const variantInput = data.input.product_variants[index]
const inventoryItems = variantInput.inventory_items || [] const inventoryItems = variantInput.inventory_items || []
@@ -795,7 +795,10 @@ export class TransactionOrchestrator extends EventEmitter {
this this
) )
.then(async (response: any) => { .then(async (response: any) => {
if (!step.definition.backgroundExecution) { if (
!step.definition.backgroundExecution ||
step.definition.nested
) {
const eventName = DistributedTransactionEvent.STEP_AWAITING const eventName = DistributedTransactionEvent.STEP_AWAITING
transaction.emit(eventName, { step, transaction }) transaction.emit(eventName, { step, transaction })
@@ -825,6 +828,7 @@ export class TransactionOrchestrator extends EventEmitter {
) )
} }
// check nested flow
await transaction.scheduleRetry( await transaction.scheduleRetry(
step, step,
step.definition.retryInterval ?? 0 step.definition.retryInterval ?? 0
@@ -1033,6 +1037,7 @@ export class TransactionOrchestrator extends EventEmitter {
hasAsyncSteps: false, hasAsyncSteps: false,
hasStepTimeouts: false, hasStepTimeouts: false,
hasRetriesTimeout: false, hasRetriesTimeout: false,
hasNestedTransactions: false,
} }
while (queue.length > 0) { while (queue.length > 0) {
@@ -1073,6 +1078,10 @@ export class TransactionOrchestrator extends EventEmitter {
features.hasRetriesTimeout = true features.hasRetriesTimeout = true
} }
if (definitionCopy.nested) {
features.hasNestedTransactions = true
}
states[id] = Object.assign( states[id] = Object.assign(
new TransactionStep(), new TransactionStep(),
existingSteps?.[id] || { existingSteps?.[id] || {
@@ -65,6 +65,11 @@ export type TransactionStepsDefinition = {
*/ */
async?: boolean async?: boolean
/**
* It flags where the step contains a sub transaction inside itself.
*/
nested?: boolean
/** /**
* It applies to "async" steps only, allowing them to run in the background and automatically complete without external intervention. * It applies to "async" steps only, allowing them to run in the background and automatically complete without external intervention.
* It is ideal for time-consuming tasks that will be complete after the execution, contrasting with standard "async" operations that require a response to be set in a later stage. * It is ideal for time-consuming tasks that will be complete after the execution, contrasting with standard "async" operations that require a response to be set in a later stage.
@@ -237,6 +242,7 @@ export type TransactionFlow = {
transactionId: string transactionId: string
metadata?: { metadata?: {
eventGroupId?: string eventGroupId?: string
parentIdempotencyKey?: string
[key: string]: unknown [key: string]: unknown
} }
hasAsyncSteps: boolean hasAsyncSteps: boolean
@@ -85,4 +85,9 @@ export type Context<TManager = unknown> = {
* A string indicating the idempotencyKey of the current workflow execution. * A string indicating the idempotencyKey of the current workflow execution.
*/ */
idempotencyKey?: string idempotencyKey?: string
/**
* A string indicating the idempotencyKey of the parent workflow execution.
*/
parentStepIdempotencyKey?: string
} }
@@ -18,6 +18,9 @@ type FlowRunOptions<TData = unknown> = {
export type Acknowledgement = { export type Acknowledgement = {
workflowId: string workflowId: string
transactionId: string transactionId: string
parentStepIdempotencyKey?: string
hasFinished: boolean
hasFailed: boolean
} }
export interface WorkflowOrchestratorRunDTO<T = unknown> export interface WorkflowOrchestratorRunDTO<T = unknown>
@@ -74,6 +74,7 @@ function createContextualWorkflowRunner<
) => { ) => {
if (!executionContainer) { if (!executionContainer) {
const container_ = flow.container as MedusaContainer const container_ = flow.container as MedusaContainer
if (!container_ || !isPresent(container_?.registrations)) { if (!container_ || !isPresent(container_?.registrations)) {
executionContainer = MedusaModule.getLoadedModules().map( executionContainer = MedusaModule.getLoadedModules().map(
(mod) => Object.values(mod)[0] (mod) => Object.values(mod)[0]
@@ -85,12 +86,13 @@ function createContextualWorkflowRunner<
flow.container = executionContainer flow.container = executionContainer
} }
const { eventGroupId } = context const { eventGroupId, parentStepIdempotencyKey } = context
attachOnFinishReleaseEvents(events, eventGroupId!, flow, { logOnError }) attachOnFinishReleaseEvents(events, eventGroupId!, flow, { logOnError })
const flowMetadata = { const flowMetadata = {
eventGroupId, eventGroupId,
parentStepIdempotencyKey,
} }
const args = [ const args = [
@@ -22,8 +22,8 @@ class MedusaWorkflow {
MedusaWorkflow.workflows[workflowId] = exportedWorkflow MedusaWorkflow.workflows[workflowId] = exportedWorkflow
} }
static getWorkflow(workflowId) { static getWorkflow(workflowId): ExportedWorkflow {
return MedusaWorkflow.workflows[workflowId] return MedusaWorkflow.workflows[workflowId] as unknown as ExportedWorkflow
} }
} }
@@ -240,8 +240,10 @@ describe("Workflow composer", () => {
expect(result).toEqual({ result: "hi from outside" }) expect(result).toEqual({ result: "hi from outside" })
expect(parentContext.transactionId).toEqual("transactionId") expect(parentContext.transactionId).toEqual(expect.any(String))
expect(parentContext.transactionId).toEqual(childContext.transactionId) expect(parentContext.transactionId).not.toEqual(
childContext.transactionId
)
expect(parentContext.eventGroupId).toEqual("eventGroupId") expect(parentContext.eventGroupId).toEqual("eventGroupId")
expect(parentContext.eventGroupId).toEqual(childContext.eventGroupId) expect(parentContext.eventGroupId).toEqual(childContext.eventGroupId)
@@ -287,7 +289,9 @@ describe("Workflow composer", () => {
expect(result).toEqual({ result: "hi from outside" }) expect(result).toEqual({ result: "hi from outside" })
expect(parentContext.transactionId).toBeTruthy() expect(parentContext.transactionId).toBeTruthy()
expect(parentContext.transactionId).toEqual(childContext.transactionId) expect(parentContext.transactionId).not.toEqual(
childContext.transactionId
)
expect(parentContext.eventGroupId).toBeTruthy() expect(parentContext.eventGroupId).toBeTruthy()
expect(parentContext.eventGroupId).toEqual(childContext.eventGroupId) expect(parentContext.eventGroupId).toEqual(childContext.eventGroupId)
@@ -7,6 +7,7 @@ import {
import { OrchestrationUtils, isString } from "@medusajs/utils" import { OrchestrationUtils, isString } from "@medusajs/utils"
import { ulid } from "ulid" import { ulid } from "ulid"
import { StepResponse, resolveValue } from "./helpers" import { StepResponse, resolveValue } from "./helpers"
import { createStepHandler } from "./helpers/create-step-handler"
import { proxify } from "./helpers/proxy" import { proxify } from "./helpers/proxy"
import { import {
CreateWorkflowComposerContext, CreateWorkflowComposerContext,
@@ -15,7 +16,6 @@ import {
StepFunctionResult, StepFunctionResult,
WorkflowData, WorkflowData,
} from "./type" } from "./type"
import { createStepHandler } from "./helpers/create-step-handler"
/** /**
* The type of invocation function passed to a step. * The type of invocation function passed to a step.
@@ -65,6 +65,11 @@ export type CompensateFn<T> = (
context: StepExecutionContext context: StepExecutionContext
) => unknown | Promise<unknown> ) => unknown | Promise<unknown>
export type LocalStepConfig = { name?: string } & Omit<
TransactionStepsDefinition,
"next" | "uuid" | "action"
>
export interface ApplyStepOptions< export interface ApplyStepOptions<
TStepInputs extends { TStepInputs extends {
[K in keyof TInvokeInput]: WorkflowData<TInvokeInput[K]> [K in keyof TInvokeInput]: WorkflowData<TInvokeInput[K]>
@@ -136,6 +141,8 @@ export function applyStep<
this.flow.addAction(stepName, stepConfig) this.flow.addAction(stepName, stepConfig)
this.isAsync ||= !!(stepConfig.async || stepConfig.compensateAsync)
if (!this.handlers.has(stepName)) { if (!this.handlers.has(stepName)) {
this.handlers.set(stepName, handler) this.handlers.set(stepName, handler)
} }
@@ -143,12 +150,7 @@ export function applyStep<
const ret = { const ret = {
__type: OrchestrationUtils.SymbolWorkflowStep, __type: OrchestrationUtils.SymbolWorkflowStep,
__step__: stepName, __step__: stepName,
config: ( config: (localConfig: LocalStepConfig) => {
localConfig: { name?: string } & Omit<
TransactionStepsDefinition,
"next" | "uuid" | "action"
>
) => {
const newStepName = localConfig.name ?? stepName const newStepName = localConfig.name ?? stepName
const newConfig = { const newConfig = {
...stepConfig, ...stepConfig,
@@ -160,6 +162,7 @@ export function applyStep<
this.handlers.set(newStepName, handler) this.handlers.set(newStepName, handler)
this.flow.replaceAction(stepConfig.uuid!, newStepName, newConfig) this.flow.replaceAction(stepConfig.uuid!, newStepName, newConfig)
this.isAsync ||= !!(newConfig.async || newConfig.compensateAsync)
ret.__step__ = newStepName ret.__step__ = newStepName
WorkflowManager.update(this.workflowId, this.flow, this.handlers) WorkflowManager.update(this.workflowId, this.flow, this.handlers)
@@ -178,7 +181,7 @@ export function applyStep<
flagSteps.push(confRef) flagSteps.push(confRef)
} }
return confRef return confRef as StepFunction<TInvokeInput, TInvokeResultOutput>
}, },
if: ( if: (
input: any, input: any,
@@ -5,6 +5,7 @@ import {
} from "@medusajs/orchestration" } from "@medusajs/orchestration"
import { LoadedModule, MedusaContainer } from "@medusajs/types" import { LoadedModule, MedusaContainer } from "@medusajs/types"
import { OrchestrationUtils, isString } from "@medusajs/utils" import { OrchestrationUtils, isString } from "@medusajs/utils"
import { ulid } from "ulid"
import { exportWorkflow } from "../../helper" import { exportWorkflow } from "../../helper"
import { createStep } from "./create-step" import { createStep } from "./create-step"
import { proxify } from "./helpers/proxy" import { proxify } from "./helpers/proxy"
@@ -104,6 +105,7 @@ export function createWorkflow<TData, TResult, THooks extends any[]>(
__type: OrchestrationUtils.SymbolMedusaWorkflowComposerContext, __type: OrchestrationUtils.SymbolMedusaWorkflowComposerContext,
workflowId: name, workflowId: name,
flow: WorkflowManager.getEmptyTransactionDefinition(), flow: WorkflowManager.getEmptyTransactionDefinition(),
isAsync: false,
handlers, handlers,
hooks_: { hooks_: {
declared: [], declared: [],
@@ -176,21 +178,32 @@ export function createWorkflow<TData, TResult, THooks extends any[]>(
}: { }: {
input: TData input: TData
}): ReturnType<StepFunction<TData, TResult>> => { }): ReturnType<StepFunction<TData, TResult>> => {
// TODO: Async sub workflow is not supported yet const step = createStep(
// Info: Once the export workflow can fire the execution through the engine if loaded, the async workflow can be executed, {
// the step would inherit the async configuration and subscribe to the onFinish event of the sub worklow and mark itself as success or failure name: `${name}-as-step`,
return createStep( async: context.isAsync,
`${name}-as-step`, nested: context.isAsync, // if async we flag this is a nested transaction
},
async (stepInput: TData, stepContext) => { async (stepInput: TData, stepContext) => {
const { container, ...sharedContext } = stepContext const { container, ...sharedContext } = stepContext
const transaction = await workflow.run({ const transaction = await workflow.run({
input: stepInput as any, input: stepInput as any,
container, container,
context: sharedContext, context: {
...sharedContext,
parentStepIdempotencyKey: stepContext.idempotencyKey,
transactionId: ulid(),
},
}) })
return new StepResponse(transaction.result, transaction) const { result, transaction: flowTransaction } = transaction
if (!context.isAsync || flowTransaction.hasFinished()) {
return new StepResponse(result, transaction)
}
return
}, },
async (transaction, { container }) => { async (transaction, { container }) => {
if (!transaction) { if (!transaction) {
@@ -200,6 +213,8 @@ export function createWorkflow<TData, TResult, THooks extends any[]>(
await workflow(container).cancel(transaction) await workflow(container).cancel(transaction)
} }
)(input) as ReturnType<StepFunction<TData, TResult>> )(input) as ReturnType<StepFunction<TData, TResult>>
return step
} }
return mainFlow as ReturnWorkflow<TData, TResult, THooks> return mainFlow as ReturnWorkflow<TData, TResult, THooks>
@@ -1,13 +1,13 @@
import { WorkflowStepHandlerArguments } from "@medusajs/orchestration"
import { OrchestrationUtils, deepCopy } from "@medusajs/utils"
import { ApplyStepOptions } from "../create-step"
import { import {
CreateWorkflowComposerContext, CreateWorkflowComposerContext,
StepExecutionContext, StepExecutionContext,
WorkflowData, WorkflowData,
} from "../type" } from "../type"
import { WorkflowStepHandlerArguments } from "@medusajs/orchestration"
import { resolveValue } from "./resolve-value" import { resolveValue } from "./resolve-value"
import { StepResponse } from "./step-response" import { StepResponse } from "./step-response"
import { deepCopy, OrchestrationUtils } from "@medusajs/utils"
import { ApplyStepOptions } from "../create-step"
export function createStepHandler< export function createStepHandler<
TInvokeInput, TInvokeInput,
@@ -36,6 +36,8 @@ export function createStepHandler<
const idempotencyKey = metadata.idempotency_key const idempotencyKey = metadata.idempotency_key
stepArguments.context!.idempotencyKey = idempotencyKey stepArguments.context!.idempotencyKey = idempotencyKey
const flowMetadata = stepArguments.transaction.getFlow()?.metadata
const executionContext: StepExecutionContext = { const executionContext: StepExecutionContext = {
workflowId: metadata.model_id, workflowId: metadata.model_id,
stepName: metadata.action, stepName: metadata.action,
@@ -45,8 +47,9 @@ export function createStepHandler<
container: stepArguments.container, container: stepArguments.container,
metadata, metadata,
eventGroupId: eventGroupId:
stepArguments.transaction.getFlow()?.metadata?.eventGroupId ?? flowMetadata?.eventGroupId ?? stepArguments.context!.eventGroupId,
stepArguments.context!.eventGroupId, parentStepIdempotencyKey:
flowMetadata?.parentStepIdempotencyKey as string,
transactionId: stepArguments.context!.transactionId, transactionId: stepArguments.context!.transactionId,
context: stepArguments.context!, context: stepArguments.context!,
} }
@@ -74,11 +77,14 @@ export function createStepHandler<
stepArguments.context!.idempotencyKey = idempotencyKey stepArguments.context!.idempotencyKey = idempotencyKey
const flowMetadata = stepArguments.transaction.getFlow()?.metadata
const executionContext: StepExecutionContext = { const executionContext: StepExecutionContext = {
workflowId: metadata.model_id, workflowId: metadata.model_id,
stepName: metadata.action, stepName: metadata.action,
action: "compensate", action: "compensate",
idempotencyKey, idempotencyKey,
parentStepIdempotencyKey:
flowMetadata?.parentStepIdempotencyKey as string,
attempt: metadata.attempt, attempt: metadata.attempt,
container: stepArguments.container, container: stepArguments.container,
metadata, metadata,
@@ -98,6 +98,7 @@ export type CreateWorkflowComposerContext = {
hooksCallback_: Record<string, HookHandler> hooksCallback_: Record<string, HookHandler>
workflowId: string workflowId: string
flow: OrchestratorBuilder flow: OrchestratorBuilder
isAsync: boolean
handlers: WorkflowHandler handlers: WorkflowHandler
stepBinder: <TOutput = unknown>( stepBinder: <TOutput = unknown>(
fn: StepFunctionResult fn: StepFunctionResult
@@ -127,6 +128,11 @@ export interface StepExecutionContext {
*/ */
idempotencyKey: string idempotencyKey: string
/**
* The idempoency key of the parent step.
*/
parentStepIdempotencyKey?: string
/** /**
* The name of the step. * The name of the step.
*/ */
@@ -35,7 +35,7 @@
"dependencies": { "dependencies": {
"@medusajs/modules-sdk": "^1.12.11", "@medusajs/modules-sdk": "^1.12.11",
"@medusajs/utils": "^1.11.9", "@medusajs/utils": "^1.11.9",
"bullmq": "5.12.0", "bullmq": "5.13.0",
"ioredis": "^5.4.1" "ioredis": "^5.4.1"
}, },
"peerDependencies": { "peerDependencies": {
@@ -1,7 +1,9 @@
import { import {
StepResponse, StepResponse,
WorkflowResponse,
createStep, createStep,
createWorkflow, createWorkflow,
parallelize,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { setTimeout } from "timers/promises" import { setTimeout } from "timers/promises"
@@ -17,9 +19,9 @@ const step_1_background = createStep(
}) })
) )
createWorkflow( const nestedWorkflow = createWorkflow(
{ {
name: "workflow_async_background", name: "nested_sub_flow_async",
}, },
function (input) { function (input) {
const resp = step_1_background(input) const resp = step_1_background(input)
@@ -27,3 +29,25 @@ createWorkflow(
return resp return resp
} }
) )
createWorkflow(
{
name: "workflow_async_background",
},
function (input) {
const [ret] = parallelize(
nestedWorkflow
.runAsStep({
input,
})
.config({ name: "step_sub_flow_1" }),
nestedWorkflow
.runAsStep({
input,
})
.config({ name: "step_sub_flow_2" })
)
return new WorkflowResponse(ret)
}
)
@@ -25,7 +25,7 @@
"watch:test": "tsc --build tsconfig.spec.json --watch", "watch:test": "tsc --build tsconfig.spec.json --watch",
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json", "build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json",
"test": "jest --passWithNoTests --runInBand --bail --forceExit -- src", "test": "jest --passWithNoTests --runInBand --bail --forceExit -- src",
"test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.ts", "test:integration": "jest --silent --forceExit -- integration-tests/**/__tests__/**/*.ts",
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate", "migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate",
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial", "migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial",
"migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create", "migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create",
@@ -11,6 +11,7 @@ import {
InjectSharedContext, InjectSharedContext,
MedusaContext, MedusaContext,
MedusaError, MedusaError,
TransactionState,
isString, isString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { import {
@@ -97,6 +98,29 @@ export class WorkflowOrchestratorService {
WorkflowScheduler.setStorage(inMemoryDistributedTransactionStorage) WorkflowScheduler.setStorage(inMemoryDistributedTransactionStorage)
} }
private async triggerParentStep(transaction, result) {
const metadata = transaction.flow.metadata
const { parentStepIdempotencyKey } = metadata ?? {}
if (parentStepIdempotencyKey) {
const hasFailed = [
TransactionState.REVERTED,
TransactionState.FAILED,
].includes(transaction.flow.state)
if (hasFailed) {
await this.setStepFailure({
idempotencyKey: parentStepIdempotencyKey,
stepResponse: result,
})
} else {
await this.setStepSuccess({
idempotencyKey: parentStepIdempotencyKey,
stepResponse: result,
})
}
}
}
@InjectSharedContext() @InjectSharedContext()
async run<T = unknown>( async run<T = unknown>(
workflowIdOrWorkflow: string | ReturnWorkflow<any, any, any>, workflowIdOrWorkflow: string | ReturnWorkflow<any, any, any>,
@@ -155,14 +179,25 @@ export class WorkflowOrchestratorService {
events, events,
}) })
// TODO: temporary const hasFinished = ret.transaction.hasFinished()
const metadata = ret.transaction.getFlow().metadata
const { parentStepIdempotencyKey } = metadata ?? {}
const hasFailed = [
TransactionState.REVERTED,
TransactionState.FAILED,
].includes(ret.transaction.getFlow().state)
const acknowledgement = { const acknowledgement = {
transactionId: context.transactionId, transactionId: context.transactionId,
workflowId: workflowId, workflowId: workflowId,
parentStepIdempotencyKey,
hasFinished,
hasFailed,
} }
if (ret.transaction.hasFinished()) { if (ret.transaction.hasFinished()) {
const { result, errors } = ret const { result, errors } = ret
this.notify({ this.notify({
eventType: "onFinish", eventType: "onFinish",
workflowId, workflowId,
@@ -170,6 +205,8 @@ export class WorkflowOrchestratorService {
result, result,
errors, errors,
}) })
await this.triggerParentStep(ret.transaction, result)
} }
return { acknowledgement, ...ret } return { acknowledgement, ...ret }
@@ -261,6 +298,7 @@ export class WorkflowOrchestratorService {
if (ret.transaction.hasFinished()) { if (ret.transaction.hasFinished()) {
const { result, errors } = ret const { result, errors } = ret
this.notify({ this.notify({
eventType: "onFinish", eventType: "onFinish",
workflowId, workflowId,
@@ -268,6 +306,8 @@ export class WorkflowOrchestratorService {
result, result,
errors, errors,
}) })
await this.triggerParentStep(ret.transaction, result)
} }
return ret return ret
@@ -325,6 +365,7 @@ export class WorkflowOrchestratorService {
if (ret.transaction.hasFinished()) { if (ret.transaction.hasFinished()) {
const { result, errors } = ret const { result, errors } = ret
this.notify({ this.notify({
eventType: "onFinish", eventType: "onFinish",
workflowId, workflowId,
@@ -332,6 +373,8 @@ export class WorkflowOrchestratorService {
result, result,
errors, errors,
}) })
await this.triggerParentStep(ret.transaction, result)
} }
return ret return ret
@@ -1,7 +1,9 @@
import { import {
StepResponse, StepResponse,
WorkflowResponse,
createStep, createStep,
createWorkflow, createWorkflow,
parallelize,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { setTimeout } from "timers/promises" import { setTimeout } from "timers/promises"
@@ -11,15 +13,15 @@ const step_1_background = createStep(
async: true, async: true,
}, },
jest.fn(async (input) => { jest.fn(async (input) => {
await setTimeout(200) await setTimeout(Math.random() * 300)
return new StepResponse(input) return new StepResponse(input)
}) })
) )
createWorkflow( const nestedWorkflow = createWorkflow(
{ {
name: "workflow_async_background", name: "nested_sub_flow_async",
}, },
function (input) { function (input) {
const resp = step_1_background(input) const resp = step_1_background(input)
@@ -27,3 +29,35 @@ createWorkflow(
return resp return resp
} }
) )
createWorkflow(
{
name: "workflow_async_background",
},
function (input) {
const [ret] = parallelize(
nestedWorkflow
.runAsStep({
input,
})
.config({ name: "step_sub_flow_1" }),
nestedWorkflow
.runAsStep({
input,
})
.config({ name: "step_sub_flow_2" }),
nestedWorkflow
.runAsStep({
input,
})
.config({ name: "step_sub_flow_3" }),
nestedWorkflow
.runAsStep({
input,
})
.config({ name: "step_sub_flow_4" })
)
return new WorkflowResponse(ret)
}
)
@@ -23,7 +23,7 @@ import "../__fixtures__"
import { createScheduled } from "../__fixtures__/workflow_scheduled" import { createScheduled } from "../__fixtures__/workflow_scheduled"
import { TestDatabase } from "../utils" import { TestDatabase } from "../utils"
jest.setTimeout(100000) jest.setTimeout(999900000)
moduleIntegrationTestRunner<IWorkflowEngineService>({ moduleIntegrationTestRunner<IWorkflowEngineService>({
moduleName: Modules.WORKFLOW_ENGINE, moduleName: Modules.WORKFLOW_ENGINE,
@@ -35,10 +35,8 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
}, },
testSuite: ({ service: workflowOrcModule, medusaApp }) => { testSuite: ({ service: workflowOrcModule, medusaApp }) => {
describe("Workflow Orchestrator module", function () { describe("Workflow Orchestrator module", function () {
const afterEach_ = async () => { beforeEach(async () => {
await TestDatabase.clearTables() await TestDatabase.clearTables()
}
beforeEach(() => {
jest.clearAllMocks() jest.clearAllMocks()
}) })
@@ -93,8 +91,6 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
}) })
describe("Testing basic workflow", function () { describe("Testing basic workflow", function () {
afterEach(afterEach_)
it("should return a list of workflow executions and remove after completed when there is no retentionTime set", async () => { it("should return a list of workflow executions and remove after completed when there is no retentionTime set", async () => {
await workflowOrcModule.run("workflow_1", { await workflowOrcModule.run("workflow_1", {
input: { input: {
@@ -271,34 +267,31 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
).toBe(true) ).toBe(true)
}) })
it("should complete an async workflow that returns a StepResponse", async () => { it("should complete an async workflow that returns a StepResponse", (done) => {
const { transaction, result } = await workflowOrcModule.run( const transactionId = "transaction_1"
"workflow_async_background", void workflowOrcModule
{ .run("workflow_async_background", {
input: { input: {
myInput: "123", myInput: "123",
}, },
transactionId: "transaction_1", transactionId,
throwOnError: false, throwOnError: true,
} })
) .then(({ transaction, result }) => {
expect(transaction.flow.state).toEqual(
TransactionStepState.INVOKING
)
expect(result).toEqual(undefined)
})
expect(transaction.flow.state).toEqual(TransactionStepState.INVOKING) void workflowOrcModule.subscribe({
expect(result).toEqual(undefined) workflowId: "workflow_async_background",
transactionId,
await setTimeout(205) subscriber: (event) => {
if (event.eventType === "onFinish") {
const trx = await workflowOrcModule.run("workflow_async_background", { done()
input: { }
myInput: "123",
}, },
transactionId: "transaction_1",
throwOnError: false,
})
expect(trx.transaction.flow.state).toEqual(TransactionStepState.DONE)
expect(trx.result).toEqual({
myInput: "123",
}) })
}) })
@@ -25,7 +25,7 @@
"watch:test": "tsc --build tsconfig.spec.json --watch", "watch:test": "tsc --build tsconfig.spec.json --watch",
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json", "build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json",
"test": "jest --passWithNoTests --runInBand --bail --forceExit -- src", "test": "jest --passWithNoTests --runInBand --bail --forceExit -- src",
"test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.ts", "test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.ts",
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate", "migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate",
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial", "migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial",
"migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create", "migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create",
@@ -47,13 +47,14 @@
"@medusajs/orchestration": "^0.5.7", "@medusajs/orchestration": "^0.5.7",
"@medusajs/utils": "^1.11.9", "@medusajs/utils": "^1.11.9",
"@medusajs/workflows-sdk": "^0.1.6", "@medusajs/workflows-sdk": "^0.1.6",
"bullmq": "5.12.0", "bullmq": "5.13.0",
"ioredis": "^5.4.1" "ioredis": "^5.4.1"
}, },
"peerDependencies": { "peerDependencies": {
"@mikro-orm/core": "5.9.7", "@mikro-orm/core": "5.9.7",
"@mikro-orm/migrations": "5.9.7", "@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7", "@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.1" "awilix": "^8.0.1",
"ulid": "^2.3.0"
} }
} }
@@ -12,7 +12,12 @@ import {
Logger, Logger,
MedusaContainer, MedusaContainer,
} from "@medusajs/types" } from "@medusajs/types"
import { InjectSharedContext, MedusaContext, isString } from "@medusajs/utils" import {
InjectSharedContext,
MedusaContext,
TransactionState,
isString,
} from "@medusajs/utils"
import { import {
FlowRunOptions, FlowRunOptions,
MedusaWorkflow, MedusaWorkflow,
@@ -146,6 +151,29 @@ export class WorkflowOrchestratorService {
await this.redisDistributedTransactionStorage_.onApplicationStart() await this.redisDistributedTransactionStorage_.onApplicationStart()
} }
private async triggerParentStep(transaction, result) {
const metadata = transaction.flow.metadata
const { parentStepIdempotencyKey } = metadata ?? {}
if (parentStepIdempotencyKey) {
const hasFailed = [
TransactionState.REVERTED,
TransactionState.FAILED,
].includes(transaction.flow.state)
if (hasFailed) {
await this.setStepFailure({
idempotencyKey: parentStepIdempotencyKey,
stepResponse: result,
})
} else {
await this.setStepSuccess({
idempotencyKey: parentStepIdempotencyKey,
stepResponse: result,
})
}
}
}
@InjectSharedContext() @InjectSharedContext()
async run<T = unknown>( async run<T = unknown>(
workflowIdOrWorkflow: string | ReturnWorkflow<any, any, any>, workflowIdOrWorkflow: string | ReturnWorkflow<any, any, any>,
@@ -180,16 +208,12 @@ export class WorkflowOrchestratorService {
transactionId: context.transactionId, transactionId: context.transactionId,
}) })
const exportedWorkflow: any = MedusaWorkflow.getWorkflow(workflowId) const exportedWorkflow = MedusaWorkflow.getWorkflow(workflowId)
if (!exportedWorkflow) { if (!exportedWorkflow) {
throw new Error(`Workflow with id "${workflowId}" not found.`) throw new Error(`Workflow with id "${workflowId}" not found.`)
} }
const flow = exportedWorkflow( const ret = await exportedWorkflow.run({
(container as MedusaContainer) ?? this.container_
)
const ret = await flow.run({
input, input,
throwOnError, throwOnError,
logOnError, logOnError,
@@ -198,14 +222,25 @@ export class WorkflowOrchestratorService {
events, events,
}) })
// TODO: temporary const hasFinished = ret.transaction.hasFinished()
const metadata = ret.transaction.getFlow().metadata
const { parentStepIdempotencyKey } = metadata ?? {}
const hasFailed = [
TransactionState.REVERTED,
TransactionState.FAILED,
].includes(ret.transaction.getFlow().state)
const acknowledgement = { const acknowledgement = {
transactionId: context.transactionId, transactionId: context.transactionId,
workflowId: workflowId, workflowId: workflowId,
parentStepIdempotencyKey,
hasFinished,
hasFailed,
} }
if (ret.transaction.hasFinished()) { if (ret.transaction.hasFinished()) {
const { result, errors } = ret const { result, errors } = ret
await this.notify({ await this.notify({
eventType: "onFinish", eventType: "onFinish",
workflowId, workflowId,
@@ -213,6 +248,8 @@ export class WorkflowOrchestratorService {
result, result,
errors, errors,
}) })
await this.triggerParentStep(ret.transaction, result)
} }
return { acknowledgement, ...ret } return { acknowledgement, ...ret }
@@ -243,12 +280,11 @@ export class WorkflowOrchestratorService {
throw new Error(`Workflow with id "${workflowId}" not found.`) throw new Error(`Workflow with id "${workflowId}" not found.`)
} }
const flow = exportedWorkflow( const transaction = await exportedWorkflow.getRunningTransaction(
(container as MedusaContainer) ?? this.container_ transactionId,
context
) )
const transaction = await flow.getRunningTransaction(transactionId, context)
return transaction return transaction
} }
@@ -282,17 +318,13 @@ export class WorkflowOrchestratorService {
throw new Error(`Workflow with id "${workflowId}" not found.`) throw new Error(`Workflow with id "${workflowId}" not found.`)
} }
const flow = exportedWorkflow(
(container as MedusaContainer) ?? this.container_
)
const events = this.buildWorkflowEvents({ const events = this.buildWorkflowEvents({
customEventHandlers: eventHandlers, customEventHandlers: eventHandlers,
transactionId, transactionId,
workflowId, workflowId,
}) })
const ret = await flow.registerStepSuccess({ const ret = await exportedWorkflow.registerStepSuccess({
idempotencyKey: idempotencyKey_, idempotencyKey: idempotencyKey_,
context, context,
resultFrom, resultFrom,
@@ -304,6 +336,7 @@ export class WorkflowOrchestratorService {
if (ret.transaction.hasFinished()) { if (ret.transaction.hasFinished()) {
const { result, errors } = ret const { result, errors } = ret
await this.notify({ await this.notify({
eventType: "onFinish", eventType: "onFinish",
workflowId, workflowId,
@@ -311,6 +344,8 @@ export class WorkflowOrchestratorService {
result, result,
errors, errors,
}) })
await this.triggerParentStep(ret.transaction, result)
} }
return ret return ret
@@ -368,6 +403,7 @@ export class WorkflowOrchestratorService {
if (ret.transaction.hasFinished()) { if (ret.transaction.hasFinished()) {
const { result, errors } = ret const { result, errors } = ret
await this.notify({ await this.notify({
eventType: "onFinish", eventType: "onFinish",
workflowId, workflowId,
@@ -375,6 +411,8 @@ export class WorkflowOrchestratorService {
result, result,
errors, errors,
}) })
await this.triggerParentStep(ret.transaction, result)
} }
return ret return ret
@@ -66,16 +66,16 @@ export class RedisDistributedTransactionStorage
} }
async onApplicationStart() { async onApplicationStart() {
const allowedJobs = [
JobType.RETRY,
JobType.STEP_TIMEOUT,
JobType.TRANSACTION_TIMEOUT,
]
this.worker = new Worker( this.worker = new Worker(
this.queueName, this.queueName,
async (job) => { async (job) => {
const allJobs = [ if (allowedJobs.includes(job.name as JobType)) {
JobType.RETRY,
JobType.STEP_TIMEOUT,
JobType.TRANSACTION_TIMEOUT,
]
if (allJobs.includes(job.name as JobType)) {
await this.executeTransaction( await this.executeTransaction(
job.data.workflowId, job.data.workflowId,
job.data.transactionId job.data.transactionId
+7 -6
View File
@@ -5855,7 +5855,7 @@ __metadata:
"@medusajs/modules-sdk": ^1.12.11 "@medusajs/modules-sdk": ^1.12.11
"@medusajs/types": ^1.11.16 "@medusajs/types": ^1.11.16
"@medusajs/utils": ^1.11.9 "@medusajs/utils": ^1.11.9
bullmq: 5.12.0 bullmq: 5.13.0
cross-env: ^5.2.1 cross-env: ^5.2.1
ioredis: ^5.4.1 ioredis: ^5.4.1
jest: ^29.7.0 jest: ^29.7.0
@@ -6776,7 +6776,7 @@ __metadata:
"@medusajs/utils": ^1.11.9 "@medusajs/utils": ^1.11.9
"@medusajs/workflows-sdk": ^0.1.6 "@medusajs/workflows-sdk": ^0.1.6
"@mikro-orm/cli": 5.9.7 "@mikro-orm/cli": 5.9.7
bullmq: 5.12.0 bullmq: 5.13.0
cross-env: ^5.2.1 cross-env: ^5.2.1
ioredis: ^5.4.1 ioredis: ^5.4.1
jest: ^29.7.0 jest: ^29.7.0
@@ -6790,6 +6790,7 @@ __metadata:
"@mikro-orm/migrations": 5.9.7 "@mikro-orm/migrations": 5.9.7
"@mikro-orm/postgresql": 5.9.7 "@mikro-orm/postgresql": 5.9.7
awilix: ^8.0.1 awilix: ^8.0.1
ulid: ^2.3.0
languageName: unknown languageName: unknown
linkType: soft linkType: soft
@@ -16196,9 +16197,9 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"bullmq@npm:5.12.0": "bullmq@npm:5.13.0":
version: 5.12.0 version: 5.13.0
resolution: "bullmq@npm:5.12.0" resolution: "bullmq@npm:5.13.0"
dependencies: dependencies:
cron-parser: ^4.6.0 cron-parser: ^4.6.0
ioredis: ^5.4.1 ioredis: ^5.4.1
@@ -16207,7 +16208,7 @@ __metadata:
semver: ^7.5.4 semver: ^7.5.4
tslib: ^2.0.0 tslib: ^2.0.0
uuid: ^9.0.0 uuid: ^9.0.0
checksum: ade12a22c16db021385bdfab826ab64488fcfb07274df1b6269bf513d073ea8e8a812b7308027a16df3f00feaf1d2afe79c8c7c86749b44794c2a2e89e34a411 checksum: 28435676dbd0a1d5085540095eb6ad338a0d07fe37e8268833d5d8d4d5e1895dc62c9ce288d5493ce0af664b8502e5eff723dd964ed5c2b2a2f5d1ee7b9a27de
languageName: node languageName: node
linkType: hard linkType: hard