Skip to content

Fix race condition #158

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 3 commits into from
Jan 30, 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
2 changes: 2 additions & 0 deletions src/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function __construct(
if (property_exists($this, 'queue')) {
$this->onQueue($this->queue);
}

$this->afterCommit = true;
}

public function backoff()
Expand Down
2 changes: 2 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function __construct(
if (property_exists($this, 'queue')) {
$this->onQueue($this->queue);
}

$this->afterCommit = true;
}

public function query($method)
Expand Down
51 changes: 51 additions & 0 deletions tests/Feature/DispatchWorkflowInTransactionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Tests\Feature;

use Illuminate\Support\Facades\DB;
use Tests\Fixtures\TestSimpleWorkflow;
use Tests\TestCase;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\WorkflowStub;

final class DispatchWorkflowInTransactionTest extends TestCase
{
public function testRaceCondition(): void
{
$workflow = null;
$start = now();

DB::transaction(static function () use (&$workflow) {
$workflow = WorkflowStub::make(TestSimpleWorkflow::class);

/**
* use case: link the workflow to an entity in the database
*
* EntityWorkflow::create(["entity_id" => 1, "workflow_id" => $workflow->id()])
*/

/**
* dispatch the workflow to the queue
*/
$workflow->start();

/**
* pretend that committing the transaction takes a while
*/
sleep(3);
});

/**
* the workflow stays in the pending state as the transaction was not committed
* when the worker was told to process the workflow
*
* the exception is silently caught in src/Workflow.php:115
*/
while ($workflow->running() && $workflow->exceptions()->isEmpty() && now()->diffInSeconds($start) < 15);

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

declare(strict_types=1);

namespace Tests\Fixtures;

use Workflow\ActivityStub;
use Workflow\Workflow;

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

public $queue = 'default';

public function execute()
{
$result = yield ActivityStub::make(TestActivity::class);

return 'workflow_' . $result;
}
}
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