Skip to content
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 src/Illuminate/Bus/DatabaseBatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function transaction(Closure $callback)
*/
public function rollBack()
{
$this->connection->rollBack();
$this->connection->rollBack(toLevel: 0);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Queue/Jobs/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ public function fail($e = null)
}
}

if ($this->shouldRollBackDatabaseTransaction($e)) {
$this->container->make('db')
->connection($this->container['config']['queue.failed.database'])
->rollBack(toLevel: 0);
}

try {
// If the job has failed, we will delete it, call the "failed" method and then call
// an event indicating the job has failed so it can be logged if needed. This is
Expand All @@ -218,6 +224,20 @@ public function fail($e = null)
}
}

/**
* Determine if the current database transaction should be rolled back to level zero.
*
* @param \Throwable $e
* @return bool
*/
protected function shouldRollBackDatabaseTransaction($e)
{
return ($e instanceof TimeoutExceededException &&
$this->container['config']['queue.failed.database'] &&
in_array($this->container['config']['queue.failed.driver'], ['database', 'database-uuids']) &&
$this->container->bound('db'));
}

/**
* Process an exception that caused the job to fail.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Tests\Integration\Database\Queue\Fixtures;

use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

class TimeOutJobWithNestedTransactions implements ShouldQueue
{
use InteractsWithQueue, Queueable, Batchable;

public int $tries = 1;
public int $timeout = 2;

public function handle(): void
{
DB::transaction(function () {
DB::transaction(fn () => sleep(20));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Tests\Integration\Database\Queue\Fixtures;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

class TimeOutNonBatchableJobWithNestedTransactions implements ShouldQueue
{
use InteractsWithQueue, Queueable;

public int $tries = 1;
public int $timeout = 2;

public function handle(): void
{
DB::transaction(function () {
DB::transaction(fn () => sleep(20));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Illuminate\Tests\Integration\Database\Queue\Fixtures;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

class TimeOutNonBatchableJobWithTransaction implements ShouldQueue
{
use InteractsWithQueue, Queueable;

public int $tries = 1;
public int $timeout = 2;

public function handle(): void
{
DB::transaction(fn () => sleep(20));
}
}
16 changes: 14 additions & 2 deletions tests/Integration/Database/Queue/QueueTransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\Attributes\WithMigration;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Symfony\Component\Process\Exception\ProcessSignaledException;
use Throwable;
Expand All @@ -29,9 +30,10 @@ protected function setUp(): void
}
}

public function testItCanHandleTimeoutJob()
#[DataProvider('timeoutJobs')]
public function testItCanHandleTimeoutJob($job)
{
dispatch(new Fixtures\TimeOutJobWithTransaction);
dispatch($job);

$this->assertSame(1, DB::table('jobs')->count());
$this->assertSame(0, DB::table('failed_jobs')->count());
Expand All @@ -49,4 +51,14 @@ public function testItCanHandleTimeoutJob()
$this->assertSame(0, DB::table('jobs')->count());
$this->assertSame(1, DB::table('failed_jobs')->count());
}

public static function timeoutJobs(): array
{
return [
[new Fixtures\TimeOutJobWithTransaction()],
[new Fixtures\TimeOutJobWithNestedTransactions()],
[new Fixtures\TimeOutNonBatchableJobWithTransaction()],
[new Fixtures\TimeOutNonBatchableJobWithNestedTransactions()],
];
}
}