Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Remember the job on the exception #48830

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 17 additions & 1 deletion src/Illuminate/Queue/MaxAttemptsExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,21 @@

class MaxAttemptsExceededException extends RuntimeException
{
//
/**
* @var \Illuminate\Contracts\Queue\Job
*/
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';
}
}