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:
Adrien de Peretti
2025-04-17 11:34:19 +02:00
committed by GitHub
parent 9abcf7a83a
commit 8618e6ee38
16 changed files with 410 additions and 145 deletions

View File

@@ -8,6 +8,7 @@ import {
} from "@medusajs/framework/types"
import {
InjectSharedContext,
isDefined,
MedusaContext,
ModulesSdkUtils,
} from "@medusajs/framework/utils"
@@ -90,13 +91,38 @@ export class WorkflowsModuleService<
transactionManager,
preventReleaseEvents,
transactionId,
parentStepIdempotencyKey,
...restContext
} = context
options_.context ??= restContext
options_.context.preventReleaseEvents ??=
!!options_.context.parentStepIdempotencyKey
delete options_.context.parentStepIdempotencyKey
let localPreventReleaseEvents = false
if (isDefined(options_.context?.preventReleaseEvents)) {
localPreventReleaseEvents = options_.context!.preventReleaseEvents!
} else {
if (
isDefined(context.eventGroupId) &&
isDefined(options_.context?.eventGroupId) &&
context.eventGroupId === options_.context?.eventGroupId
) {
localPreventReleaseEvents = true
}
}
let eventGroupId
if (options_.context?.eventGroupId) {
eventGroupId = options_.context.eventGroupId
} else if (localPreventReleaseEvents && context.eventGroupId) {
eventGroupId = context.eventGroupId
}
options_.context = {
...(restContext ?? {}),
...(options_.context ?? {}),
eventGroupId,
preventReleaseEvents: localPreventReleaseEvents,
}
const ret = await this.workflowOrchestratorService_.run<
TWorkflow extends ReturnWorkflow<any, any, any>
@@ -133,8 +159,11 @@ export class WorkflowsModuleService<
},
@MedusaContext() context: Context = {}
) {
options ??= {}
options.context ??= context
const options_ = JSON.parse(JSON.stringify(options ?? {}))
const { manager, transactionManager, ...restContext } = context
options_.context ??= restContext
return await this.workflowOrchestratorService_.setStepSuccess({
idempotencyKey,
@@ -156,8 +185,11 @@ export class WorkflowsModuleService<
},
@MedusaContext() context: Context = {}
) {
options ??= {}
options.context ??= context
const options_ = JSON.parse(JSON.stringify(options ?? {}))
const { manager, transactionManager, ...restContext } = context
options_.context ??= restContext
return await this.workflowOrchestratorService_.setStepFailure({
idempotencyKey,
@@ -205,7 +237,7 @@ export class WorkflowsModuleService<
options: WorkflowOrchestratorCancelOptions,
@MedusaContext() context: Context = {}
) {
return this.workflowOrchestratorService_.cancel(
return await this.workflowOrchestratorService_.cancel(
workflowIdOrWorkflow,
options
)

View File

@@ -310,10 +310,16 @@ export class InMemoryDistributedTransactionStorage
const { modelId: workflowId, transactionId } = transaction
const inter = setTimeout(async () => {
const context = transaction.getFlow().metadata ?? {}
await this.workflowOrchestratorService_.run(workflowId, {
transactionId,
logOnError: true,
throwOnError: false,
context: {
eventGroupId: context.eventGroupId,
parentStepIdempotencyKey: context.parentStepIdempotencyKey,
preventReleaseEvents: context.preventReleaseEvents,
},
})
}, interval * 1e3)
@@ -343,9 +349,16 @@ export class InMemoryDistributedTransactionStorage
const { modelId: workflowId, transactionId } = transaction
const inter = setTimeout(async () => {
const context = transaction.getFlow().metadata ?? {}
await this.workflowOrchestratorService_.run(workflowId, {
transactionId,
logOnError: true,
throwOnError: false,
context: {
eventGroupId: context.eventGroupId,
parentStepIdempotencyKey: context.parentStepIdempotencyKey,
preventReleaseEvents: context.preventReleaseEvents,
},
})
}, interval * 1e3)
@@ -375,9 +388,16 @@ export class InMemoryDistributedTransactionStorage
const { modelId: workflowId, transactionId } = transaction
const inter = setTimeout(async () => {
const context = transaction.getFlow().metadata ?? {}
await this.workflowOrchestratorService_.run(workflowId, {
transactionId,
logOnError: true,
throwOnError: false,
context: {
eventGroupId: context.eventGroupId,
parentStepIdempotencyKey: context.parentStepIdempotencyKey,
preventReleaseEvents: context.preventReleaseEvents,
},
})
}, interval * 1e3)