Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/forward-parent-pointer-discard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/workflow": patch
---

Forward the parent pointer when spawning a child workflow with `discard: true`
60 changes: 60 additions & 0 deletions packages/cluster/test/ClusterWorkflowEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,36 @@ describe.concurrent("ClusterWorkflowEngine", () => {

assert.isTrue(flags.get("catch"))
}).pipe(Effect.provide(TestWorkflowLayer)))

it.effect("forwards parent pointer when spawning a child with discard:true", () =>
Effect.gen(function*() {
const driver = yield* MessageStorage.MemoryDriver
const fiber = yield* DiscardParentWorkflow.execute({ id: "discard-parent-1" }).pipe(
Effect.fork
)
yield* TestClock.adjust(1)
yield* Fiber.join(fiber)

const findRun = (entityType: string) =>
driver.journal.find(
(envelope) =>
envelope._tag === "Request" &&
envelope.address.entityType === entityType &&
envelope.tag === "run"
)
const parentRun = findRun("Workflow/DiscardParentWorkflow")
const childRun = findRun("Workflow/DiscardChildWorkflow")
assert.exists(parentRun, "expected a run envelope for the parent workflow")
assert.exists(childRun, "expected a run envelope for the child workflow")

const childPayload = (childRun as { payload: Record<string, unknown> }).payload
const parent = childPayload["~@effect/workflow/parent"] as
| { workflowName: string; executionId: string }
| undefined
assert.exists(parent, "child payload should carry the parent pointer")
expect(parent!.workflowName).toEqual("DiscardParentWorkflow")
expect(parent!.executionId).toEqual((parentRun as { address: { entityId: string } }).address.entityId)
}).pipe(Effect.provide(TestWorkflowLayer)))
})

const TestShardingConfig = ShardingConfig.layer({
Expand Down Expand Up @@ -498,6 +528,34 @@ const ChildWorkflowLayer = ChildWorkflow.toLayer(Effect.fnUntraced(function*() {
flags.set("child-end", true)
}))

const DiscardParentWorkflow = Workflow.make({
name: "DiscardParentWorkflow",
payload: { id: Schema.String },
idempotencyKey(payload) {
return payload.id
}
})

const DiscardChildWorkflow = Workflow.make({
name: "DiscardChildWorkflow",
payload: { id: Schema.String },
idempotencyKey(payload) {
return payload.id
}
})

const DiscardParentWorkflowLayer = DiscardParentWorkflow.toLayer(
Effect.fnUntraced(function*({ id }) {
yield* DiscardChildWorkflow.execute({ id: `${id}-child` }, { discard: true })
})
)

const DiscardChildWorkflowLayer = DiscardChildWorkflow.toLayer(
Effect.fnUntraced(function*() {
return yield* Effect.void
})
)

const SuspendOnFailureWorkflow = Workflow.make({
name: "SuspendOnFailureWorkflow",
payload: {
Expand Down Expand Up @@ -556,6 +614,8 @@ const TestWorkflowLayer = EmailWorkflowLayer.pipe(
Layer.merge(DurableRaceWorkflowLayer),
Layer.merge(ParentWorkflowLayer),
Layer.merge(ChildWorkflowLayer),
Layer.merge(DiscardParentWorkflowLayer),
Layer.merge(DiscardChildWorkflowLayer),
Layer.merge(SuspendOnFailureWorkflowLayer),
Layer.merge(CatchWorkflowLayer),
Layer.provideMerge(Flags.Default),
Expand Down
3 changes: 2 additions & 1 deletion packages/workflow/src/WorkflowEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ export const makeUnsafe = (options: Encoded): WorkflowEngine["Type"] =>
yield* options.execute(self, {
executionId,
payload: payload as object,
discard: true
discard: true,
parent: Option.getOrUndefined(parentInstance)
})
return executionId
}
Expand Down
Loading