Skip to content

Commit 3b35222

Browse files
authored
Expand documentation on side effects and signals
1 parent 2f13f64 commit 3b35222

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

docs/features/side-effects.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,39 @@ class MyWorkflow extends Workflow
2424
The workflow will only call `random_int()` once and save the result, even if the workflow later fails and is retried.
2525

2626
**Important:** The code inside a side effect should never fail because it will not be retried. Code that can possibly fail and therefore need to be retried should be moved to an activity instead.
27+
28+
## Signals and Control Flow
29+
30+
Signals are applied before the workflow continues execution. That means a signal-mutated variable can break replays unless the control flow decision is recorded deterministically.
31+
32+
Use `sideEffect` to snapshot any signal-driven branch decision:
33+
34+
```php
35+
use function Workflow\{activity, sideEffect};
36+
use Exception;
37+
use Workflow\SignalMethod;
38+
use Workflow\Workflow;
39+
40+
class MyWorkflow extends Workflow
41+
{
42+
private bool $cancelled = false;
43+
44+
#[SignalMethod]
45+
public function cancel()
46+
{
47+
$this->cancelled = true;
48+
}
49+
50+
public function execute()
51+
{
52+
while (true) {
53+
yield activity(MyActivity::class);
54+
55+
if (yield sideEffect(fn () => $this->cancelled)) {
56+
throw new Exception('Workflow cancelled by signal.');
57+
}
58+
}
59+
}
60+
```
61+
62+
This keeps control flow replay-safe and avoids non-deterministic branching during signal handling.

0 commit comments

Comments
 (0)