Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/ChildWorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use function React\Promise\resolve;
use RuntimeException;
use Throwable;
use Workflow\Exceptions\TransitionNotFound;
use Workflow\Serializers\Serializer;
use Workflow\States\WorkflowFailedStatus;

final class ChildWorkflowStub
{
Expand Down Expand Up @@ -44,9 +47,41 @@ public static function make($workflow, ...$arguments): PromiseInterface
}

if ($log) {
++$context->index;
WorkflowStub::setContext($context);
return resolve(Serializer::unserialize($log->result));
$result = Serializer::unserialize($log->result);
if (
is_array($result)
&& array_key_exists('class', $result)
&& is_subclass_of($result['class'], Throwable::class)
) {
if (! $context->replaying) {
$storedChildWorkflow = $context->storedWorkflow->children()
->wherePivot('parent_index', $context->index)
->first();
if ($storedChildWorkflow && $storedChildWorkflow->status::class !== WorkflowFailedStatus::class) {
$log->delete();
$log = null;
}
}

if ($log) {
++$context->index;
WorkflowStub::setContext($context);
try {
$throwable = new $result['class']($result['message'] ?? '', (int) ($result['code'] ?? 0));
} catch (Throwable $throwable) {
throw new RuntimeException(
sprintf('[%s] %s', $result['class'], (string) ($result['message'] ?? '')),
(int) ($result['code'] ?? 0),
$throwable
);
}
throw $throwable;
}
} else {
++$context->index;
WorkflowStub::setContext($context);
return resolve($result);
}
}

if (! $context->replaying) {
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handle()
try {
if ($this->storedWorkflow->hasLogByIndex($this->index)) {
$workflow->resume();
} else {
} elseif (! $this->storedWorkflow->logs()->where('class', self::class)->exists()) {
$workflow->next($this->index, $this->now, self::class, $this->exception);
}
} catch (TransitionNotFound) {
Expand Down
40 changes: 36 additions & 4 deletions src/WorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,44 @@ public function fail($exception): void

$this->storedWorkflow->parents()
->each(static function ($parentWorkflow) use ($exception) {
try {
$parentWorkflow->toWorkflow()
->fail($exception);
} catch (TransitionNotFound) {
if (
$parentWorkflow->pivot->parent_index === StoredWorkflow::CONTINUE_PARENT_INDEX
|| $parentWorkflow->pivot->parent_index === StoredWorkflow::ACTIVE_WORKFLOW_INDEX
) {
try {
$parentWorkflow->toWorkflow()
->fail($exception);
} catch (TransitionNotFound) {
return;
}
return;
}

$file = new SplFileObject($exception->getFile());
$iterator = new LimitIterator($file, max(0, $exception->getLine() - 4), 7);

$throwable = [
'class' => get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'line' => $exception->getLine(),
'file' => $exception->getFile(),
'trace' => collect($exception->getTrace())
->filter(static fn ($trace) => Serializer::serializable($trace))
->toArray(),
'snippet' => array_slice(iterator_to_array($iterator), 0, 7),
];

$parentWf = $parentWorkflow->toWorkflow();

Exception::dispatch(
$parentWorkflow->pivot->parent_index,
$parentWorkflow->pivot->parent_now,
$parentWorkflow,
$throwable,
$parentWf->connection(),
$parentWf->queue()
);
});
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Feature/SagaChildWorkflowTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Tests\Feature;

use Tests\Fixtures\TestSagaChildWorkflow;
use Tests\Fixtures\TestSagaSingleChildWorkflow;
use Tests\TestCase;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\WorkflowStub;

final class SagaChildWorkflowTest extends TestCase
{
public function testSingleChildExceptionTriggersCompensation(): void
{
$workflow = WorkflowStub::make(TestSagaSingleChildWorkflow::class);

$workflow->start();

while ($workflow->running());

$this->assertSame(WorkflowCompletedStatus::class, $workflow->status());
$this->assertSame('compensated', $workflow->output());
}

public function testParallelChildExceptionsTriggersCompensation(): void
{
$workflow = WorkflowStub::make(TestSagaChildWorkflow::class);

$workflow->start();

while ($workflow->running());

$this->assertSame(WorkflowCompletedStatus::class, $workflow->status());
$this->assertSame('compensated', $workflow->output());
}
}
16 changes: 16 additions & 0 deletions tests/Fixtures/TestChildExceptionThrowingWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Exception;
use Workflow\Workflow;

class TestChildExceptionThrowingWorkflow extends Workflow
{
public function execute()
{
throw new Exception('child failed');
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/TestRequiredArgException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Exception;

class TestRequiredArgException extends Exception
{
public function __construct(
string $message,
int $code,
private readonly string $requiredContext,
) {
parent::__construct($message, $code);
}
}
33 changes: 33 additions & 0 deletions tests/Fixtures/TestSagaChildWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Workflow\Workflow;
use function Workflow\{activity, all, child};

class TestSagaChildWorkflow extends Workflow
{
public function execute()
{
try {
yield activity(TestActivity::class);
$this->addCompensation(static fn () => activity(TestUndoActivity::class));

$children = [
child(TestChildExceptionThrowingWorkflow::class),
child(TestChildExceptionThrowingWorkflow::class),
child(TestChildExceptionThrowingWorkflow::class),
];

yield all($children);

return 'success';
} catch (\Throwable $th) {
yield from $this->compensate();

return 'compensated';
}
}
}
27 changes: 27 additions & 0 deletions tests/Fixtures/TestSagaSingleChildWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Workflow\Workflow;
use function Workflow\{activity, child};

class TestSagaSingleChildWorkflow extends Workflow
{
public function execute()
{
try {
yield activity(TestActivity::class);
$this->addCompensation(static fn () => activity(TestUndoActivity::class));

yield child(TestChildExceptionThrowingWorkflow::class);

return 'success';
} catch (\Throwable $th) {
yield from $this->compensate();

return 'compensated';
}
}
}
Loading