Skip to content

Commit

Permalink
[10.x] Remember the job on the exception (#48830)
Browse files Browse the repository at this point in the history
* Remember the job on the exception

* Update tests/Queue/QueueExceptionTest.php

Co-authored-by: Julius Kiekbusch <[email protected]>

* Update tests/Queue/QueueExceptionTest.php

Co-authored-by: Julius Kiekbusch <[email protected]>

* Update src/Illuminate/Queue/MaxAttemptsExceededException.php

Co-authored-by: Dries Vints <[email protected]>

* Update MaxAttemptsExceededException.php

---------

Co-authored-by: Dries Vints <[email protected]>
Co-authored-by: Julius Kiekbusch <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
4 people authored Oct 30, 2023
1 parent 84a1580 commit 9d7868b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
20 changes: 19 additions & 1 deletion src/Illuminate/Queue/MaxAttemptsExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,23 @@

class MaxAttemptsExceededException extends RuntimeException
{
//
/**
* The job instance.
*
* @var \Illuminate\Contracts\Queue\Job|null
*/
public $job;

/**
* Create a new instance for the job.
*
* @param \Illuminate\Contracts\Queue\Job $job
* @return static
*/
public static function forJob($job)
{
return tap(new static($job->resolveName().' has been attempted too many times.'), function ($e) use ($job) {
$e->job = $job;
});
}
}
13 changes: 12 additions & 1 deletion src/Illuminate/Queue/TimeoutExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,16 @@

class TimeoutExceededException extends MaxAttemptsExceededException
{
//
/**
* Create a new instance for the job.
*
* @param \Illuminate\Contracts\Queue\Job $job
* @return static
*/
public static function forJob($job)
{
return tap(new static($job->resolveName().' has timed out.'), function ($e) use ($job) {
$e->job = $job;
});
}
}
8 changes: 2 additions & 6 deletions src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,7 @@ public function kill($status = 0, $options = null)
*/
protected function maxAttemptsExceededException($job)
{
return new MaxAttemptsExceededException(
$job->resolveName().' has been attempted too many times.'
);
return MaxAttemptsExceededException::forJob($job);
}

/**
Expand All @@ -795,9 +793,7 @@ protected function maxAttemptsExceededException($job)
*/
protected function timeoutExceededException($job)
{
return new TimeoutExceededException(
$job->resolveName().' has timed out.'
);
return TimeoutExceededException::forJob($job);
}

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

namespace Illuminate\Tests\Queue;

use Illuminate\Queue\Jobs\RedisJob;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Queue\MaxAttemptsExceededException;
use Illuminate\Queue\TimeoutExceededException;
use PHPUnit\Framework\TestCase;

class QueueExceptionTest extends TestCase
{
public function test_it_can_create_timeout_exception_for_job()
{
$e = TimeoutExceededException::forJob($job = new MyFakeRedisJob());

$this->assertSame('App\\Jobs\\UnderlyingJob has timed out.', $e->getMessage());
$this->assertSame($job, $e->job);
}

public function test_it_can_create_max_attempts_exception_for_job()
{
$e = MaxAttemptsExceededException::forJob($job = new MyFakeRedisJob());

$this->assertSame('App\\Jobs\\UnderlyingJob has been attempted too many times.', $e->getMessage());
$this->assertSame($job, $e->job);
}
}

class MyFakeRedisJob extends RedisJob
{
public function __construct()
{
//
}

public function resolveName()
{
return 'App\\Jobs\\UnderlyingJob';
}
}

0 comments on commit 9d7868b

Please sign in to comment.