chore(medusa, orchestration, utils, workflows-sdk): add transaction options support and cleanup (#6020)

* chore(medusa, orchestration, utils, workflows-sdk): add transaction options support and cleanup
This commit is contained in:
Adrien de Peretti
2024-01-08 14:07:12 +01:00
committed by GitHub
parent 0c858f7fd4
commit fbee006e51
9 changed files with 92 additions and 16 deletions

View File

@@ -9,8 +9,8 @@ import {
TransactionHandlerType,
TransactionModelOptions,
TransactionState,
TransactionStepStatus,
TransactionStepsDefinition,
TransactionStepStatus,
} from "./types"
import { MedusaError, promiseAll } from "@medusajs/utils"
@@ -61,6 +61,11 @@ export class TransactionOrchestrator extends EventEmitter {
public static getKeyName(...params: string[]): string {
return params.join(this.SEPARATOR)
}
public getOptions(): TransactionModelOptions {
return this.options ?? {}
}
private getPreviousStep(flow: TransactionFlow, step: TransactionStep) {
const id = step.id.split(".")
id.pop()
@@ -465,7 +470,7 @@ export class TransactionOrchestrator extends EventEmitter {
if (flow.options?.retentionTime == undefined) {
await transaction.deleteCheckpoint()
} else {
await transaction.archiveCheckpoint()
await transaction.saveCheckpoint()
}
this.emit(DistributedTransactionEvent.FINISH, { transaction })

View File

@@ -5,6 +5,7 @@ import {
DistributedTransaction,
DistributedTransactionEvent,
DistributedTransactionEvents,
TransactionModelOptions,
TransactionOrchestrator,
TransactionStepsDefinition,
} from "../transaction"
@@ -24,6 +25,7 @@ export class LocalWorkflow {
protected container: MedusaContainer
protected workflowId: string
protected flow: OrchestratorBuilder
protected customOptions: Partial<TransactionModelOptions> = {}
protected workflow: WorkflowDefinition
protected handlers: Map<string, StepHandler>
@@ -64,10 +66,21 @@ export class LocalWorkflow {
protected commit() {
const finalFlow = this.flow.build()
const globalWorkflow = WorkflowManager.getWorkflow(this.workflowId)
const customOptions = {
...globalWorkflow?.options,
...this.customOptions,
}
this.workflow = {
id: this.workflowId,
flow_: finalFlow,
orchestrator: new TransactionOrchestrator(this.workflowId, finalFlow),
orchestrator: new TransactionOrchestrator(
this.workflowId,
finalFlow,
customOptions
),
options: customOptions,
handler: WorkflowManager.buildHandlers(this.handlers),
handlers_: this.handlers,
}
@@ -361,6 +374,11 @@ export class LocalWorkflow {
return transaction
}
setOptions(options: Partial<TransactionModelOptions>) {
this.customOptions = options
return this
}
addAction(
action: string,
handler: StepHandler,

View File

@@ -4,6 +4,7 @@ import {
OrchestratorBuilder,
TransactionHandlerType,
TransactionMetadata,
TransactionModelOptions,
TransactionOrchestrator,
TransactionStepHandler,
TransactionStepsDefinition,
@@ -21,6 +22,7 @@ export interface WorkflowDefinition {
string,
{ invoke: WorkflowStepHandler; compensate?: WorkflowStepHandler }
>
options: TransactionModelOptions
requiredModules?: Set<string>
optionalModules?: Set<string>
}
@@ -72,6 +74,7 @@ export class WorkflowManager {
workflowId: string,
flow: TransactionStepsDefinition | OrchestratorBuilder | undefined,
handlers: WorkflowHandler,
options: TransactionModelOptions = {},
requiredModules?: Set<string>,
optionalModules?: Set<string>
) {
@@ -93,9 +96,14 @@ export class WorkflowManager {
WorkflowManager.workflows.set(workflowId, {
id: workflowId,
flow_: finalFlow!,
orchestrator: new TransactionOrchestrator(workflowId, finalFlow ?? {}),
orchestrator: new TransactionOrchestrator(
workflowId,
finalFlow ?? {},
options
),
handler: WorkflowManager.buildHandlers(handlers),
handlers_: handlers,
options,
requiredModules,
optionalModules,
})
@@ -108,6 +116,7 @@ export class WorkflowManager {
string,
{ invoke: WorkflowStepHandler; compensate?: WorkflowStepHandler }
>,
options: TransactionModelOptions = {},
requiredModules?: Set<string>,
optionalModules?: Set<string>
) {
@@ -126,9 +135,10 @@ export class WorkflowManager {
WorkflowManager.workflows.set(workflowId, {
id: workflowId,
flow_: finalFlow,
orchestrator: new TransactionOrchestrator(workflowId, finalFlow),
orchestrator: new TransactionOrchestrator(workflowId, finalFlow, options),
handler: WorkflowManager.buildHandlers(workflow.handlers_),
handlers_: workflow.handlers_,
options: { ...workflow.options, ...options },
requiredModules,
optionalModules,
})