chore(orchestration): idempotent (#7771)

This commit is contained in:
Carlos R. L. Rodrigues
2024-06-25 10:34:00 -03:00
committed by GitHub
parent 66d17fabde
commit 5600e58b7f
19 changed files with 421 additions and 403 deletions
@@ -1,5 +1,6 @@
export * from "./workflow_1"
export * from "./workflow_2"
export * from "./workflow_async"
export * from "./workflow_idempotent"
export * from "./workflow_step_timeout"
export * from "./workflow_transaction_timeout"
@@ -0,0 +1,68 @@
import {
StepResponse,
createStep,
createWorkflow,
} from "@medusajs/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
}
console.log("reverted", compensateInput.compensate)
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_idempotent",
idempotent: true,
},
function (input) {
step_1(input)
const ret2 = step_2({ hey: "oh" })
step_2({ hey: "hello" }).config({
name: "new_step_name",
})
return step_3(ret2)
}
)
@@ -5,6 +5,7 @@ import {
RemoteQueryFunction,
} from "@medusajs/types"
import { Modules, TransactionHandlerType } from "@medusajs/utils"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
import { setTimeout as setTimeoutPromise } from "timers/promises"
import "../__fixtures__"
import { workflow2Step2Invoke, workflow2Step3Invoke } from "../__fixtures__"
@@ -14,7 +15,6 @@ import {
workflowEventGroupIdStep2Mock,
} from "../__fixtures__/workflow_event_group_id"
import { createScheduled } from "../__fixtures__/workflow_scheduled"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(100000)
@@ -235,6 +235,10 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
})
describe("Scheduled workflows", () => {
beforeEach(() => {
jest.clearAllMocks()
})
beforeAll(() => {
jest.useFakeTimers()
jest.spyOn(global, "setTimeout")
@@ -247,8 +251,6 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
it("should execute a scheduled workflow", async () => {
const spy = createScheduled("standard")
jest.clearAllMocks()
await jest.runOnlyPendingTimersAsync()
expect(setTimeout).toHaveBeenCalledTimes(2)
expect(spy).toHaveBeenCalledTimes(1)
@@ -291,6 +293,46 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
"Tried to execute a scheduled workflow with ID remove-scheduled that does not exist, removing it from the scheduler."
)
})
it("should fetch an idempotent workflow after its completion", async () => {
const { transaction: firstRun } = await workflowOrcModule.run(
"workflow_idempotent",
{
input: {
value: "123",
},
throwOnError: true,
transactionId: "transaction_1",
}
)
let executionsList = await query({
workflow_executions: {
fields: ["id"],
},
})
const { transaction: secondRun } = await workflowOrcModule.run(
"workflow_idempotent",
{
input: {
value: "123",
},
throwOnError: true,
transactionId: "transaction_1",
}
)
const executionsListAfter = await query({
workflow_executions: {
fields: ["id"],
},
})
expect(secondRun.flow.startedAt).toEqual(firstRun.flow.startedAt)
expect(executionsList).toHaveLength(1)
expect(executionsListAfter).toHaveLength(1)
})
})
})
},