chore(workflow-engine): export cancel method (#11844)
What: * Workflow engine exports the method `cancel` to revert a workflow.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
export * from "./workflow_1"
|
||||
export * from "./workflow_2"
|
||||
export * from "./workflow_async"
|
||||
export * from "./workflow_async_compensate"
|
||||
export * from "./workflow_step_timeout"
|
||||
export * from "./workflow_sync"
|
||||
export * from "./workflow_transaction_timeout"
|
||||
export * from "./workflow_when"
|
||||
export * from "./workflow_async_compensate"
|
||||
|
||||
+64
@@ -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))
|
||||
}
|
||||
)
|
||||
@@ -512,6 +512,26 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
|
||||
|
||||
failTrap(done)
|
||||
})
|
||||
|
||||
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")
|
||||
})
|
||||
})
|
||||
|
||||
// Note: These tests depend on actual Redis instance and waiting for the scheduled jobs to run, which isn't great.
|
||||
|
||||
@@ -35,8 +35,11 @@ export type WorkflowOrchestratorRunOptions<T> = Omit<
|
||||
|
||||
export type WorkflowOrchestratorCancelOptions = Omit<
|
||||
FlowCancelOptions,
|
||||
"transaction"
|
||||
>
|
||||
"transaction" | "transactionId" | "container"
|
||||
> & {
|
||||
transactionId: string
|
||||
container?: ContainerLike
|
||||
}
|
||||
|
||||
type RegisterStepSuccessOptions<T> = Omit<
|
||||
WorkflowOrchestratorRunOptions<T>,
|
||||
@@ -379,10 +382,8 @@ export class WorkflowOrchestratorService {
|
||||
async getRunningTransaction(
|
||||
workflowId: string,
|
||||
transactionId: string,
|
||||
options?: { context?: Context }
|
||||
context?: Context
|
||||
): Promise<DistributedTransactionType> {
|
||||
let { context } = options ?? {}
|
||||
|
||||
if (!workflowId) {
|
||||
throw new Error("Workflow ID is required")
|
||||
}
|
||||
@@ -398,10 +399,9 @@ export class WorkflowOrchestratorService {
|
||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||
}
|
||||
|
||||
const transaction = await exportedWorkflow.getRunningTransaction(
|
||||
transactionId,
|
||||
context
|
||||
)
|
||||
const flow = exportedWorkflow()
|
||||
|
||||
const transaction = await flow.getRunningTransaction(transactionId, context)
|
||||
|
||||
return transaction
|
||||
}
|
||||
|
||||
@@ -17,7 +17,10 @@ import type {
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { WorkflowExecution } from "@models"
|
||||
import { WorkflowOrchestratorService } from "@services"
|
||||
import {
|
||||
WorkflowOrchestratorCancelOptions,
|
||||
WorkflowOrchestratorService,
|
||||
} from "@services"
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: SqlEntityManager
|
||||
@@ -112,7 +115,7 @@ export class WorkflowsModuleService<
|
||||
return await this.workflowOrchestratorService_.getRunningTransaction(
|
||||
workflowId,
|
||||
transactionId,
|
||||
{ context }
|
||||
context
|
||||
)
|
||||
}
|
||||
|
||||
@@ -194,4 +197,13 @@ export class WorkflowsModuleService<
|
||||
updated_at <= (CURRENT_TIMESTAMP - INTERVAL '1 second' * retention_time);
|
||||
`)
|
||||
}
|
||||
|
||||
@InjectSharedContext()
|
||||
async cancel(
|
||||
workflowId: string,
|
||||
options: WorkflowOrchestratorCancelOptions,
|
||||
@MedusaContext() context: Context = {}
|
||||
) {
|
||||
return this.workflowOrchestratorService_.cancel(workflowId, options)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user