Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[11.x] Allow SyncQueue to dispatch jobs after a transaction is committed #48860

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
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: 1 addition & 1 deletion 2 src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function () use ($payload, $queue, $delay, $callback, $job) {
*/
protected function shouldDispatchAfterCommit($job)
{
if (is_object($job) && $job instanceof ShouldQueueAfterCommit) {
if ($job instanceof ShouldQueueAfterCommit) {
return true;
}

Expand Down
22 changes: 22 additions & 0 deletions 22 src/Illuminate/Queue/SyncQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ public function size($queue = null)
* @throws \Throwable
*/
public function push($job, $data = '', $queue = null)
{
if ($this->shouldDispatchAfterCommit($job) &&
$this->container->bound('db.transactions')) {
return $this->container->make('db.transactions')->addCallback(
fn () => $this->executeJob($job, $data, $queue)
);
}

return $this->executeJob($job, $data, $queue);
}

/**
* Execute a given job synchronously.
*
* @param string $job
* @param mixed $data
* @param string|null $queue
* @return int
*
* @throws \Throwable
*/
protected function executeJob($job, $data = '', $queue = null)
{
$queueJob = $this->resolveJob($this->createPayload($job, $queue, $data), $queue);

Expand Down
48 changes: 48 additions & 0 deletions 48 tests/Queue/QueueSyncQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldQueueAfterCommit;
use Illuminate\Database\DatabaseTransactionsManager;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Queue\SyncQueue;
Expand Down Expand Up @@ -77,6 +79,32 @@ public function testCreatesPayloadObject()
$this->assertSame('extraValue', $e->getMessage());
}
}

public function testItAddsATransactionCallbackForAfterCommitJobs()
{
$sync = new SyncQueue;
$container = new Container;
$container->bind(\Illuminate\Contracts\Container\Container::class, \Illuminate\Container\Container::class);
$transactionManager = m::mock(DatabaseTransactionsManager::class);
$transactionManager->shouldReceive('addCallback')->once()->andReturn(null);
$container->instance('db.transactions', $transactionManager);

$sync->setContainer($container);
$sync->push(new SyncQueueAfterCommitJob());
}

public function testItAddsATransactionCallbackForInterfaceBasedAfterCommitJobs()
{
$sync = new SyncQueue;
$container = new Container;
$container->bind(\Illuminate\Contracts\Container\Container::class, \Illuminate\Container\Container::class);
$transactionManager = m::mock(DatabaseTransactionsManager::class);
$transactionManager->shouldReceive('addCallback')->once()->andReturn(null);
$container->instance('db.transactions', $transactionManager);

$sync->setContainer($container);
$sync->push(new SyncQueueAfterCommitInterfaceJob());
}
}

class SyncQueueTestEntity implements QueueableEntity
Expand Down Expand Up @@ -134,3 +162,23 @@ public function getValueFromJob($key)
return $payload['data'][$key] ?? null;
}
}

class SyncQueueAfterCommitJob
{
use InteractsWithQueue;

public $afterCommit = true;

public function handle()
{
}
}

class SyncQueueAfterCommitInterfaceJob implements ShouldQueueAfterCommit
{
use InteractsWithQueue;

public function handle()
{
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.