Skip to content

fix(wfe): should notify when finished + add state info #12982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/rude-windows-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/workflow-engine-inmemory": patch
"@medusajs/workflow-engine-redis": patch
---

fix(wfe): should notify when finished + add state info
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type NotifyOptions = {
eventType: keyof DistributedTransactionEvents
workflowId: string
transactionId?: string
state?: TransactionState
step?: TransactionStep
response?: unknown
result?: unknown
Expand Down Expand Up @@ -269,9 +270,6 @@ export class WorkflowOrchestratorService {
throw new Error(`Workflow with id "${workflowId}" not found.`)
}

const originalOnFinishHandler = events.onFinish!
delete events.onFinish

const transaction = await this.getRunningTransaction(
workflowId,
transactionId,
Expand Down Expand Up @@ -307,12 +305,11 @@ export class WorkflowOrchestratorService {
const metadata = ret.transaction.getFlow().metadata
const { parentStepIdempotencyKey } = metadata ?? {}

const hasFailed = [TransactionState.FAILED].includes(
ret.transaction.getFlow().state
)
const transactionState = ret.transaction.getFlow().state
const hasFailed = [TransactionState.FAILED].includes(transactionState)

const acknowledgement = {
transactionId: context.transactionId,
transactionId: transaction.transactionId,
workflowId: workflowId,
parentStepIdempotencyKey,
hasFinished,
Expand All @@ -323,8 +320,11 @@ export class WorkflowOrchestratorService {
if (hasFinished) {
const { result, errors } = ret

await originalOnFinishHandler({
transaction: ret.transaction,
this.notify({
eventType: "onFinish",
workflowId,
transactionId: transaction.transactionId,
state: transactionState as TransactionState,
result,
errors,
})
Expand Down Expand Up @@ -423,6 +423,7 @@ export class WorkflowOrchestratorService {
eventType: "onFinish",
workflowId,
transactionId,
state: ret.transaction.getFlow().state as TransactionState,
result,
errors,
})
Expand Down Expand Up @@ -493,6 +494,7 @@ export class WorkflowOrchestratorService {
eventType: "onFinish",
workflowId,
transactionId,
state: ret.transaction.getFlow().state as TransactionState,
result,
errors,
})
Expand Down Expand Up @@ -598,6 +600,7 @@ export class WorkflowOrchestratorService {
result,
step,
response,
state,
} = options

const subscribers: TransactionSubscribers =
Expand All @@ -613,6 +616,7 @@ export class WorkflowOrchestratorService {
response,
result,
errors,
state,
})
})
}
Expand Down Expand Up @@ -641,12 +645,14 @@ export class WorkflowOrchestratorService {
result,
response,
errors,
state,
}: {
eventType: keyof DistributedTransactionEvents
step?: TransactionStep
response?: unknown
result?: unknown
errors?: unknown[]
state?: TransactionState
}) => {
this.notify({
workflowId,
Expand All @@ -656,6 +662,7 @@ export class WorkflowOrchestratorService {
step,
result,
errors,
state,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({

void workflowOrcModule.subscribe({
workflowId: "wf-when",
transactionId: "trx_123_when",
subscriber: (event) => {
if (event.eventType === "onFinish") {
done()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type NotifyOptions = {
response?: unknown
result?: unknown
errors?: unknown[]
state?: TransactionState
}

type WorkflowId = string
Expand Down Expand Up @@ -318,9 +319,6 @@ export class WorkflowOrchestratorService {
throw new Error(`Workflow with id "${workflowId}" not found.`)
}

const originalOnFinishHandler = events.onFinish!
delete events.onFinish

const transaction = await this.getRunningTransaction(
workflowId,
transactionId,
Expand Down Expand Up @@ -352,12 +350,11 @@ export class WorkflowOrchestratorService {
const metadata = ret.transaction.getFlow().metadata
const { parentStepIdempotencyKey } = metadata ?? {}

const hasFailed = [TransactionState.FAILED].includes(
ret.transaction.getFlow().state
)
const transactionState = ret.transaction.getFlow().state
const hasFailed = [TransactionState.FAILED].includes(transactionState)

const acknowledgement = {
transactionId: context.transactionId,
transactionId: transaction.transactionId,
workflowId: workflowId,
parentStepIdempotencyKey,
hasFinished,
Expand All @@ -368,8 +365,11 @@ export class WorkflowOrchestratorService {
if (hasFinished) {
const { result, errors } = ret

await originalOnFinishHandler({
transaction: ret.transaction,
this.notify({
eventType: "onFinish",
workflowId,
transactionId: transaction.transactionId,
state: transactionState as TransactionState,
result,
errors,
})
Expand Down Expand Up @@ -449,9 +449,6 @@ export class WorkflowOrchestratorService {
workflowId,
})

const originalOnFinishHandler = events.onFinish!
delete events.onFinish

const ret = await exportedWorkflow.registerStepSuccess({
idempotencyKey: idempotencyKey_,
context,
Expand All @@ -466,8 +463,11 @@ export class WorkflowOrchestratorService {
if (ret.transaction.hasFinished()) {
const { result, errors } = ret

await originalOnFinishHandler({
transaction: ret.transaction,
this.notify({
eventType: "onFinish",
workflowId,
transactionId,
state: ret.transaction.getFlow().state as TransactionState,
result,
errors,
})
Expand Down Expand Up @@ -520,9 +520,6 @@ export class WorkflowOrchestratorService {
workflowId,
})

const originalOnFinishHandler = events.onFinish!
delete events.onFinish

const ret = await exportedWorkflow.registerStepFailure({
idempotencyKey: idempotencyKey_,
context,
Expand All @@ -537,8 +534,11 @@ export class WorkflowOrchestratorService {
if (ret.transaction.hasFinished()) {
const { result, errors } = ret

await originalOnFinishHandler({
transaction: ret.transaction,
this.notify({
eventType: "onFinish",
workflowId,
transactionId,
state: ret.transaction.getFlow().state as TransactionState,
result,
errors,
})
Expand Down Expand Up @@ -677,6 +677,7 @@ export class WorkflowOrchestratorService {
result,
step,
response,
state,
} = options

const subscribers: TransactionSubscribers =
Expand All @@ -692,6 +693,7 @@ export class WorkflowOrchestratorService {
response,
result,
errors,
state,
}
const isPromise = "then" in handler
if (isPromise) {
Expand Down Expand Up @@ -737,12 +739,14 @@ export class WorkflowOrchestratorService {
result,
response,
errors,
state,
}: {
eventType: keyof DistributedTransactionEvents
step?: TransactionStep
response?: unknown
result?: unknown
errors?: unknown[]
state?: TransactionState
}) => {
await this.notify({
workflowId,
Expand All @@ -752,6 +756,7 @@ export class WorkflowOrchestratorService {
step,
result,
errors,
state,
})
}

Expand Down