Skip to content
Merged
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
9 changes: 9 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Quality Cycle

Run these commands in order before considering any change complete:

1. `composer ecs` — Fix code style (auto-fixes)
2. `composer stan` — Static analysis (must pass with no errors)
3. `composer unit` — Unit tests (must all pass)
4. `composer coverage` — Unit tests with coverage (must be 100%)
5. `composer feature` — Feature tests (must all pass)
21 changes: 20 additions & 1 deletion src/ChildWorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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;

Expand Down Expand Up @@ -46,7 +48,24 @@ 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)
) {
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;
}
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
8 changes: 8 additions & 0 deletions tests/Feature/ParentWorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ public function testRetry(): void
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
$storedWorkflow->status = WorkflowCreatedStatus::class;
$storedWorkflow->save();
$storedWorkflow->logs()
->delete();
$storedWorkflow->exceptions()
->delete();

$storedChildWorkflow = StoredWorkflow::findOrFail($workflow->id() + 1);
$storedChildWorkflow->status = WorkflowCreatedStatus::class;
$storedChildWorkflow->save();
$storedChildWorkflow->logs()
->delete();
$storedChildWorkflow->exceptions()
->delete();

$workflow->fresh()
->start(shouldThrow: false);
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());
}
}
13 changes: 13 additions & 0 deletions tests/Feature/SagaWorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Feature;

use Tests\Fixtures\TestActivity;
use Tests\Fixtures\TestSagaParallelActivityWorkflow;
use Tests\Fixtures\TestSagaWorkflow;
use Tests\Fixtures\TestUndoActivity;
use Tests\TestCase;
Expand Down Expand Up @@ -48,4 +49,16 @@ public function testFailed(): void
->values()
->toArray());
}

public function testParallelActivityExceptionsTriggersCompensation(): void
{
$workflow = WorkflowStub::make(TestSagaParallelActivityWorkflow::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';
}
}
}
32 changes: 32 additions & 0 deletions tests/Fixtures/TestSagaParallelActivityWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use function Workflow\activity;
use Workflow\ActivityStub;
use Workflow\Workflow;

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

yield ActivityStub::all([
activity(TestSagaActivity::class),
activity(TestSagaActivity::class),
activity(TestSagaActivity::class),
]);

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