Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/Illuminate/Queue/DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Queue\ClearableQueue;
use Illuminate\Contracts\Queue\Queue as QueueContract;
use Illuminate\Database\Connection;
use Illuminate\Database\DetectsConcurrencyErrors;
use Illuminate\Queue\Jobs\DatabaseJob;
use Illuminate\Queue\Jobs\DatabaseJobRecord;
use Illuminate\Support\Carbon;
Expand All @@ -16,6 +17,7 @@

class DatabaseQueue extends Queue implements QueueContract, ClearableQueue
{
use DetectsConcurrencyErrors;
/**
* The database connection instance.
*
Expand Down Expand Up @@ -295,6 +297,10 @@ public function pop($queue = null)
}
});
} catch (Throwable $e) {
if ($this->causedByConcurrencyError($e)) {
return null;
}

// Potentially invalid job that we need to fail (#58978)...
if ($jobRecord) {
try {
Expand Down
24 changes: 24 additions & 0 deletions tests/Queue/QueueDatabaseQueueUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use Illuminate\Bus\Batchable;
use Illuminate\Container\Container;
use Illuminate\Database\Connection;
use Illuminate\Database\QueryException;
use Illuminate\Queue\DatabaseQueue;
use Illuminate\Queue\Jobs\DatabaseJobRecord;
use Illuminate\Queue\Queue;
use Illuminate\Support\Str;
use Mockery as m;
use PDOException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
Expand Down Expand Up @@ -196,6 +199,27 @@ public function testBuildDatabaseRecordWithPayloadAtTheEnd()
$this->assertArrayHasKey('payload', $record);
$this->assertArrayHasKey('payload', array_slice($record, -1, 1, true));
}

public function testPopReturnsNullOnDeadlock()
{
$queue = $this->getMockBuilder(DatabaseQueue::class)
->onlyMethods(['getNextAvailableJob', 'marshalJob', 'deleteReserved'])
->setConstructorArgs([$database = m::mock(Connection::class), 'table', 'default'])
->getMock();

$queue->setContainer(m::mock(Container::class));
$queue->method('getNextAvailableJob')->willReturn(m::mock(DatabaseJobRecord::class));
$queue->method('marshalJob')->willThrowException(
new QueryException('mysql', '', [], new PDOException('SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction'))
);
$queue->expects($this->never())->method('deleteReserved');

$database->shouldReceive('transaction')->andReturnUsing(function ($callback) {
return $callback();
});

$this->assertNull($queue->pop('default'));
}
}

class MyTestJob
Expand Down
Loading