diff --git a/src/Traits/Continues.php b/src/Traits/Continues.php index 2cfcff6b..2294b63a 100644 --- a/src/Traits/Continues.php +++ b/src/Traits/Continues.php @@ -16,8 +16,25 @@ public static function continueAsNew(...$arguments): PromiseInterface $context = self::$context; if (! $context->replaying) { + $parentWorkflow = $context->storedWorkflow->parents() + ->wherePivot('parent_index', '!=', StoredWorkflow::CONTINUE_PARENT_INDEX) + ->wherePivot('parent_index', '!=', StoredWorkflow::ACTIVE_WORKFLOW_INDEX) + ->withPivot('parent_index') + ->first(); + $newWorkflow = self::make($context->storedWorkflow->class); - $newWorkflow->start(...$arguments); + + if ($parentWorkflow) { + $parentWorkflow->children() + ->attach($newWorkflow->storedWorkflow, [ + 'parent_index' => $parentWorkflow->pivot->parent_index, + 'parent_now' => $context->now, + ]); + + $parentWorkflow->children() + ->wherePivot('parent_index', $parentWorkflow->pivot->parent_index) + ->detach($context->storedWorkflow); + } $newWorkflow->storedWorkflow->parents() ->attach($context->storedWorkflow, [ @@ -45,6 +62,8 @@ public static function continueAsNew(...$arguments): PromiseInterface 'parent_now' => $context->now, ]); } + + $newWorkflow->start(...$arguments); } self::$context = $context; diff --git a/tests/Feature/ParentContinueAsNewChildWorkflowTest.php b/tests/Feature/ParentContinueAsNewChildWorkflowTest.php new file mode 100644 index 00000000..78961e80 --- /dev/null +++ b/tests/Feature/ParentContinueAsNewChildWorkflowTest.php @@ -0,0 +1,25 @@ +start(); + + while ($workflow->running()); + + $this->assertEquals(WorkflowCompletedStatus::class, $workflow->status()); + $this->assertEquals('parent_child_done', $workflow->output()); + } +} diff --git a/tests/Fixtures/TestChildContinueAsNewWorkflow.php b/tests/Fixtures/TestChildContinueAsNewWorkflow.php new file mode 100644 index 00000000..e61faf40 --- /dev/null +++ b/tests/Fixtures/TestChildContinueAsNewWorkflow.php @@ -0,0 +1,19 @@ += $totalCount) { + return 'child_done'; + } + return yield WorkflowStub::continueAsNew($count + 1, $totalCount); + } +} diff --git a/tests/Fixtures/TestParentContinueAsNewChildWorkflow.php b/tests/Fixtures/TestParentContinueAsNewChildWorkflow.php new file mode 100644 index 00000000..3ead2e82 --- /dev/null +++ b/tests/Fixtures/TestParentContinueAsNewChildWorkflow.php @@ -0,0 +1,17 @@ +