Skip to content

Restart Child Workflows #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 23, 2023
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
17 changes: 15 additions & 2 deletions src/ChildWorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ public static function make($workflow, ...$arguments): PromiseInterface
return resolve(Y::unserialize($log->result));
}

WorkflowStub::make($workflow)
->startAsChild($context->storedWorkflow, $context->index, $context->now, ...$arguments);
$storedChildWorkflow = $context->storedWorkflow->children()
->wherePivot('parent_index', $context->index)
->first();

$childWorkflow = $storedChildWorkflow ? $storedChildWorkflow->toWorkflow() : WorkflowStub::make($workflow);

if ($childWorkflow->running() && ! $childWorkflow->created()) {
try {
$childWorkflow->resume();
} catch (\Spatie\ModelStates\Exceptions\TransitionNotFound) {
// already running
}
} elseif (! $childWorkflow->completed()) {
$childWorkflow->startAsChild($context->storedWorkflow, $context->index, $context->now, ...$arguments);
}

++$context->index;
WorkflowStub::setContext($context);
Expand Down
10 changes: 10 additions & 0 deletions src/Models/StoredWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ public function parents(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
'parent_workflow_id'
)->withPivot(['parent_index', 'parent_now']);
}

public function children(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(
config('workflows.stored_workflow_model', self::class),
'workflow_relationships',
'parent_workflow_id',
'child_workflow_id'
)->withPivot(['parent_index', 'parent_now']);
}
}
47 changes: 29 additions & 18 deletions src/WorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Workflow\Models\StoredWorkflow;
use Workflow\Serializers\Y;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\States\WorkflowCreatedStatus;
use Workflow\States\WorkflowFailedStatus;
use Workflow\States\WorkflowPendingStatus;
use Workflow\Traits\Awaits;
Expand Down Expand Up @@ -128,6 +129,21 @@ public function output()
return Y::unserialize($this->storedWorkflow->fresh()->output);
}

public function completed(): bool
{
return $this->status() === WorkflowCompletedStatus::class;
}

public function created(): bool
{
return $this->status() === WorkflowCreatedStatus::class;
}

public function failed(): bool
{
return $this->status() === WorkflowFailedStatus::class;
}

public function running(): bool
{
return ! in_array($this->status(), [WorkflowCompletedStatus::class, WorkflowFailedStatus::class], true);
Expand All @@ -146,22 +162,6 @@ public function fresh(): static
return $this;
}

public function restart(...$arguments): void
{
$this->storedWorkflow->arguments = Y::serialize($arguments);
$this->storedWorkflow->output = null;
$this->storedWorkflow->exceptions()
->delete();
$this->storedWorkflow->logs()
->delete();
$this->storedWorkflow->signals()
->delete();
$this->storedWorkflow->timers()
->delete();

$this->dispatch();
}

public function resume(): void
{
$this->dispatch();
Expand All @@ -176,15 +176,16 @@ public function start(...$arguments): void

public function startAsChild(StoredWorkflow $parentWorkflow, int $index, $now, ...$arguments): void
{
$this->storedWorkflow->arguments = Y::serialize($arguments);
$this->storedWorkflow->parents()
->detach();

$this->storedWorkflow->parents()
->attach($parentWorkflow, [
'parent_index' => $index,
'parent_now' => $now,
]);

$this->dispatch();
$this->start(...$arguments);
}

public function fail($exception): void
Expand All @@ -200,6 +201,16 @@ public function fail($exception): void
}

$this->storedWorkflow->status->transitionTo(WorkflowFailedStatus::class);

$this->storedWorkflow->parents()
->each(static function ($parentWorkflow) use ($exception) {
try {
$parentWorkflow->toWorkflow()
->fail($exception);
} catch (\Spatie\ModelStates\Exceptions\TransitionNotFound) {
return;
}
});
}

public function next($index, $now, $class, $result): void
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/ParentWorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace Tests\Feature;

use Tests\Fixtures\TestActivity;
use Tests\Fixtures\TestChildExceptionWorkflow;
use Tests\Fixtures\TestChildWorkflow;
use Tests\Fixtures\TestParentExceptionWorkflow;
use Tests\Fixtures\TestParentWorkflow;
use Tests\TestCase;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\States\WorkflowFailedStatus;
use Workflow\WorkflowStub;

final class ParentWorkflowTest extends TestCase
Expand All @@ -29,4 +32,29 @@ public function testCompleted(): void
->values()
->toArray());
}

public function testRetry(): void
{
$workflow = WorkflowStub::make(TestParentExceptionWorkflow::class);

$workflow->start(shouldThrow: true);

while ($workflow->running());

$this->assertSame(WorkflowFailedStatus::class, $workflow->status());
$this->assertNull($workflow->output());

$workflow->fresh()
->start(shouldThrow: false);

while ($workflow->running());

$this->assertSame(WorkflowCompletedStatus::class, $workflow->status());
$this->assertSame('workflow_activity_other', $workflow->output());
$this->assertSame([TestActivity::class, TestChildExceptionWorkflow::class], $workflow->logs()
->pluck('class')
->sort()
->values()
->toArray());
}
}
27 changes: 27 additions & 0 deletions tests/Fixtures/TestChildExceptionWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Exception;
use Workflow\ActivityStub;
use Workflow\Workflow;

class TestChildExceptionWorkflow extends Workflow
{
public $connection = 'redis';

public $queue = 'default';

public function execute($shouldThrow = false)
{
if ($shouldThrow) {
throw new Exception('failed');
}

$otherResult = yield ActivityStub::make(TestOtherActivity::class, 'other');

return $otherResult;
}
}
25 changes: 25 additions & 0 deletions tests/Fixtures/TestParentExceptionWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Workflow\ActivityStub;
use Workflow\ChildWorkflowStub;
use Workflow\Workflow;

class TestParentExceptionWorkflow extends Workflow
{
public $connection = 'redis';

public $queue = 'default';

public function execute($shouldThrow = false)
{
$otherResult = yield ChildWorkflowStub::make(TestChildExceptionWorkflow::class, $shouldThrow);

$result = yield ActivityStub::make(TestActivity::class);

return 'workflow_' . $result . '_' . $otherResult;
}
}
31 changes: 31 additions & 0 deletions tests/Unit/ChildWorkflowStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,37 @@ public function testLoadsStoredResult(): void
$this->assertSame('test', $result);
}

public function testLoadsChildWorkflow(): void
{
$workflow = WorkflowStub::load(WorkflowStub::make(TestParentWorkflow::class)->id());
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
$storedWorkflow->update([
'arguments' => Y::serialize([]),
'status' => WorkflowPendingStatus::$name,
]);

$childWorkflow = WorkflowStub::load(WorkflowStub::make(TestChildWorkflow::class)->id());
$storedChildWorkflow = StoredWorkflow::findOrFail($childWorkflow->id());
$storedChildWorkflow->update([
'arguments' => Y::serialize([]),
'status' => WorkflowPendingStatus::$name,
]);
$storedChildWorkflow->parents()
->attach($storedWorkflow, [
'parent_index' => 0,
'parent_now' => now(),
]);

$workflow = $storedWorkflow->toWorkflow();

$existingChildWorkflow = ChildWorkflowStub::make(TestChildWorkflow::class)
->then(static function ($value) use (&$result) {
$result = $value;
});

$this->assertNull($result);
}

public function testAll(): void
{
$workflow = WorkflowStub::load(WorkflowStub::make(TestParentWorkflow::class)->id());
Expand Down
25 changes: 17 additions & 8 deletions tests/Unit/WorkflowStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Tests\Fixtures\TestAwaitWorkflow;
use Tests\Fixtures\TestWorkflow;
use Tests\TestCase;
use Workflow\Models\StoredWorkflow;
use Workflow\Serializers\Y;
use Workflow\Signal;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\States\WorkflowFailedStatus;
use Workflow\States\WorkflowPendingStatus;
use Workflow\WorkflowStub;

Expand All @@ -22,7 +22,15 @@ public function testMake(): void
{
Carbon::setTestNow('2022-01-01');

$parentWorkflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
$storedParentWorkflow = StoredWorkflow::findOrFail($parentWorkflow->id());
$storedParentWorkflow->update([
'arguments' => Y::serialize([]),
'status' => WorkflowPendingStatus::$name,
]);

$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
$workflow->start();
$workflow->cancel();
while (! $workflow->isCanceled());
Expand All @@ -33,21 +41,22 @@ public function testMake(): void
$this->assertNull($workflow->output());
$this->assertSame(1, $workflow->logs()->count());

$storedWorkflow->parents()
->attach($storedParentWorkflow, [
'parent_index' => 0,
'parent_now' => now(),
]);
$workflow->fail(new Exception('test'));
$this->assertSame(WorkflowFailedStatus::class, $workflow->status());

$workflow->restart();
$workflow->fresh();
$this->assertSame(WorkflowPendingStatus::class, $workflow->status());
$this->assertSame(0, $workflow->logs()->count());
$this->assertTrue($workflow->failed());
$this->assertTrue($parentWorkflow->failed());

$workflow->cancel();
while (! $workflow->isCanceled());

$workflow->fresh();
$this->assertSame(WorkflowPendingStatus::class, $workflow->status());
$this->assertNull($workflow->output());
$this->assertSame(1, $workflow->logs()->count());
$this->assertSame(2, $workflow->logs()->count());
}

public function testComplete(): void
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy