chore(workflow-engine): export cancel method (#11844)
What: * Workflow engine exports the method `cancel` to revert a workflow.
This commit is contained in:
committed by
GitHub
parent
3db146c56e
commit
0625f76cd4
@@ -4,4 +4,5 @@ export * from "./workflow_async"
|
||||
export * from "./workflow_conditional_step"
|
||||
export * from "./workflow_idempotent"
|
||||
export * from "./workflow_step_timeout"
|
||||
export * from "./workflow_sync"
|
||||
export * from "./workflow_transaction_timeout"
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import {
|
||||
createStep,
|
||||
createWorkflow,
|
||||
StepResponse,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
|
||||
const step_1 = createStep(
|
||||
"step_1",
|
||||
jest.fn((input) => {
|
||||
input.test = "test"
|
||||
return new StepResponse(input, { compensate: 123 })
|
||||
}),
|
||||
jest.fn((compensateInput) => {
|
||||
if (!compensateInput) {
|
||||
return
|
||||
}
|
||||
|
||||
return new StepResponse({
|
||||
reverted: true,
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
const step_2 = createStep(
|
||||
"step_2",
|
||||
jest.fn((input, context) => {
|
||||
if (input) {
|
||||
return new StepResponse({ notAsyncResponse: input.hey })
|
||||
}
|
||||
}),
|
||||
jest.fn((_, context) => {
|
||||
return new StepResponse({
|
||||
step: context.metadata.action,
|
||||
idempotency_key: context.metadata.idempotency_key,
|
||||
reverted: true,
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
const step_3 = createStep(
|
||||
"step_3",
|
||||
jest.fn((res) => {
|
||||
return new StepResponse({
|
||||
done: {
|
||||
inputFromSyncStep: res.notAsyncResponse,
|
||||
},
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
createWorkflow(
|
||||
{
|
||||
name: "workflow_sync",
|
||||
idempotent: true,
|
||||
},
|
||||
function (input) {
|
||||
step_1(input)
|
||||
|
||||
const ret2 = step_2({ hey: "oh" })
|
||||
|
||||
return new WorkflowResponse(step_3(ret2))
|
||||
}
|
||||
)
|
||||
@@ -300,6 +300,26 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
|
||||
expect(onFinish).toHaveBeenCalledTimes(0)
|
||||
})
|
||||
|
||||
it("should cancel and revert a completed workflow", async () => {
|
||||
const workflowId = "workflow_sync"
|
||||
|
||||
const { acknowledgement, transaction: trx } =
|
||||
await workflowOrcModule.run(workflowId, {
|
||||
input: {
|
||||
value: "123",
|
||||
},
|
||||
})
|
||||
|
||||
expect(trx.getFlow().state).toEqual("done")
|
||||
expect(acknowledgement.hasFinished).toBe(true)
|
||||
|
||||
const { transaction } = await workflowOrcModule.cancel(workflowId, {
|
||||
transactionId: acknowledgement.transactionId,
|
||||
})
|
||||
|
||||
expect(transaction.getFlow().state).toEqual("reverted")
|
||||
})
|
||||
|
||||
it("should run conditional steps if condition is true", (done) => {
|
||||
void workflowOrcModule.subscribe({
|
||||
workflowId: "workflow_conditional_step",
|
||||
|
||||
@@ -6,7 +6,11 @@ import {
|
||||
TransactionStep,
|
||||
WorkflowScheduler,
|
||||
} from "@medusajs/framework/orchestration"
|
||||
import { ContainerLike, MedusaContainer } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerLike,
|
||||
Context,
|
||||
MedusaContainer,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
isString,
|
||||
MedusaError,
|
||||
@@ -18,9 +22,9 @@ import {
|
||||
resolveValue,
|
||||
ReturnWorkflow,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { WorkflowOrchestratorCancelOptions } from "@types"
|
||||
import { ulid } from "ulid"
|
||||
import { InMemoryDistributedTransactionStorage } from "../utils"
|
||||
import { WorkflowOrchestratorCancelOptions } from "@types"
|
||||
|
||||
export type WorkflowOrchestratorRunOptions<T> = Omit<
|
||||
FlowRunOptions<T>,
|
||||
@@ -319,10 +323,8 @@ export class WorkflowOrchestratorService {
|
||||
async getRunningTransaction(
|
||||
workflowId: string,
|
||||
transactionId: string,
|
||||
options?: WorkflowOrchestratorRunOptions<unknown>
|
||||
context?: Context
|
||||
): Promise<DistributedTransactionType> {
|
||||
let { context, container } = options ?? {}
|
||||
|
||||
if (!workflowId) {
|
||||
throw new Error("Workflow ID is required")
|
||||
}
|
||||
@@ -339,9 +341,7 @@ export class WorkflowOrchestratorService {
|
||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||
}
|
||||
|
||||
const flow = exportedWorkflow(
|
||||
(container as MedusaContainer) ?? this.container_
|
||||
)
|
||||
const flow = exportedWorkflow()
|
||||
|
||||
const transaction = await flow.getRunningTransaction(transactionId, context)
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import type {
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { WorkflowExecution } from "@models"
|
||||
import { WorkflowOrchestratorService } from "@services"
|
||||
import { WorkflowOrchestratorCancelOptions } from "@types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: SqlEntityManager
|
||||
@@ -185,4 +186,16 @@ export class WorkflowsModuleService<
|
||||
updated_at <= (CURRENT_TIMESTAMP - INTERVAL '1 second' * retention_time);
|
||||
`)
|
||||
}
|
||||
|
||||
@InjectSharedContext()
|
||||
async cancel<TWorkflow extends string | ReturnWorkflow<any, any, any>>(
|
||||
workflowIdOrWorkflow: TWorkflow,
|
||||
options: WorkflowOrchestratorCancelOptions,
|
||||
@MedusaContext() context: Context = {}
|
||||
) {
|
||||
return this.workflowOrchestratorService_.cancel(
|
||||
workflowIdOrWorkflow,
|
||||
options
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ export type InitializeModuleInjectableDependencies = {
|
||||
|
||||
export type WorkflowOrchestratorCancelOptions = Omit<
|
||||
FlowCancelOptions,
|
||||
"transaction" | "container"
|
||||
"transaction" | "transactionId" | "container"
|
||||
> & {
|
||||
transactionId: string
|
||||
container?: ContainerLike
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user