Reset workflow execution history to handle long-running workflows efficiently
Continue as New allows long-running workflows to reset their execution history by starting a new workflow instance with fresh state while maintaining logical continuity.
Workflows maintain an execution history of all activities, timers, and signals. For long-running workflows (especially infinite loops), this history can grow unbounded. Continue as New solves this by completing the current workflow and starting a new instance with the same workflow class.
Use continueAsNew() to reset history periodically:
use function Workflow\{activity, continueAsNew};// ✓ History stays boundedclass DailyReportWorkflow extends Workflow{ public function execute($day = 0) { yield activity(GenerateReportActivity::class, $day); yield timer('24 hours'); // Start fresh with incremented day return yield continueAsNew($day + 1); }}
Each continueAsNew() creates a new workflow instance with a fresh history. The old workflow completes successfully.
Continue as New properly maintains parent-child relationships:
class ParentWorkflow extends Workflow{ public function execute() { // Child uses continue as new $result = yield child(LongRunningChildWorkflow::class); // Parent receives result from the final continued workflow return $result; }}class LongRunningChildWorkflow extends Workflow{ public function execute($iteration = 0) { yield activity(ProcessActivity::class, $iteration); if ($iteration >= 10) { return 'child_completed'; } // Parent waits for the entire chain return yield continueAsNew($iteration + 1); }}
The parent workflow waits for all continued instances to complete. The final result is returned to the parent.
Query the workflow relationship to track the chain:
use Workflow\Models\StoredWorkflow;// Get the current active workflow in a chain$workflow = WorkflowStub::find($originalWorkflowId);$activeWorkflow = $workflow->storedWorkflow ->children() ->wherePivot('parent_index', StoredWorkflow::ACTIVE_WORKFLOW_INDEX) ->first();if ($activeWorkflow) { echo "Currently running: " . $activeWorkflow->id;} else { echo "Original workflow still running";}
// Don't continue after every single operationpublic function execute($count = 0) { yield activity(QuickTask::class); return yield continueAsNew($count + 1); // Too frequent!}
// Continue after meaningful batchespublic function execute($count = 0) { for ($i = 0; $i < 100; $i++) { yield activity(QuickTask::class, $count + $i); } return yield continueAsNew($count + 100);}