fix(wfe): should notify when finished + add state info (#12982)
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/workflow-engine-inmemory": patch
|
||||||
|
"@medusajs/workflow-engine-redis": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(wfe): should notify when finished + add state info
|
||||||
@@ -51,6 +51,7 @@ type NotifyOptions = {
|
|||||||
eventType: keyof DistributedTransactionEvents
|
eventType: keyof DistributedTransactionEvents
|
||||||
workflowId: string
|
workflowId: string
|
||||||
transactionId?: string
|
transactionId?: string
|
||||||
|
state?: TransactionState
|
||||||
step?: TransactionStep
|
step?: TransactionStep
|
||||||
response?: unknown
|
response?: unknown
|
||||||
result?: unknown
|
result?: unknown
|
||||||
@@ -269,9 +270,6 @@ export class WorkflowOrchestratorService {
|
|||||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const originalOnFinishHandler = events.onFinish!
|
|
||||||
delete events.onFinish
|
|
||||||
|
|
||||||
const transaction = await this.getRunningTransaction(
|
const transaction = await this.getRunningTransaction(
|
||||||
workflowId,
|
workflowId,
|
||||||
transactionId,
|
transactionId,
|
||||||
@@ -307,12 +305,11 @@ export class WorkflowOrchestratorService {
|
|||||||
const metadata = ret.transaction.getFlow().metadata
|
const metadata = ret.transaction.getFlow().metadata
|
||||||
const { parentStepIdempotencyKey } = metadata ?? {}
|
const { parentStepIdempotencyKey } = metadata ?? {}
|
||||||
|
|
||||||
const hasFailed = [TransactionState.FAILED].includes(
|
const transactionState = ret.transaction.getFlow().state
|
||||||
ret.transaction.getFlow().state
|
const hasFailed = [TransactionState.FAILED].includes(transactionState)
|
||||||
)
|
|
||||||
|
|
||||||
const acknowledgement = {
|
const acknowledgement = {
|
||||||
transactionId: context.transactionId,
|
transactionId: transaction.transactionId,
|
||||||
workflowId: workflowId,
|
workflowId: workflowId,
|
||||||
parentStepIdempotencyKey,
|
parentStepIdempotencyKey,
|
||||||
hasFinished,
|
hasFinished,
|
||||||
@@ -323,8 +320,11 @@ export class WorkflowOrchestratorService {
|
|||||||
if (hasFinished) {
|
if (hasFinished) {
|
||||||
const { result, errors } = ret
|
const { result, errors } = ret
|
||||||
|
|
||||||
await originalOnFinishHandler({
|
this.notify({
|
||||||
transaction: ret.transaction,
|
eventType: "onFinish",
|
||||||
|
workflowId,
|
||||||
|
transactionId: transaction.transactionId,
|
||||||
|
state: transactionState as TransactionState,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
})
|
})
|
||||||
@@ -423,6 +423,7 @@ export class WorkflowOrchestratorService {
|
|||||||
eventType: "onFinish",
|
eventType: "onFinish",
|
||||||
workflowId,
|
workflowId,
|
||||||
transactionId,
|
transactionId,
|
||||||
|
state: ret.transaction.getFlow().state as TransactionState,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
})
|
})
|
||||||
@@ -493,6 +494,7 @@ export class WorkflowOrchestratorService {
|
|||||||
eventType: "onFinish",
|
eventType: "onFinish",
|
||||||
workflowId,
|
workflowId,
|
||||||
transactionId,
|
transactionId,
|
||||||
|
state: ret.transaction.getFlow().state as TransactionState,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
})
|
})
|
||||||
@@ -598,6 +600,7 @@ export class WorkflowOrchestratorService {
|
|||||||
result,
|
result,
|
||||||
step,
|
step,
|
||||||
response,
|
response,
|
||||||
|
state,
|
||||||
} = options
|
} = options
|
||||||
|
|
||||||
const subscribers: TransactionSubscribers =
|
const subscribers: TransactionSubscribers =
|
||||||
@@ -613,6 +616,7 @@ export class WorkflowOrchestratorService {
|
|||||||
response,
|
response,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
|
state,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -641,12 +645,14 @@ export class WorkflowOrchestratorService {
|
|||||||
result,
|
result,
|
||||||
response,
|
response,
|
||||||
errors,
|
errors,
|
||||||
|
state,
|
||||||
}: {
|
}: {
|
||||||
eventType: keyof DistributedTransactionEvents
|
eventType: keyof DistributedTransactionEvents
|
||||||
step?: TransactionStep
|
step?: TransactionStep
|
||||||
response?: unknown
|
response?: unknown
|
||||||
result?: unknown
|
result?: unknown
|
||||||
errors?: unknown[]
|
errors?: unknown[]
|
||||||
|
state?: TransactionState
|
||||||
}) => {
|
}) => {
|
||||||
this.notify({
|
this.notify({
|
||||||
workflowId,
|
workflowId,
|
||||||
@@ -656,6 +662,7 @@ export class WorkflowOrchestratorService {
|
|||||||
step,
|
step,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
|
state,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -841,6 +841,7 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
|
|||||||
|
|
||||||
void workflowOrcModule.subscribe({
|
void workflowOrcModule.subscribe({
|
||||||
workflowId: "wf-when",
|
workflowId: "wf-when",
|
||||||
|
transactionId: "trx_123_when",
|
||||||
subscriber: (event) => {
|
subscriber: (event) => {
|
||||||
if (event.eventType === "onFinish") {
|
if (event.eventType === "onFinish") {
|
||||||
done()
|
done()
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ type NotifyOptions = {
|
|||||||
response?: unknown
|
response?: unknown
|
||||||
result?: unknown
|
result?: unknown
|
||||||
errors?: unknown[]
|
errors?: unknown[]
|
||||||
|
state?: TransactionState
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkflowId = string
|
type WorkflowId = string
|
||||||
@@ -318,9 +319,6 @@ export class WorkflowOrchestratorService {
|
|||||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const originalOnFinishHandler = events.onFinish!
|
|
||||||
delete events.onFinish
|
|
||||||
|
|
||||||
const transaction = await this.getRunningTransaction(
|
const transaction = await this.getRunningTransaction(
|
||||||
workflowId,
|
workflowId,
|
||||||
transactionId,
|
transactionId,
|
||||||
@@ -352,12 +350,11 @@ export class WorkflowOrchestratorService {
|
|||||||
const metadata = ret.transaction.getFlow().metadata
|
const metadata = ret.transaction.getFlow().metadata
|
||||||
const { parentStepIdempotencyKey } = metadata ?? {}
|
const { parentStepIdempotencyKey } = metadata ?? {}
|
||||||
|
|
||||||
const hasFailed = [TransactionState.FAILED].includes(
|
const transactionState = ret.transaction.getFlow().state
|
||||||
ret.transaction.getFlow().state
|
const hasFailed = [TransactionState.FAILED].includes(transactionState)
|
||||||
)
|
|
||||||
|
|
||||||
const acknowledgement = {
|
const acknowledgement = {
|
||||||
transactionId: context.transactionId,
|
transactionId: transaction.transactionId,
|
||||||
workflowId: workflowId,
|
workflowId: workflowId,
|
||||||
parentStepIdempotencyKey,
|
parentStepIdempotencyKey,
|
||||||
hasFinished,
|
hasFinished,
|
||||||
@@ -368,8 +365,11 @@ export class WorkflowOrchestratorService {
|
|||||||
if (hasFinished) {
|
if (hasFinished) {
|
||||||
const { result, errors } = ret
|
const { result, errors } = ret
|
||||||
|
|
||||||
await originalOnFinishHandler({
|
this.notify({
|
||||||
transaction: ret.transaction,
|
eventType: "onFinish",
|
||||||
|
workflowId,
|
||||||
|
transactionId: transaction.transactionId,
|
||||||
|
state: transactionState as TransactionState,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
})
|
})
|
||||||
@@ -449,9 +449,6 @@ export class WorkflowOrchestratorService {
|
|||||||
workflowId,
|
workflowId,
|
||||||
})
|
})
|
||||||
|
|
||||||
const originalOnFinishHandler = events.onFinish!
|
|
||||||
delete events.onFinish
|
|
||||||
|
|
||||||
const ret = await exportedWorkflow.registerStepSuccess({
|
const ret = await exportedWorkflow.registerStepSuccess({
|
||||||
idempotencyKey: idempotencyKey_,
|
idempotencyKey: idempotencyKey_,
|
||||||
context,
|
context,
|
||||||
@@ -466,8 +463,11 @@ export class WorkflowOrchestratorService {
|
|||||||
if (ret.transaction.hasFinished()) {
|
if (ret.transaction.hasFinished()) {
|
||||||
const { result, errors } = ret
|
const { result, errors } = ret
|
||||||
|
|
||||||
await originalOnFinishHandler({
|
this.notify({
|
||||||
transaction: ret.transaction,
|
eventType: "onFinish",
|
||||||
|
workflowId,
|
||||||
|
transactionId,
|
||||||
|
state: ret.transaction.getFlow().state as TransactionState,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
})
|
})
|
||||||
@@ -520,9 +520,6 @@ export class WorkflowOrchestratorService {
|
|||||||
workflowId,
|
workflowId,
|
||||||
})
|
})
|
||||||
|
|
||||||
const originalOnFinishHandler = events.onFinish!
|
|
||||||
delete events.onFinish
|
|
||||||
|
|
||||||
const ret = await exportedWorkflow.registerStepFailure({
|
const ret = await exportedWorkflow.registerStepFailure({
|
||||||
idempotencyKey: idempotencyKey_,
|
idempotencyKey: idempotencyKey_,
|
||||||
context,
|
context,
|
||||||
@@ -537,8 +534,11 @@ export class WorkflowOrchestratorService {
|
|||||||
if (ret.transaction.hasFinished()) {
|
if (ret.transaction.hasFinished()) {
|
||||||
const { result, errors } = ret
|
const { result, errors } = ret
|
||||||
|
|
||||||
await originalOnFinishHandler({
|
this.notify({
|
||||||
transaction: ret.transaction,
|
eventType: "onFinish",
|
||||||
|
workflowId,
|
||||||
|
transactionId,
|
||||||
|
state: ret.transaction.getFlow().state as TransactionState,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
})
|
})
|
||||||
@@ -677,6 +677,7 @@ export class WorkflowOrchestratorService {
|
|||||||
result,
|
result,
|
||||||
step,
|
step,
|
||||||
response,
|
response,
|
||||||
|
state,
|
||||||
} = options
|
} = options
|
||||||
|
|
||||||
const subscribers: TransactionSubscribers =
|
const subscribers: TransactionSubscribers =
|
||||||
@@ -692,6 +693,7 @@ export class WorkflowOrchestratorService {
|
|||||||
response,
|
response,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
|
state,
|
||||||
}
|
}
|
||||||
const isPromise = "then" in handler
|
const isPromise = "then" in handler
|
||||||
if (isPromise) {
|
if (isPromise) {
|
||||||
@@ -737,12 +739,14 @@ export class WorkflowOrchestratorService {
|
|||||||
result,
|
result,
|
||||||
response,
|
response,
|
||||||
errors,
|
errors,
|
||||||
|
state,
|
||||||
}: {
|
}: {
|
||||||
eventType: keyof DistributedTransactionEvents
|
eventType: keyof DistributedTransactionEvents
|
||||||
step?: TransactionStep
|
step?: TransactionStep
|
||||||
response?: unknown
|
response?: unknown
|
||||||
result?: unknown
|
result?: unknown
|
||||||
errors?: unknown[]
|
errors?: unknown[]
|
||||||
|
state?: TransactionState
|
||||||
}) => {
|
}) => {
|
||||||
await this.notify({
|
await this.notify({
|
||||||
workflowId,
|
workflowId,
|
||||||
@@ -752,6 +756,7 @@ export class WorkflowOrchestratorService {
|
|||||||
step,
|
step,
|
||||||
result,
|
result,
|
||||||
errors,
|
errors,
|
||||||
|
state,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user