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",
|
||||
|
||||
Reference in New Issue
Block a user