Skip to content

Commit 9d7868b

Browse files
timacdonalddriesvintsJubekitaylorotwell
authored
[10.x] Remember the job on the exception (#48830)
* 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]>
1 parent 84a1580 commit 9d7868b

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

src/Illuminate/Queue/MaxAttemptsExceededException.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,23 @@
66

77
class MaxAttemptsExceededException extends RuntimeException
88
{
9-
//
9+
/**
10+
* The job instance.
11+
*
12+
* @var \Illuminate\Contracts\Queue\Job|null
13+
*/
14+
public $job;
15+
16+
/**
17+
* Create a new instance for the job.
18+
*
19+
* @param \Illuminate\Contracts\Queue\Job $job
20+
* @return static
21+
*/
22+
public static function forJob($job)
23+
{
24+
return tap(new static($job->resolveName().' has been attempted too many times.'), function ($e) use ($job) {
25+
$e->job = $job;
26+
});
27+
}
1028
}

src/Illuminate/Queue/TimeoutExceededException.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,16 @@
44

55
class TimeoutExceededException extends MaxAttemptsExceededException
66
{
7-
//
7+
/**
8+
* Create a new instance for the job.
9+
*
10+
* @param \Illuminate\Contracts\Queue\Job $job
11+
* @return static
12+
*/
13+
public static function forJob($job)
14+
{
15+
return tap(new static($job->resolveName().' has timed out.'), function ($e) use ($job) {
16+
$e->job = $job;
17+
});
18+
}
819
}

src/Illuminate/Queue/Worker.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,7 @@ public function kill($status = 0, $options = null)
782782
*/
783783
protected function maxAttemptsExceededException($job)
784784
{
785-
return new MaxAttemptsExceededException(
786-
$job->resolveName().' has been attempted too many times.'
787-
);
785+
return MaxAttemptsExceededException::forJob($job);
788786
}
789787

790788
/**
@@ -795,9 +793,7 @@ protected function maxAttemptsExceededException($job)
795793
*/
796794
protected function timeoutExceededException($job)
797795
{
798-
return new TimeoutExceededException(
799-
$job->resolveName().' has timed out.'
800-
);
796+
return TimeoutExceededException::forJob($job);
801797
}
802798

803799
/**

tests/Queue/QueueExceptionTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Queue;
4+
5+
use Illuminate\Queue\Jobs\RedisJob;
6+
use Illuminate\Queue\Jobs\SyncJob;
7+
use Illuminate\Queue\MaxAttemptsExceededException;
8+
use Illuminate\Queue\TimeoutExceededException;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class QueueExceptionTest extends TestCase
12+
{
13+
public function test_it_can_create_timeout_exception_for_job()
14+
{
15+
$e = TimeoutExceededException::forJob($job = new MyFakeRedisJob());
16+
17+
$this->assertSame('App\\Jobs\\UnderlyingJob has timed out.', $e->getMessage());
18+
$this->assertSame($job, $e->job);
19+
}
20+
21+
public function test_it_can_create_max_attempts_exception_for_job()
22+
{
23+
$e = MaxAttemptsExceededException::forJob($job = new MyFakeRedisJob());
24+
25+
$this->assertSame('App\\Jobs\\UnderlyingJob has been attempted too many times.', $e->getMessage());
26+
$this->assertSame($job, $e->job);
27+
}
28+
}
29+
30+
class MyFakeRedisJob extends RedisJob
31+
{
32+
public function __construct()
33+
{
34+
//
35+
}
36+
37+
public function resolveName()
38+
{
39+
return 'App\\Jobs\\UnderlyingJob';
40+
}
41+
}

0 commit comments

Comments
 (0)