Skip to content

Commit 1f23ea0

Browse files
authored
Enhance child workflows documentation (#5)
Added documentation on signaling child workflows, including methods for getting child handles, handling multiple children, forwarding signals, and accessing child workflow IDs.
1 parent 349156d commit 1f23ea0

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

docs/features/child-workflows.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,117 @@ class ParentWorkflow extends Workflow
2121
}
2222
```
2323

24+
## Signaling Child Workflows
25+
26+
Parent workflows can signal their child workflows to coordinate behavior or pass data. To signal a child safely without corrupting the parent's execution context, use the `child()` or `children()` methods.
27+
28+
### Getting a Child Handle
29+
30+
The `child()` method returns a `ChildWorkflowHandle` for the most recently created child workflow:
31+
32+
```php
33+
use Workflow\ChildWorkflowStub;
34+
use Workflow\Workflow;
35+
36+
class ParentWorkflow extends Workflow
37+
{
38+
public function execute()
39+
{
40+
$childPromise = ChildWorkflowStub::make(ChildWorkflow::class);
41+
42+
$childHandle = $this->child();
43+
44+
$childHandle->approve('approved');
45+
46+
$result = yield $childPromise;
47+
48+
return $result;
49+
}
50+
}
51+
```
52+
53+
### Multiple Children
54+
55+
Use the `children()` method to get handles for all child workflows created by the parent:
56+
57+
```php
58+
use Workflow\ChildWorkflowStub;
59+
use Workflow\Workflow;
60+
61+
class ParentWorkflow extends Workflow
62+
{
63+
public function execute()
64+
{
65+
$child1 = ChildWorkflowStub::make(ChildWorkflow::class, 'first');
66+
$child2 = ChildWorkflowStub::make(ChildWorkflow::class, 'second');
67+
$child3 = ChildWorkflowStub::make(ChildWorkflow::class, 'third');
68+
69+
$childHandles = $this->children();
70+
71+
foreach ($childHandles as $childHandle) {
72+
$childHandle->approve('approved');
73+
}
74+
75+
$results = yield ChildWorkflowStub::all([$child1, $child2, $child3]);
76+
77+
return $results;
78+
}
79+
}
80+
```
81+
82+
The `children()` method returns children in reverse chronological order (most recently created first).
83+
84+
### Forwarding Signals to Children
85+
86+
You can forward external signals to child workflows by combining signal methods with child handles:
87+
88+
```php
89+
use Workflow\ChildWorkflowStub;
90+
use Workflow\SignalMethod;
91+
use Workflow\Workflow;
92+
use Workflow\WorkflowStub;
93+
94+
class ParentWorkflow extends Workflow
95+
{
96+
private bool $receivedApproval = false;
97+
private ?string $approvalStatus = null;
98+
99+
#[SignalMethod]
100+
public function approve(string $status): void
101+
{
102+
$this->receivedApproval = true;
103+
$this->approvalStatus = $status;
104+
}
105+
106+
public function execute()
107+
{
108+
$childPromise = ChildWorkflowStub::make(ChildWorkflow::class);
109+
110+
$childHandle = $this->child();
111+
112+
yield WorkflowStub::await(fn () => $this->receivedApproval);
113+
114+
if ($childHandle && $this->approvalStatus) {
115+
$childHandle->processApproval($this->approvalStatus);
116+
}
117+
118+
$result = yield $childPromise;
119+
120+
return $result;
121+
}
122+
}
123+
```
124+
125+
Always call `$this->child()` or `$this->children()` in the `execute()` method. Never call these methods in signal methods or query methods, as this violates determinism during workflow replay.
126+
127+
### Getting Child Workflow IDs
128+
129+
You can access the underlying stored workflow ID using the `id()` method. This allows you to store the ID for external systems to signal the child directly.
130+
131+
```php
132+
yield ActivityStub::make(StoreWorkflowIdActivity::class, $this->child()->id());
133+
```
134+
24135
## Async Activities
25136

26137
Rather than creating a child workflow, you can pass a callback to `ActivityStub::async()` and it will be executed in the context of a separate workflow.

0 commit comments

Comments
 (0)