feat: Ensure the event group id is either generated or picked up from the context (#7646)

**What**
- Pick the context event group id or generate it to be accessible from the workflow and down the module calls

FIXES CORE-2273
This commit is contained in:
Adrien de Peretti
2024-06-07 11:14:02 +02:00
committed by GitHub
parent 987141ab2d
commit 3cd2d60daa
2 changed files with 54 additions and 1 deletions

View File

@@ -24,6 +24,52 @@ describe("Workflow composer", function () {
describe("Using steps returning plain values", function () {
afterEach(afterEach_)
it("should compose a workflow and pass down the event group id provided as part of the context", async () => {
let context
const mockStep1Fn = jest
.fn()
.mockImplementation((input, { context: stepContext }) => {
context = stepContext
})
const step1 = createStep("step1", mockStep1Fn)
const workflow = createWorkflow("workflow1", function (input) {
step1(input)
})
await workflow().run({
context: {
eventGroupId: "event-group-id",
},
})
expect(mockStep1Fn).toHaveBeenCalledTimes(1)
expect(mockStep1Fn.mock.calls[0]).toHaveLength(2)
expect(context.eventGroupId).toEqual("event-group-id")
})
it("should compose a workflow and pass down the autogenerated event group id if not provided as part of the context", async () => {
let context
const mockStep1Fn = jest
.fn()
.mockImplementation((input, { context: stepContext }) => {
context = stepContext
})
const step1 = createStep("step1", mockStep1Fn)
const workflow = createWorkflow("workflow1", function (input) {
step1(input)
})
await workflow().run({})
expect(mockStep1Fn).toHaveBeenCalledTimes(1)
expect(mockStep1Fn.mock.calls[0]).toHaveLength(2)
expect(context.eventGroupId).toBeTruthy()
})
it("should compose a new workflow composed retryable steps", async () => {
const maxRetries = 1

View File

@@ -5,7 +5,7 @@ import {
TransactionState,
} from "@medusajs/orchestration"
import { LoadedModule, MedusaContainer } from "@medusajs/types"
import { MedusaContextType, isPresent } from "@medusajs/utils"
import { isPresent, MedusaContextType } from "@medusajs/utils"
import { EOL } from "os"
import { ulid } from "ulid"
import { MedusaWorkflow } from "../medusa-workflow"
@@ -140,6 +140,7 @@ function createContextualWorkflowRunner<
}
context.transactionId ??= ulid()
context.eventGroupId ??= ulid()
if (typeof dataPreparation === "function") {
try {
@@ -193,6 +194,8 @@ function createContextualWorkflowRunner<
__type: MedusaContextType,
}
context.eventGroupId ??= ulid()
return await originalExecution(
originalRegisterStepSuccess,
{ throwOnError, resultFrom, container },
@@ -229,6 +232,8 @@ function createContextualWorkflowRunner<
__type: MedusaContextType,
}
context.eventGroupId ??= ulid()
return await originalExecution(
originalRegisterStepFailure,
{ throwOnError, resultFrom, container },
@@ -260,6 +265,8 @@ function createContextualWorkflowRunner<
__type: MedusaContextType,
}
context.eventGroupId ??= ulid()
return await originalExecution(
originalCancel,
{