Skip to content

Commit

Permalink
[11.x] Fix integrity constraint violation on failed_jobs_uuid_unique (#…
Browse files Browse the repository at this point in the history
…53264)

* [11.x] Fix integrity constraint violation on failed_jobs_uuid_unique (#53230)

* notify observer

* remove old code

* style ci

* add test

* style ci

* skip test on redis/beanstalkd

* Revert "style ci"

This reverts commit ff22f3a.

* Revert "remove old code"

This reverts commit 2086b6c.

* Revert "[11.x] Fix integrity constraint violation on failed_jobs_uuid_unique (#53230)"

This reverts commit 51a0eba

* use static var

* reset static vars

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
bytestream and taylorotwell authored Oct 29, 2024
1 parent 57af5c3 commit a8363e4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Http\Middleware\TrustHosts;
use Illuminate\Http\Middleware\TrustProxies;
use Illuminate\Queue\Console\WorkCommand;
use Illuminate\Queue\Queue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Facade;
Expand Down Expand Up @@ -178,6 +179,7 @@ protected function tearDownTheTestEnvironment(): void
TrustProxies::flushState();
TrustHosts::flushState();
ValidateCsrfToken::flushState();
WorkCommand::flushState();

if ($this->callbackException) {
throw $this->callbackException;
Expand Down
23 changes: 23 additions & 0 deletions src/Illuminate/Queue/Console/WorkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ class WorkCommand extends Command
*/
protected $latestStartedAt;

/**
* Indicates if the worker's event listeners have been registered.
*
* @var bool
*/
private static $hasRegisteredListeners = false;

/**
* Create a new queue work command.
*
Expand Down Expand Up @@ -172,6 +179,10 @@ protected function gatherWorkerOptions()
*/
protected function listenForEvents()
{
if (static::$hasRegisteredListeners) {
return;
}

$this->laravel['events']->listen(JobProcessing::class, function ($event) {
$this->writeOutput($event->job, 'starting');
});
Expand All @@ -189,6 +200,8 @@ protected function listenForEvents()

$this->logFailedJob($event);
});

static::$hasRegisteredListeners = true;
}

/**
Expand Down Expand Up @@ -360,4 +373,14 @@ protected function outputUsingJson()

return $this->option('json');
}

/**
* Reset static variables.
*
* @return void
*/
public static function flushState()
{
static::$hasRegisteredListeners = false;
}
}
37 changes: 37 additions & 0 deletions tests/Integration/Queue/WorkCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Queue\Console\WorkCommand;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Exceptions;
use Illuminate\Support\Facades\Queue;
use Orchestra\Testbench\Attributes\WithMigration;
use RuntimeException;

#[WithMigration]
#[WithMigration('queue')]
Expand All @@ -29,6 +34,13 @@ protected function setUp(): void
$this->markTestSkippedWhenUsingSyncQueueDriver();
}

protected function tearDown(): void
{
WorkCommand::flushState();

parent::tearDown();
}

public function testRunningOneJob()
{
Queue::push(new FirstJob);
Expand Down Expand Up @@ -157,6 +169,21 @@ public function testMaxTimeExceeded()
$this->assertFalse(FirstJob::$ran);
$this->assertFalse(SecondJob::$ran);
}

public function testFailedJobListenerOnlyRunsOnce()
{
$this->markTestSkippedWhenUsingQueueDrivers(['redis', 'beanstalkd']);

Exceptions::fake();

Queue::push(new FirstJob);
$this->withoutMockingConsoleOutput()->artisan('queue:work', ['--once' => true, '--sleep' => 0]);

Queue::push(new JobWillFail);
$this->withoutMockingConsoleOutput()->artisan('queue:work', ['--once' => true]);
Exceptions::assertNotReported(UniqueConstraintViolationException::class);
$this->assertSame(2, substr_count(Artisan::output(), JobWillFail::class));
}
}

class FirstJob implements ShouldQueue
Expand Down Expand Up @@ -196,3 +223,13 @@ public function handle()
static::$ran = true;
}
}

class JobWillFail implements ShouldQueue
{
use Dispatchable, Queueable;

public function handle()
{
throw new RuntimeException;
}
}

0 comments on commit a8363e4

Please sign in to comment.