Skip to content

Commit

Permalink
Fix: abandoned child workflow start (#238)
Browse files Browse the repository at this point in the history
* Fix: abandoned child workflow start

* Fix: abandoned child workflow start

* Fix: abandoned child workflow start

* Fix: abandoned child workflow start
  • Loading branch information
seregazhuk authored Aug 25, 2022
1 parent 5e15c13 commit f2d6dd7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/Internal/Workflow/ChildWorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ public function getExecution(): PromiseInterface
return $this->execution->promise();
}

public function start(... $args): PromiseInterface
{
if ($this->request !== null) {
throw new \LogicException('Child workflow already has been executed');
}

$this->request = new ExecuteChildWorkflow(
$this->workflow,
EncodedValues::fromValues($args),
$this->getOptionsArray()
);

return $this->request($this->request);
}

/**
* {@inheritDoc}
*/
Expand Down
19 changes: 19 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Temporal\Workflow\ChildWorkflowStubInterface;
use Temporal\Workflow\ContinueAsNewOptions;
use Temporal\Workflow\ExternalWorkflowStubInterface;
use Temporal\Workflow\ParentClosePolicy;
use Temporal\Workflow\ScopedContextInterface;
use Temporal\Internal\Workflow\WorkflowContext;
use Temporal\Workflow\WorkflowExecution;
Expand Down Expand Up @@ -634,6 +635,7 @@ public static function executeChildWorkflow(
* This method is equivalent to {@see Workflow::executeChildWorkflow}, but
* it takes the workflow class as the first argument, and the further api
* is built on the basis of calls to the methods of the passed workflow.
* For starting abandon child workflow {@see Workflow::newUntypedChildWorkflowStub()}.
*
* <code>
* // Any workflow interface example:
Expand Down Expand Up @@ -696,6 +698,23 @@ public static function newChildWorkflowStub(string $class, ChildWorkflowOptions
* }
* </code>
*
* To start abandoned child workflow use `yield` and method `start()`:
*
* <code>
* #[WorkflowMethod]
* public function handler()
* {
* // ExampleWorkflow proxy
* $workflow = Workflow::newUntypedChildWorkflowStub(
* 'WorkflowName',
* ChildWorkflowOptions::new()->withParentClosePolicy(ParentClosePolicy::POLICY_ABANDON)
* );
*
* // Start child workflow
* yield $workflow->start(42);
* }
* </code>
*
* @param string $name
* @param ChildWorkflowOptions|null $options
* @return ChildWorkflowStubInterface
Expand Down
6 changes: 6 additions & 0 deletions src/Workflow/ChildWorkflowStubInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public function getOptions(): ChildWorkflowOptions;
*/
public function execute(array $args = [], $returnType = null): PromiseInterface;

/**
* @param array $args
* @return CompletableResultInterface
*/
public function start(... $args): PromiseInterface;

/**
* @param string $name
* @param array $args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#[WorkflowInterface]
class AbandonedChildWithTimerWorkflow
{
#[WorkflowMethod]
#[WorkflowMethod('abandoned_workflow')]
public function wait(int $timeoutInSeconds)
{
Workflow::timer($timeoutInSeconds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class ParentWithAbandonedChildWorkflow
#[WorkflowMethod]
public function start(int $childTimeoutInSeconds)
{
$child = Workflow::newChildWorkflowStub(
AbandonedChildWithTimerWorkflow::class,
$child = Workflow::newUntypedChildWorkflowStub(
'abandoned_workflow',
ChildWorkflowOptions::new()
->withParentClosePolicy(ParentClosePolicy::POLICY_ABANDON)
);

$child->wait($childTimeoutInSeconds);
yield $child->start($childTimeoutInSeconds);

return 'Welcome from parent';
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/Client/AwaitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Temporal\Exception\Failure\ActivityFailure;
use Temporal\Exception\Failure\ApplicationFailure;
use Temporal\Exception\Failure\CanceledFailure;
use Temporal\Testing\WithoutTimeSkipping;
use Temporal\Tests\Workflow\AggregatedWorkflow;
use Temporal\Tests\Workflow\LoopWithSignalCoroutinesWorkflow;
use Temporal\Tests\Workflow\LoopWorkflow;
Expand All @@ -28,6 +29,8 @@
*/
class AwaitTestCase extends ClientTestCase
{
use WithoutTimeSkipping;

public function testSimpleAwait()
{
$client = $this->createClient();
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/Client/ExternalWorkflowTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

use Temporal\Exception\Client\WorkflowFailedException;
use Temporal\Exception\Failure\CanceledFailure;
use Temporal\Testing\WithoutTimeSkipping;
use Temporal\Tests\Workflow\LoopKillerWorkflow;
use Temporal\Tests\Workflow\LoopSignallingWorkflow;
use Temporal\Tests\Workflow\LoopWorkflow;

class ExternalWorkflowTestCase extends ClientTestCase
{
use WithoutTimeSkipping;

public function testSignalWorkflowExecution()
{
$client = $this->createClient();
Expand Down

0 comments on commit f2d6dd7

Please sign in to comment.