feat(workflows-sdk,orchestration): async step as background task (#6886)

This commit is contained in:
Carlos R. L. Rodrigues
2024-04-03 11:17:00 +02:00
committed by GitHub
parent 3dcf5224a1
commit a164c0d512
17 changed files with 290 additions and 34 deletions
@@ -1,4 +1,5 @@
export * from "./workflow_1"
export * from "./workflow_2"
export * from "./workflow_async"
export * from "./workflow_step_timeout"
export * from "./workflow_transaction_timeout"
@@ -25,8 +25,6 @@ const step_1 = createStep(
const step_2 = createStep(
"step_2",
jest.fn((input, context) => {
console.log("triggered async request", context.metadata.idempotency_key)
if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}
@@ -25,8 +25,6 @@ const step_1 = createStep(
const step_2 = createStep(
"step_2",
jest.fn((input, context) => {
console.log("triggered async request", context.metadata.idempotency_key)
if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}
@@ -0,0 +1,29 @@
import {
StepResponse,
createStep,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { setTimeout } from "timers/promises"
const step_1_background = createStep(
{
name: "step_1_background",
async: true,
},
jest.fn(async (input) => {
await setTimeout(200)
return new StepResponse(input)
})
)
createWorkflow(
{
name: "workflow_async_background",
},
function (input) {
const resp = step_1_background(input)
return resp
}
)
@@ -22,7 +22,7 @@ const step_1_async = createStep(
},
jest.fn(async (input) => {
return new StepResponse(input, { compensate: 123 })
return
})
)
@@ -4,7 +4,7 @@ import {
TransactionTimeoutError,
} from "@medusajs/orchestration"
import { RemoteQueryFunction } from "@medusajs/types"
import { TransactionHandlerType } from "@medusajs/utils"
import { TransactionHandlerType, TransactionStepState } from "@medusajs/utils"
import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
import { knex } from "knex"
import { setTimeout } from "timers/promises"
@@ -237,5 +237,64 @@ describe("Workflow Orchestrator module", function () {
TransactionTimeoutError.isTransactionTimeoutError(errors[0].error)
).toBe(true)
})
it("should complete an async workflow that returns a StepResponse", async () => {
const { transaction, result } = await workflowOrcModule.run(
"workflow_async_background",
{
input: {
myInput: "123",
},
transactionId: "transaction_1",
throwOnError: false,
}
)
expect(transaction.flow.state).toEqual(TransactionStepState.INVOKING)
expect(result).toEqual(undefined)
await setTimeout(205)
const trx = await workflowOrcModule.run("workflow_async_background", {
input: {
myInput: "123",
},
transactionId: "transaction_1",
throwOnError: false,
})
expect(trx.transaction.flow.state).toEqual(TransactionStepState.DONE)
expect(trx.result).toEqual({
myInput: "123",
})
})
it("should subsctibe to a async workflow and receive the response when it finishes", (done) => {
const transactionId = "trx_123"
const onFinish = jest.fn(() => {
done()
})
void workflowOrcModule.run("workflow_async_background", {
input: {
myInput: "123",
},
transactionId,
throwOnError: false,
})
void workflowOrcModule.subscribe({
workflowId: "workflow_async_background",
transactionId,
subscriber: (event) => {
if (event.eventType === "onFinish") {
onFinish()
}
},
})
expect(onFinish).toHaveBeenCalledTimes(0)
})
})
})