fix(workflows-sdk): name for when/then step (#10459)
This commit is contained in:
committed by
GitHub
parent
7ff3f15d6d
commit
90ae187e09
@@ -9,6 +9,7 @@ import {
|
||||
import {
|
||||
ContainerLike,
|
||||
Context,
|
||||
Logger,
|
||||
MedusaContainer,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
@@ -24,6 +25,7 @@ import {
|
||||
ReturnWorkflow,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import Redis from "ioredis"
|
||||
import { setTimeout } from "timers"
|
||||
import { ulid } from "ulid"
|
||||
import type { RedisDistributedTransactionStorage } from "../utils"
|
||||
|
||||
@@ -92,6 +94,8 @@ export class WorkflowOrchestratorService {
|
||||
private subscribers: Subscribers = new Map()
|
||||
private activeStepsCount: number = 0
|
||||
|
||||
readonly #logger: Logger
|
||||
|
||||
protected redisDistributedTransactionStorage_: RedisDistributedTransactionStorage
|
||||
|
||||
constructor({
|
||||
@@ -112,6 +116,9 @@ export class WorkflowOrchestratorService {
|
||||
this.redisPublisher = redisPublisher
|
||||
this.redisSubscriber = redisSubscriber
|
||||
|
||||
this.#logger =
|
||||
this.container_.resolve("logger", { allowUnregistered: true }) ?? console
|
||||
|
||||
redisDistributedTransactionStorage.setWorkflowOrchestratorService(this)
|
||||
|
||||
if (!dataLoaderOnly) {
|
||||
@@ -149,6 +156,7 @@ export class WorkflowOrchestratorService {
|
||||
private async triggerParentStep(transaction, result) {
|
||||
const metadata = transaction.flow.metadata
|
||||
const { parentStepIdempotencyKey } = metadata ?? {}
|
||||
|
||||
if (parentStepIdempotencyKey) {
|
||||
const hasFailed = [
|
||||
TransactionState.REVERTED,
|
||||
@@ -159,11 +167,17 @@ export class WorkflowOrchestratorService {
|
||||
await this.setStepFailure({
|
||||
idempotencyKey: parentStepIdempotencyKey,
|
||||
stepResponse: result,
|
||||
options: {
|
||||
logOnError: true,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
await this.setStepSuccess({
|
||||
idempotencyKey: parentStepIdempotencyKey,
|
||||
stepResponse: result,
|
||||
options: {
|
||||
logOnError: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -209,6 +223,9 @@ export class WorkflowOrchestratorService {
|
||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||
}
|
||||
|
||||
const originalOnFinishHandler = events.onFinish!
|
||||
delete events.onFinish
|
||||
|
||||
const ret = await exportedWorkflow.run({
|
||||
input,
|
||||
throwOnError: false,
|
||||
@@ -235,13 +252,11 @@ export class WorkflowOrchestratorService {
|
||||
hasFailed,
|
||||
}
|
||||
|
||||
if (ret.transaction.hasFinished()) {
|
||||
if (hasFinished) {
|
||||
const { result, errors } = ret
|
||||
|
||||
await this.notify({
|
||||
eventType: "onFinish",
|
||||
workflowId,
|
||||
transactionId: context.transactionId,
|
||||
await originalOnFinishHandler({
|
||||
transaction: ret.transaction,
|
||||
result,
|
||||
errors,
|
||||
})
|
||||
@@ -327,6 +342,9 @@ export class WorkflowOrchestratorService {
|
||||
workflowId,
|
||||
})
|
||||
|
||||
const originalOnFinishHandler = events.onFinish!
|
||||
delete events.onFinish
|
||||
|
||||
const ret = await exportedWorkflow.registerStepSuccess({
|
||||
idempotencyKey: idempotencyKey_,
|
||||
context,
|
||||
@@ -341,10 +359,8 @@ export class WorkflowOrchestratorService {
|
||||
if (ret.transaction.hasFinished()) {
|
||||
const { result, errors } = ret
|
||||
|
||||
await this.notify({
|
||||
eventType: "onFinish",
|
||||
workflowId,
|
||||
transactionId,
|
||||
await originalOnFinishHandler({
|
||||
transaction: ret.transaction,
|
||||
result,
|
||||
errors,
|
||||
})
|
||||
@@ -397,6 +413,9 @@ export class WorkflowOrchestratorService {
|
||||
workflowId,
|
||||
})
|
||||
|
||||
const originalOnFinishHandler = events.onFinish!
|
||||
delete events.onFinish
|
||||
|
||||
const ret = await exportedWorkflow.registerStepFailure({
|
||||
idempotencyKey: idempotencyKey_,
|
||||
context,
|
||||
@@ -411,10 +430,8 @@ export class WorkflowOrchestratorService {
|
||||
if (ret.transaction.hasFinished()) {
|
||||
const { result, errors } = ret
|
||||
|
||||
await this.notify({
|
||||
eventType: "onFinish",
|
||||
workflowId,
|
||||
transactionId,
|
||||
await originalOnFinishHandler({
|
||||
transaction: ret.transaction,
|
||||
result,
|
||||
errors,
|
||||
})
|
||||
@@ -517,7 +534,6 @@ export class WorkflowOrchestratorService {
|
||||
|
||||
if (publish) {
|
||||
const channel = this.getChannelName(options.workflowId)
|
||||
|
||||
const message = JSON.stringify({
|
||||
instanceId: this.instanceId,
|
||||
data: options,
|
||||
@@ -540,7 +556,7 @@ export class WorkflowOrchestratorService {
|
||||
|
||||
const notifySubscribers = (handlers: SubscriberHandler[]) => {
|
||||
handlers.forEach((handler) => {
|
||||
handler({
|
||||
const args = {
|
||||
eventType,
|
||||
workflowId,
|
||||
transactionId,
|
||||
@@ -548,13 +564,30 @@ export class WorkflowOrchestratorService {
|
||||
response,
|
||||
result,
|
||||
errors,
|
||||
})
|
||||
}
|
||||
const isPromise = "then" in handler
|
||||
if (isPromise) {
|
||||
;(handler(args) as unknown as Promise<any>).catch((e) => {
|
||||
this.#logger.error(e)
|
||||
})
|
||||
} else {
|
||||
try {
|
||||
handler(args)
|
||||
} catch (e) {
|
||||
this.#logger.error(e)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (transactionId) {
|
||||
const transactionSubscribers = subscribers.get(transactionId) ?? []
|
||||
notifySubscribers(transactionSubscribers)
|
||||
|
||||
// removes transaction id subscribers on finish
|
||||
if (eventType === "onFinish") {
|
||||
subscribers.delete(transactionId)
|
||||
}
|
||||
}
|
||||
|
||||
const workflowSubscribers = subscribers.get(AnySubscriber) ?? []
|
||||
@@ -613,8 +646,8 @@ export class WorkflowOrchestratorService {
|
||||
await notify({ eventType: "onCompensateBegin" })
|
||||
},
|
||||
onFinish: async ({ transaction, result, errors }) => {
|
||||
// TODO: unsubscribe transaction handlers on finish
|
||||
customEventHandlers?.onFinish?.({ transaction, result, errors })
|
||||
await notify({ eventType: "onFinish" })
|
||||
},
|
||||
|
||||
onStepBegin: async ({ step, transaction }) => {
|
||||
|
||||
@@ -94,7 +94,10 @@ export class RedisDistributedTransactionStorage
|
||||
)
|
||||
}
|
||||
},
|
||||
{ connection: this.redisWorkerConnection }
|
||||
{
|
||||
connection:
|
||||
this.redisWorkerConnection /*, runRetryDelay: 100000 for tests */,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user