fix(): Properly handle workflow as step now that events are fixed entirely (#12196)
**What** Now that all events management are fixed in the workflows life cycle, the run as step needs to leverage the workflow engine if present (which should always be the case for async workflows) in order to ensure the continuation and the ability to mark parent step in parent workflow as success or failure Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
9abcf7a83a
commit
8618e6ee38
@@ -102,6 +102,7 @@ function createContextualWorkflowRunner<
|
||||
eventGroupId,
|
||||
parentStepIdempotencyKey,
|
||||
sourcePath: options?.sourcePath,
|
||||
preventReleaseEvents,
|
||||
}
|
||||
|
||||
const args = [
|
||||
|
||||
@@ -3,10 +3,15 @@ import {
|
||||
WorkflowHandler,
|
||||
WorkflowManager,
|
||||
} from "@medusajs/orchestration"
|
||||
import { LoadedModule, MedusaContainer } from "@medusajs/types"
|
||||
import {
|
||||
IWorkflowEngineService,
|
||||
LoadedModule,
|
||||
MedusaContainer,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
getCallerFilePath,
|
||||
isString,
|
||||
Modules,
|
||||
OrchestrationUtils,
|
||||
} from "@medusajs/utils"
|
||||
import { ulid } from "ulid"
|
||||
@@ -188,38 +193,72 @@ export function createWorkflow<TData, TResult, THooks extends any[]>(
|
||||
async (stepInput: TData, stepContext) => {
|
||||
const { container, ...sharedContext } = stepContext
|
||||
|
||||
const transaction = await workflow.run({
|
||||
input: stepInput as any,
|
||||
container,
|
||||
context: {
|
||||
...sharedContext,
|
||||
transactionId:
|
||||
step.__step__ + "-" + (stepContext.transactionId ?? ulid()),
|
||||
parentStepIdempotencyKey: stepContext.idempotencyKey,
|
||||
preventReleaseEvents: true,
|
||||
},
|
||||
})
|
||||
const workflowEngine = container.resolve(Modules.WORKFLOW_ENGINE, {
|
||||
allowUnregistered: true,
|
||||
}) as IWorkflowEngineService
|
||||
|
||||
const { result } = transaction
|
||||
const executionContext = {
|
||||
...(sharedContext?.context ?? {}),
|
||||
transactionId:
|
||||
step.__step__ + "-" + (stepContext.transactionId ?? ulid()),
|
||||
parentStepIdempotencyKey: stepContext.idempotencyKey,
|
||||
preventReleaseEvents: true,
|
||||
}
|
||||
|
||||
let transaction
|
||||
if (workflowEngine && context.isAsync) {
|
||||
transaction = await workflowEngine.run(name, {
|
||||
input: stepInput as any,
|
||||
context: executionContext,
|
||||
})
|
||||
} else {
|
||||
transaction = await workflow.run({
|
||||
input: stepInput as any,
|
||||
container,
|
||||
context: executionContext,
|
||||
})
|
||||
}
|
||||
|
||||
return new StepResponse(
|
||||
result,
|
||||
transaction.result,
|
||||
context.isAsync ? stepContext.transactionId : transaction
|
||||
)
|
||||
},
|
||||
async (transaction, stepContext) => {
|
||||
// The step itself has failed, there is nothing to revert
|
||||
if (!transaction) {
|
||||
return
|
||||
}
|
||||
|
||||
const { container, ...sharedContext } = stepContext
|
||||
|
||||
const workflowEngine = container.resolve(Modules.WORKFLOW_ENGINE, {
|
||||
allowUnregistered: true,
|
||||
}) as IWorkflowEngineService
|
||||
|
||||
const executionContext = {
|
||||
...(sharedContext?.context ?? {}),
|
||||
transactionId:
|
||||
step.__step__ + "-" + (stepContext.transactionId ?? ulid()),
|
||||
parentStepIdempotencyKey: stepContext.idempotencyKey,
|
||||
preventReleaseEvents: true,
|
||||
}
|
||||
|
||||
const transactionId = step.__step__ + "-" + stepContext.transactionId
|
||||
await workflow(container).cancel({
|
||||
transaction: (transaction as WorkflowResult<any>)?.transaction,
|
||||
transactionId,
|
||||
container,
|
||||
context: {
|
||||
...sharedContext,
|
||||
parentStepIdempotencyKey: stepContext.idempotencyKey,
|
||||
},
|
||||
})
|
||||
|
||||
if (workflowEngine && context.isAsync) {
|
||||
await workflowEngine.cancel(name, {
|
||||
transactionId: transactionId,
|
||||
context: executionContext,
|
||||
})
|
||||
} else {
|
||||
await workflow(container).cancel({
|
||||
transaction: (transaction as WorkflowResult<any>)?.transaction,
|
||||
transactionId,
|
||||
container,
|
||||
context: executionContext,
|
||||
})
|
||||
}
|
||||
}
|
||||
)(input) as ReturnType<StepFunction<TData, TResult>>
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ function buildStepContext({
|
||||
eventGroupId:
|
||||
flowMetadata?.eventGroupId ?? stepArguments.context!.eventGroupId,
|
||||
parentStepIdempotencyKey: flowMetadata?.parentStepIdempotencyKey as string,
|
||||
preventReleaseEvents: flowMetadata?.preventReleaseEvents ?? false,
|
||||
transactionId: stepArguments.context!.transactionId,
|
||||
context: stepArguments.context!,
|
||||
" getStepResult"(
|
||||
|
||||
@@ -144,6 +144,11 @@ export interface StepExecutionContext {
|
||||
*/
|
||||
parentStepIdempotencyKey?: string
|
||||
|
||||
/**
|
||||
* Whether to prevent release events.
|
||||
*/
|
||||
preventReleaseEvents?: boolean
|
||||
|
||||
/**
|
||||
* The name of the step.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user