Skip to content

Commit

Permalink
Don't wait for abandoned child workflow (#202)
Browse files Browse the repository at this point in the history
* Don't wait for abandoned child workflow

* Restore Workflow Test case
  • Loading branch information
seregazhuk authored Jun 29, 2022
1 parent d248301 commit 18bc538
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/Internal/Transport/Request/GetChildWorkflowExecution.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@
namespace Temporal\Internal\Transport\Request;

use Temporal\Worker\Transport\Command\Request;
use Temporal\Workflow\ParentClosePolicy;

final class GetChildWorkflowExecution extends Request
{
public const NAME = 'GetChildWorkflowExecution';
/** @see ParentClosePolicy */
private int $parentClosePolicy;

/**
* @param ExecuteChildWorkflow $execution
*/
public function __construct(ExecuteChildWorkflow $execution)
{
$this->parentClosePolicy = $execution->getOptions()['options']['ParentClosePolicy'] ?? ParentClosePolicy::POLICY_UNSPECIFIED;
parent::__construct(self::NAME, ['id' => $execution->getID()]);
}

/**
* We don't wait for child workflow with parent close policy ABANDON
*/
public function shouldBeWaitedFor(): bool
{
return $this->parentClosePolicy !== ParentClosePolicy::POLICY_ABANDON;
}
}
4 changes: 3 additions & 1 deletion src/Internal/Workflow/Process/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ protected function onRequest(RequestInterface $request, PromiseInterface $promis
$cancelID = $this->cancelID;

// do not close the scope until all promises are complete
$this->awaitLock++;
if ($request->shouldBeWaitedFor()) {
$this->awaitLock++;
}

// do not cancel already complete promises
$cleanup = function () use ($cancelID): void {
Expand Down
5 changes: 5 additions & 0 deletions src/Worker/Transport/Command/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public function getFailure(): ?\Throwable
{
return $this->failure;
}

public function shouldBeWaitedFor(): bool
{
return true;
}
}
2 changes: 2 additions & 0 deletions src/Worker/Transport/Command/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public function getPayloads(): ValuesInterface;
* @return \Throwable|null
*/
public function getFailure(): ?\Throwable;

public function shouldBeWaitedFor(): bool;
}
22 changes: 22 additions & 0 deletions tests/Fixtures/src/Workflow/AbandonedChildWithTimerWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Workflow;

use Carbon\CarbonInterval;
use Temporal\Activity\ActivityOptions;
use Temporal\Common\RetryOptions;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;

#[WorkflowInterface]
class AbandonedChildWithTimerWorkflow
{
#[WorkflowMethod]
public function wait(int $timeoutInSeconds)
{
Workflow::timer($timeoutInSeconds);
}
}
29 changes: 29 additions & 0 deletions tests/Fixtures/src/Workflow/ParentWithAbandonedChildWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Workflow;

use Temporal\Workflow;
use Temporal\Workflow\ChildWorkflowOptions;
use Temporal\Workflow\ParentClosePolicy;
use Temporal\Workflow\WorkflowMethod;


#[Workflow\WorkflowInterface]
class ParentWithAbandonedChildWorkflow
{
#[WorkflowMethod]
public function start(int $childTimeoutInSeconds)
{
$child = Workflow::newChildWorkflowStub(
AbandonedChildWithTimerWorkflow::class,
ChildWorkflowOptions::new()
->withParentClosePolicy(ParentClosePolicy::POLICY_ABANDON)
);

$child->wait($childTimeoutInSeconds);

return 'Welcome from parent';
}
}
25 changes: 25 additions & 0 deletions tests/Functional/Client/AbandonedChildWorkflowTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Functional\Client;

use Temporal\Testing\WithoutTimeSkipping;
use Temporal\Testing\WorkflowTestCase;
use Temporal\Tests\Workflow\ParentWithAbandonedChildWorkflow;

final class AbandonedChildWorkflowTestCase extends WorkflowTestCase
{
use WithoutTimeSkipping;

public function testParentEndsWithoutWaitingForChild(): void
{
$timeBeforeStart = $this->testingService->getCurrentTime();
/** @var ParentWithAbandonedChildWorkflow $parentWorkflow */
$parentWorkflow = $this->workflowClient->newWorkflowStub(ParentWithAbandonedChildWorkflow::class);
$parentWorkflow->start(10);
$timeAfterStart = $this->testingService->getCurrentTime();
static::assertTrue($timeAfterStart->diffInSeconds($timeBeforeStart) < 2);
}
}

5 changes: 0 additions & 5 deletions tests/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

declare(strict_types=1);

use Temporal\DataConverter\DataConverter;
use Temporal\WorkerFactory;
use Temporal\Worker\Transport\RoadRunner;
use Temporal\Worker\Transport\Goridge;
use Temporal\Tests;
use Spiral\Goridge\Relay;

require __DIR__ . '/../vendor/autoload.php';

Expand Down

0 comments on commit 18bc538

Please sign in to comment.