feat: Ensure async workflow executions have access to shared container (#8157)
* feat: Ensure async workflow executions have access to shared container * fix: Register workflow worker on application start
This commit is contained in:
@@ -6,8 +6,10 @@ import {
|
||||
} from "@medusajs/workflows-sdk"
|
||||
|
||||
export const createScheduled = (name: string, schedule?: SchedulerOptions) => {
|
||||
const workflowScheduledStepInvoke = jest.fn((input, context) => {
|
||||
return new StepResponse({})
|
||||
const workflowScheduledStepInvoke = jest.fn((input, { container }) => {
|
||||
return new StepResponse({
|
||||
testValue: container.resolve("test-value"),
|
||||
})
|
||||
})
|
||||
|
||||
const step = createStep("step_1", workflowScheduledStepInvoke)
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
} from "../__fixtures__/workflow_event_group_id"
|
||||
import { createScheduled } from "../__fixtures__/workflow_scheduled"
|
||||
import { WorkflowsModuleService } from "@services"
|
||||
import { asFunction } from "awilix"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
@@ -367,6 +368,25 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
|
||||
)
|
||||
})
|
||||
|
||||
it("the scheduled workflow should have access to the shared container", async () => {
|
||||
const sharedContainer =
|
||||
workflowOrcModule["workflowOrchestratorService_"]["container_"]
|
||||
|
||||
sharedContainer.register(
|
||||
"test-value",
|
||||
asFunction(() => "test")
|
||||
)
|
||||
|
||||
const spy = await createScheduled("remove-scheduled", {
|
||||
cron: "* * * * * *",
|
||||
})
|
||||
await jest.runOnlyPendingTimersAsync()
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
expect(spy).toHaveReturnedWith(
|
||||
expect.objectContaining({ output: { testValue: "test" } })
|
||||
)
|
||||
})
|
||||
|
||||
it("should fetch an idempotent workflow after its completion", async () => {
|
||||
const { transaction: firstRun } = await workflowOrcModule.run(
|
||||
"workflow_idempotent",
|
||||
|
||||
@@ -80,13 +80,17 @@ const AnySubscriber = "any"
|
||||
|
||||
export class WorkflowOrchestratorService {
|
||||
private subscribers: Subscribers = new Map()
|
||||
private container_: MedusaContainer
|
||||
|
||||
constructor({
|
||||
inMemoryDistributedTransactionStorage,
|
||||
sharedContainer,
|
||||
}: {
|
||||
inMemoryDistributedTransactionStorage: InMemoryDistributedTransactionStorage
|
||||
workflowOrchestratorService: WorkflowOrchestratorService
|
||||
sharedContainer: MedusaContainer
|
||||
}) {
|
||||
this.container_ = sharedContainer
|
||||
inMemoryDistributedTransactionStorage.setWorkflowOrchestratorService(this)
|
||||
DistributedTransaction.setStorage(inMemoryDistributedTransactionStorage)
|
||||
WorkflowScheduler.setStorage(inMemoryDistributedTransactionStorage)
|
||||
@@ -136,7 +140,9 @@ export class WorkflowOrchestratorService {
|
||||
)
|
||||
}
|
||||
|
||||
const flow = exportedWorkflow(container as MedusaContainer)
|
||||
const flow = exportedWorkflow(
|
||||
(container as MedusaContainer) ?? this.container_
|
||||
)
|
||||
|
||||
const ret = await flow.run({
|
||||
input,
|
||||
@@ -191,7 +197,9 @@ export class WorkflowOrchestratorService {
|
||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||
}
|
||||
|
||||
const flow = exportedWorkflow(container as MedusaContainer)
|
||||
const flow = exportedWorkflow(
|
||||
(container as MedusaContainer) ?? this.container_
|
||||
)
|
||||
|
||||
const transaction = await flow.getRunningTransaction(transactionId, context)
|
||||
|
||||
@@ -227,7 +235,9 @@ export class WorkflowOrchestratorService {
|
||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||
}
|
||||
|
||||
const flow = exportedWorkflow(container as MedusaContainer)
|
||||
const flow = exportedWorkflow(
|
||||
(container as MedusaContainer) ?? this.container_
|
||||
)
|
||||
|
||||
const events = this.buildWorkflowEvents({
|
||||
customEventHandlers: eventHandlers,
|
||||
@@ -287,7 +297,9 @@ export class WorkflowOrchestratorService {
|
||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||
}
|
||||
|
||||
const flow = exportedWorkflow(container as MedusaContainer)
|
||||
const flow = exportedWorkflow(
|
||||
(container as MedusaContainer) ?? this.container_
|
||||
)
|
||||
|
||||
const events = this.buildWorkflowEvents({
|
||||
customEventHandlers: eventHandlers,
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
Context,
|
||||
DAL,
|
||||
InternalModuleDeclaration,
|
||||
MedusaContainer,
|
||||
ModulesSdkTypes,
|
||||
WorkflowsSdkTypes,
|
||||
} from "@medusajs/types"
|
||||
@@ -31,6 +32,7 @@ export class WorkflowsModuleService<
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected workflowExecutionService_: ModulesSdkTypes.IMedusaInternalService<TWorkflowExecution>
|
||||
protected workflowOrchestratorService_: WorkflowOrchestratorService
|
||||
protected container_: MedusaContainer
|
||||
|
||||
constructor(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user