-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Fixes Batch Callbacks not triggering if job timeout while in t…
…ransaction (#48961) * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Update BatchableTransactionTest.php * Apply fixes from StyleCI * wip * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * formatting * Apply fixes from StyleCI * formatting * formatting * Apply fixes from StyleCI * fix test --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
95e924d
commit 20085fe
Showing
6 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
|
||
use Closure; | ||
|
||
/** | ||
* @method void rollBack() | ||
*/ | ||
interface BatchRepository | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
tests/Integration/Database/Queue/BatchableTransactionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\Queue; | ||
|
||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
use Illuminate\Support\Facades\Bus; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Tests\Integration\Database\DatabaseTestCase; | ||
use Orchestra\Testbench\Attributes\WithMigration; | ||
use PHPUnit\Framework\Attributes\RequiresPhpExtension; | ||
use Symfony\Component\Process\Exception\ProcessSignaledException; | ||
use Throwable; | ||
|
||
use function Orchestra\Testbench\remote; | ||
|
||
#[RequiresPhpExtension('pcntl')] | ||
#[WithMigration('laravel', 'queue')] | ||
class BatchableTransactionTest extends DatabaseTestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
protected function defineEnvironment($app) | ||
{ | ||
parent::defineEnvironment($app); | ||
|
||
$config = $app['config']; | ||
|
||
if ($config->get('database.default') === 'testing') { | ||
$this->markTestSkipped('Test does not support using :memory: database connection'); | ||
} | ||
|
||
$config->set(['queue.default' => 'database']); | ||
} | ||
|
||
public function testItCanHandleTimeoutJob() | ||
{ | ||
Bus::batch([new Fixtures\TimeOutJobWithTransaction()]) | ||
->allowFailures() | ||
->dispatch(); | ||
|
||
$this->assertSame(1, DB::table('jobs')->count()); | ||
$this->assertSame(0, DB::table('failed_jobs')->count()); | ||
$this->assertSame(1, DB::table('job_batches')->count()); | ||
|
||
try { | ||
remote('queue:work --stop-when-empty', [ | ||
'DB_CONNECTION' => config('database.default'), | ||
'QUEUE_CONNECTION' => config('queue.default'), | ||
])->run(); | ||
} catch (Throwable $e) { | ||
$this->assertInstanceOf(ProcessSignaledException::class, $e); | ||
$this->assertSame('The process has been signaled with signal "9".', $e->getMessage()); | ||
} | ||
|
||
$this->assertSame(0, DB::table('jobs')->count()); | ||
$this->assertSame(1, DB::table('failed_jobs')->count()); | ||
|
||
$this->assertDatabaseHas('job_batches', [ | ||
'total_jobs' => 1, | ||
'pending_jobs' => 1, | ||
'failed_jobs' => 1, | ||
'failed_job_ids' => json_encode(DB::table('failed_jobs')->pluck('uuid')->all()), | ||
]); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/Integration/Database/Queue/Fixtures/TimeOutJobWithTransaction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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 TimeOutJobWithTransaction implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, Batchable; | ||
|
||
public int $tries = 1; | ||
public int $timeout = 2; | ||
|
||
public function handle(): void | ||
{ | ||
DB::transaction(fn () => sleep(20)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters