Skip to content

Commit a04188a

Browse files
authored
Document 'Continue As New' pattern in Laravel Workflow
Added documentation for the 'Continue As New' pattern in Laravel Workflow, including usage examples and benefits.
1 parent c2f2593 commit a04188a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/features/continue-as-new.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
sidebar_position: 12
3+
---
4+
5+
# Continue As New
6+
7+
Laravel Workflow supports the **Continue As New** pattern, allowing a running workflow to restart itself with new arguments.
8+
This is useful when you need to:
9+
10+
* Prevent unbounded workflow history growth.
11+
* Model iterative loops or recursive workflows.
12+
* Split long-running workflows into smaller, manageable executions while preserving continuity.
13+
14+
## Using `continueAsNew`
15+
16+
To restart a workflow as new, call the static method `WorkflowStub::continueAsNew(...)` from within the workflow’s `execute()` method.
17+
18+
```php
19+
use Workflow\ActivityStub;
20+
use Workflow\Workflow;
21+
use Workflow\WorkflowStub;
22+
23+
class CounterWorkflow extends Workflow
24+
{
25+
public function execute(int $count = 0, int $max = 3)
26+
{
27+
$result = yield ActivityStub::make(CountActivity::class, $count);
28+
29+
if ($count >= $max) {
30+
return 'workflow_' . $result;
31+
}
32+
33+
return yield WorkflowStub::continueAsNew($count + 1, $max);
34+
}
35+
}
36+
```
37+
38+
In this example:
39+
40+
* The workflow executes an activity each iteration.
41+
* If the maximum count has not been reached, it continues as new with incremented arguments.
42+
* The final result is returned only when the loop completes.

0 commit comments

Comments
 (0)