chore: ensure the event group id is kept for async workflow (#7668)

* chore: ensure the event group id is kept for async workflow

* revert test

* fix tests

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
Adrien de Peretti
2024-06-11 13:20:17 +02:00
committed by GitHub
parent 3d72002c28
commit afae395fea
6 changed files with 157 additions and 18 deletions

View File

@@ -0,0 +1,40 @@
import { createStep, createWorkflow } from "@medusajs/workflows-sdk"
import { setTimeout } from "timers/promises"
export const workflowEventGroupIdStep1Mock = jest.fn(async (input) => {
await setTimeout(200)
})
export const workflowEventGroupIdStep2Mock = jest.fn(async (input) => {
return
})
const step_1_background = createStep(
{
name: "step_1_event_group_id_background",
async: true,
},
workflowEventGroupIdStep1Mock
)
const step_2 = createStep(
{
name: "step_2_event_group_id",
async: true,
},
workflowEventGroupIdStep2Mock
)
export const eventGroupWorkflowId = "workflow_event_group_id"
createWorkflow(
{
name: eventGroupWorkflowId,
},
function (input) {
const resp = step_1_background(input)
step_2()
return resp
}
)

View File

@@ -1,14 +1,22 @@
import { MedusaApp } from "@medusajs/modules-sdk"
import { RemoteJoinerQuery } from "@medusajs/types"
import { WorkflowManager } from "@medusajs/orchestration"
import {
Context,
IWorkflowEngineService,
RemoteJoinerQuery,
} from "@medusajs/types"
import { TransactionHandlerType } from "@medusajs/utils"
import { IWorkflowEngineService, MedusaWorkflow } from "@medusajs/workflows-sdk"
import { knex } from "knex"
import { setTimeout as setTimeoutPromise } from "timers/promises"
import "../__fixtures__"
import { workflow2Step2Invoke, workflow2Step3Invoke } from "../__fixtures__"
import {
eventGroupWorkflowId,
workflowEventGroupIdStep1Mock,
workflowEventGroupIdStep2Mock,
} from "../__fixtures__/workflow_event_group_id"
import { createScheduled } from "../__fixtures__/workflow_scheduled"
import { DB_URL, TestDatabase } from "../utils"
import { WorkflowManager } from "@medusajs/orchestration"
const sharedPgConnection = knex<any, any>({
client: "pg",
@@ -59,7 +67,68 @@ describe("Workflow Orchestrator module", function () {
workflowOrcModule = modules.workflows as unknown as IWorkflowEngineService
})
afterEach(afterEach_)
it("should execute an async workflow keeping track of the event group id provided in the context", async () => {
const eventGroupId = "event-group-id"
await workflowOrcModule.run(eventGroupWorkflowId, {
input: {},
context: {
eventGroupId,
transactionId: "transaction_id",
},
throwOnError: true,
})
await workflowOrcModule.setStepSuccess({
idempotencyKey: {
action: TransactionHandlerType.INVOKE,
stepId: "step_1_event_group_id_background",
workflowId: eventGroupWorkflowId,
transactionId: "transaction_id",
},
stepResponse: { hey: "oh" },
})
// Validate context event group id
expect(workflowEventGroupIdStep1Mock.mock.calls[0][1]).toEqual(
expect.objectContaining({ eventGroupId })
)
expect(workflowEventGroupIdStep2Mock.mock.calls[0][1]).toEqual(
expect.objectContaining({ eventGroupId })
)
})
it("should execute an async workflow keeping track of the event group id that has been auto generated", async () => {
await workflowOrcModule.run(eventGroupWorkflowId, {
input: {},
context: {
transactionId: "transaction_id_2",
},
throwOnError: true,
})
await workflowOrcModule.setStepSuccess({
idempotencyKey: {
action: TransactionHandlerType.INVOKE,
stepId: "step_1_event_group_id_background",
workflowId: eventGroupWorkflowId,
transactionId: "transaction_id_2",
},
stepResponse: { hey: "oh" },
})
const generatedEventGroupId = (workflowEventGroupIdStep1Mock.mock
.calls[0][1] as unknown as Context)!.eventGroupId
// Validate context event group id
expect(workflowEventGroupIdStep1Mock.mock.calls[0][1]).toEqual(
expect.objectContaining({ eventGroupId: generatedEventGroupId })
)
expect(workflowEventGroupIdStep2Mock.mock.calls[0][1]).toEqual(
expect.objectContaining({ eventGroupId: generatedEventGroupId })
)
})
describe("Testing basic workflow", function () {
it("should return a list of workflow executions and remove after completed when there is no retentionTime set", async () => {
await workflowOrcModule.run("workflow_1", {
@@ -174,7 +243,7 @@ describe("Workflow Orchestrator module", function () {
expect(transaction.flow.state).toEqual("reverted")
})
it("should subsctibe to a async workflow and receive the response when it finishes", (done) => {
it("should subscribe to a async workflow and receive the response when it finishes", (done) => {
const transactionId = "trx_123"
const onFinish = jest.fn(() => {