Skip to content

Fix already pending parent workflow #179

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 9 commits into from
May 19, 2024
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
4 changes: 2 additions & 2 deletions src/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use LimitIterator;
use SplFileObject;
use Throwable;
use Workflow\Middleware\ActivityMiddleware;
use Workflow\Middleware\WithoutOverlappingMiddleware;
use Workflow\Middleware\WorkflowMiddleware;
use Workflow\Models\StoredWorkflow;
use Workflow\Serializers\Y;

Expand Down Expand Up @@ -106,7 +106,7 @@ public function middleware()
0,
$this->timeout
),
new WorkflowMiddleware(),
new ActivityMiddleware(),
];
}

Expand Down
67 changes: 67 additions & 0 deletions src/ChildWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Workflow;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Workflow\Middleware\WithoutOverlappingMiddleware;
use Workflow\Models\StoredWorkflow;

final class ChildWorkflow implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public $tries = PHP_INT_MAX;

public $maxExceptions = PHP_INT_MAX;

public $timeout = 0;

public function __construct(
public int $index,
public string $now,
public StoredWorkflow $storedWorkflow,
public $return,
public StoredWorkflow $parentWorkflow,
$connection = null,
$queue = null
) {
$connection = $connection ?? config('queue.default');
$queue = $queue ?? config('queue.connections.' . $connection . '.queue', 'default');
$this->onConnection($connection);
$this->onQueue($queue);
}

public function handle()
{
$workflow = $this->parentWorkflow->toWorkflow();

try {
if ($this->parentWorkflow->logs()->whereIndex($this->index)->exists()) {
$workflow->resume();
} else {
$workflow->next($this->index, $this->now, $this->storedWorkflow->class, $this->return);
}
} catch (\Spatie\ModelStates\Exceptions\TransitionNotFound) {
if ($workflow->running()) {
$this->release();
}
}
}

public function middleware()
{
return [
new WithoutOverlappingMiddleware($this->parentWorkflow->id, WithoutOverlappingMiddleware::ACTIVITY),
];
}
}
41 changes: 37 additions & 4 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@

namespace Workflow;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Workflow\Middleware\WithoutOverlappingMiddleware;
use Workflow\Models\StoredWorkflow;

class Exception extends Activity
final class Exception implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public $tries = PHP_INT_MAX;

public $maxExceptions = PHP_INT_MAX;

public $timeout = 0;

public function __construct(
public int $index,
public string $now,
Expand All @@ -24,10 +42,25 @@ public function __construct(

public function handle()
{
if ($this->storedWorkflow->logs()->whereIndex($this->index)->exists()) {
return;
$workflow = $this->storedWorkflow->toWorkflow();

try {
if ($this->storedWorkflow->logs()->whereIndex($this->index)->exists()) {
$workflow->resume();
} else {
$workflow->next($this->index, $this->now, self::class, $this->exception);
}
} catch (\Spatie\ModelStates\Exceptions\TransitionNotFound) {
if ($workflow->running()) {
$this->release();
}
}
}

return $this->exception;
public function middleware()
{
return [
new WithoutOverlappingMiddleware($this->storedWorkflow->id, WithoutOverlappingMiddleware::ACTIVITY),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Workflow\Events\ActivityFailed;
use Workflow\Events\ActivityStarted;

final class WorkflowMiddleware
final class ActivityMiddleware
{
public function handle($job, $next): void
{
Expand All @@ -33,6 +33,7 @@ public function handle($job, $next): void
try {
$job->storedWorkflow->toWorkflow()
->next($job->index, $job->now, $job::class, $result);

ActivityCompleted::dispatch(
$job->storedWorkflow->id,
$uuid,
Expand All @@ -59,6 +60,7 @@ public function handle($job, $next): void
'snippet' => array_slice(iterator_to_array($iterator), 0, 7),
]), now()
->format('Y-m-d\TH:i:s.u\Z'));

throw $throwable;
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,13 @@ public function handle(): void
);

if ($parentWorkflow) {
$parentWorkflow->toWorkflow()
->next($parentWorkflow->pivot->parent_index, $this->now, $this->storedWorkflow->class, $return);
ChildWorkflow::dispatch(
$parentWorkflow->pivot->parent_index,
$this->now,
$this->storedWorkflow,
$return,
$parentWorkflow
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
use Workflow\Events\ActivityCompleted;
use Workflow\Events\ActivityFailed;
use Workflow\Events\ActivityStarted;
use Workflow\Middleware\WorkflowMiddleware;
use Workflow\Middleware\ActivityMiddleware;
use Workflow\Models\StoredWorkflow;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\States\WorkflowRunningStatus;
use Workflow\States\WorkflowWaitingStatus;
use Workflow\WorkflowStub;

final class WorkflowMiddlewareTest extends TestCase
final class ActivityMiddlewareTest extends TestCase
{
public function testMiddleware(): void
{
Expand All @@ -42,7 +42,7 @@ public function testMiddleware(): void
->toDateTimeString();
$activity->storedWorkflow = $storedWorkflow;

$middleware = new WorkflowMiddleware();
$middleware = new ActivityMiddleware();

$middleware->handle($activity, static function ($job) {
return true;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function testAlreadyCompleted(): void
->toDateTimeString();
$activity->storedWorkflow = $storedWorkflow;

$middleware = new WorkflowMiddleware();
$middleware = new ActivityMiddleware();

$middleware->handle($activity, static function ($job) {
return true;
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testAlreadyRunning(): void
->toDateTimeString();
$activity->storedWorkflow = $storedWorkflow;

$middleware = new WorkflowMiddleware();
$middleware = new ActivityMiddleware();

$middleware->handle($activity, static function ($job) {
return true;
Expand Down Expand Up @@ -137,7 +137,7 @@ public function testException(): void
->toDateTimeString();
$activity->storedWorkflow = $storedWorkflow;

$middleware = new WorkflowMiddleware();
$middleware = new ActivityMiddleware();

try {
$middleware->handle($activity, static function ($job) {
Expand Down
68 changes: 64 additions & 4 deletions tests/Unit/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,25 @@ public function testException(): void
{
$exception = new \Exception('test');
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
$storedWorkflow->arguments = Y::serialize([]);
$storedWorkflow->save();
$activity = new Exception(0, now()->toDateTimeString(), StoredWorkflow::findOrFail(
$workflow->id()
), $exception);

$result = $activity->handle();
$activity->handle();

$this->assertSame($exception, $result);
$this->assertSame(Exception::class, $storedWorkflow->logs()->first()->class);
}

public function testExceptionAlreadyLogged(): void
{
$exception = new \Exception('test');
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
$storedWorkflow->arguments = Y::serialize([]);
$storedWorkflow->save();
$activity = new Exception(0, now()->toDateTimeString(), StoredWorkflow::findOrFail(
$workflow->id()
), $exception);
Expand All @@ -50,9 +55,9 @@ public function testExceptionAlreadyLogged(): void
'result' => Y::serialize($exception),
]);

$result = $activity->handle();
$activity->handle();

$this->assertNull($result);
$this->assertSame(1, $storedWorkflow->logs()->count());
}

public function testParent(): void
Expand Down Expand Up @@ -108,4 +113,59 @@ public function testParent(): void
$this->assertSame(WorkflowCompletedStatus::class, $childWorkflow->status());
$this->assertSame('other', $childWorkflow->output());
}

public function testParentPending(): void
{
Carbon::setTestNow('2022-01-01');

$parentWorkflow = WorkflowStub::load(WorkflowStub::make(TestParentWorkflow::class)->id());

$storedParentWorkflow = StoredWorkflow::findOrFail($parentWorkflow->id());
$storedParentWorkflow->arguments = Y::serialize([]);
$storedParentWorkflow->status = WorkflowPendingStatus::class;
$storedParentWorkflow->save();

$storedParentWorkflow->logs()
->create([
'index' => 0,
'now' => now(),
'class' => TestChildWorkflow::class,
'result' => Y::serialize('child_workflow'),
]);

$storedParentWorkflow->logs()
->create([
'index' => 1,
'now' => now(),
'class' => TestActivity::class,
'result' => Y::serialize('activity'),
]);

$childWorkflow = WorkflowStub::load(WorkflowStub::make(TestChildWorkflow::class)->id());

$storedChildWorkflow = StoredWorkflow::findOrFail($childWorkflow->id());
$storedChildWorkflow->arguments = Y::serialize([]);
$storedChildWorkflow->status = WorkflowPendingStatus::class;
$storedChildWorkflow->save();
$storedChildWorkflow->parents()
->attach($storedParentWorkflow, [
'parent_index' => 0,
'parent_now' => now(),
]);

$storedChildWorkflow->logs()
->create([
'index' => 0,
'now' => now(),
'class' => TestOtherActivity::class,
'result' => Y::serialize('other'),
]);

(new (TestChildWorkflow::class)($storedChildWorkflow))->handle();
(new (TestParentWorkflow::class)($storedParentWorkflow))->handle();

$this->assertSame('2022-01-01 00:00:00', WorkflowStub::now()->toDateTimeString());
$this->assertSame(WorkflowCompletedStatus::class, $childWorkflow->status());
$this->assertSame('other', $childWorkflow->output());
}
}
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