chore: local workflow proxying methods to pass context (#6263)

What:
- When calling a module's method inside a Local Workflow the MedusaContext is passed as the last argument to the method if not provided
- Add `requestId` to req
- A couple of fixes on Remote Joiner and the data fetcher for internal services

Why:
- The context used to initialize the workflow has to be shared with all modules. properties like transactionId will be used to emit events and requestId to trace logs for example.
This commit is contained in:
Carlos R. L. Rodrigues authored and GitHub committed 2024-02-01 13:37:26 +00:00
1 parent a2bf6756ac
commit 45134e4d11
40 files changed
+576 -170

No files matched your search

@@ -9,6 +9,7 @@ import {
import { Context, LoadedModule, MedusaContainer } from "@medusajs/types"
import { MedusaModule } from "@medusajs/modules-sdk"
import { MedusaContextType } from "@medusajs/utils"
import { EOL } from "os"
import { ulid } from "ulid"
import { MedusaWorkflow } from "../medusa-workflow"
@@ -159,7 +160,7 @@ function createContextualWorkflowRunner<
)
}
const flow = new LocalWorkflow(workflowId, container)
const flow = new LocalWorkflow(workflowId, container!)
const originalRun = flow.run.bind(flow)
const originalRegisterStepSuccess = flow.registerStepSuccess.bind(flow)
@@ -197,7 +198,13 @@ function createContextualWorkflowRunner<
}
const newRun = async (
{ input, context, throwOnError, resultFrom, events }: FlowRunOptions = {
{
input,
context: outerContext,
throwOnError,
resultFrom,
events,
}: FlowRunOptions = {
throwOnError: true,
resultFrom: defaultResult,
}
@@ -205,6 +212,13 @@ function createContextualWorkflowRunner<
resultFrom ??= defaultResult
throwOnError ??= true
const context = {
...outerContext,
__type: MedusaContextType,
}
context.transactionId ??= ulid()
if (typeof dataPreparation === "function") {
try {
const copyInput = input ? JSON.parse(JSON.stringify(input)) : input
@@ -224,7 +238,7 @@ function createContextualWorkflowRunner<
return await originalExecution(
originalRun,
{ throwOnError, resultFrom },
context?.transactionId ?? ulid(),
context.transactionId,
input,
context,
events
@@ -236,7 +250,7 @@ function createContextualWorkflowRunner<
{
response,
idempotencyKey,
context,
context: outerContext,
throwOnError,
resultFrom,
events,
@@ -249,6 +263,13 @@ function createContextualWorkflowRunner<
resultFrom ??= defaultResult
throwOnError ??= true
const [, transactionId] = idempotencyKey.split(":")
const context = {
...outerContext,
transactionId,
__type: MedusaContextType,
}
return await originalExecution(
originalRegisterStepSuccess,
{ throwOnError, resultFrom },
@@ -264,7 +285,7 @@ function createContextualWorkflowRunner<
{
response,
idempotencyKey,
context,
context: outerContext,
throwOnError,
resultFrom,
events,
@@ -277,6 +298,13 @@ function createContextualWorkflowRunner<
resultFrom ??= defaultResult
throwOnError ??= true
const [, transactionId] = idempotencyKey.split(":")
const context = {
...outerContext,
transactionId,
__type: MedusaContextType,
}
return await originalExecution(
originalRegisterStepFailure,
{ throwOnError, resultFrom },