Skip to content

[5.7] Transactional jobs #26515

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

Closed
wants to merge 8 commits into from
Closed
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
7 changes: 7 additions & 0 deletions src/Illuminate/Contracts/Queue/Transactional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Illuminate\Contracts\Queue;

interface Transactional
{
}
19 changes: 18 additions & 1 deletion src/Illuminate/Queue/Jobs/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Queue\Jobs;

use Illuminate\Support\InteractsWithTime;
use Illuminate\Contracts\Queue\Transactional;

abstract class Job
{
Expand Down Expand Up @@ -80,7 +81,23 @@ public function fire()

[$class, $method] = JobName::parse($payload['job']);

($this->instance = $this->resolve($class))->{$method}($this, $payload['data']);
$this->instance = $this->resolve($class);

$handler = function () use ($method, $payload) {
$this->instance->{$method}($this, $payload['data']);
};

if ($this->instance instanceof Transactional) {
$handler = function () use ($handler) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH this style is freaking me out: "use"ing a variable and overwriting it in the same scope so the final call to it is the same? => That's a bit too much magic for my taste

$connection = method_exists($this->instance, 'dbConnection') && \is_callable([$this->instance, 'dbConnection'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we, except with documentation, better indicate to developers that this dbConnection method is possible to use?

The interface doesn't indicate it.

Also I'd like to see the naming a bit improved. E.g. getConnectionForTransaction or something, otherwise it's ambiguous.

? $this->instance->dbConnection()
: null;

$this->container->make('db')->getConnection($connection)->transaction($handler);
};
}

$handler();
}

/**
Expand Down
92 changes: 92 additions & 0 deletions tests/Queue/QueueSyncTransactionalJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Illuminate\Tests\Queue;

use Illuminate\Queue\SyncQueue;
use PHPUnit\Framework\TestCase;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Container\Container;
use Illuminate\Database\Connection;
use Illuminate\Database\Capsule\Manager;
use Illuminate\Contracts\Queue\Transactional;

class QueueTransactionalJobTest extends TestCase
{
/**
* @param $jobClassname
* @param $isTransactional
* @throws \ReflectionException
*
* @dataProvider jobsDataProvider
*/
public function testRunFireInDatabaseTransaction($jobClassname, $isTransactional, $connectionName)
{
$connection = $this->createMock(Connection::class);
$connection->expects($isTransactional ? $this->once() : $this->never())
->method('transaction')
->willReturnCallback(function ($handler) {
$handler();
});

$db = $this->createMock(Manager::class);
$db->expects($isTransactional ? $this->once() : $this->never())
->method('getConnection')
->with($connectionName)
->willReturn($connection);

/** @var Container|\PHPUnit_Framework_MockObject_MockObject $container */
$container = $this->createMock(Container::class);
$container->expects($this->exactly($isTransactional ? 2 : 1))
->method('make')
->willReturnCallback(function ($abstract) use ($db) {
switch ($abstract) {
case 'db':
return $db;
default:
return new $abstract;
}
});

$payloadFactory = new \ReflectionMethod(SyncQueue::class, 'createPayload');
$payloadFactory->setAccessible(true);

$payload = $payloadFactory->invoke(new SyncQueue(), $jobClassname, null, '');

unset($_SERVER['0(*_*)0']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do better then working with a global variable here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, job can use any payload, given from container mock to detect works.
But this is a simple test like Taylor have used here

It also can use real DB connection, real transactions. It's only base ones.

$job = new SyncJob($container, $payload, '', '');
$job->fire();
$this->assertEquals('0(*_*)0', $_SERVER['0(*_*)0']);
}

/**
* @return array
*/
public function jobsDataProvider(): array
{
return [
[TransactionalWithCustomConnection::class, true, 'custom'],
[TransactionalJob::class, true, null],
[SimpleJob::class, false, null],
];
}
}

class SimpleJob
{
public function fire()
{
$_SERVER['0(*_*)0'] = '0(*_*)0';
}
}

class TransactionalJob extends SimpleJob implements Transactional
{
}

class TransactionalWithCustomConnection extends TransactionalJob
{
public function dbConnection(): string
{
return 'custom';
}
}