InteractsWithQueue 中 重试次数用尽后调用的是fail还是failed?
laravel文档中写的是failed
事件系统《Laravel 10 中文文档》
<?php
namespace App\Listeners;
use App\Events\OrderShipped;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Throwable;
class SendShipmentNotification implements ShouldQueue
{
use InteractsWithQueue;
/**
* 事件处理。
*/
public function handle(OrderShipped $event): void
{
// ...
}
/**
* 处理失败任务。
*/
public function failed(OrderShipped $event, Throwable $exception): void
{
// ...
}
}
我在 InteractsWithQueue 代码中 发现只提供了 fail 方法.
<?php
namespace Illuminate\Queue;
use DateTimeInterface;
use Illuminate\Contracts\Queue\Job as JobContract;
use Illuminate\Support\InteractsWithTime;
use InvalidArgumentException;
use Throwable;
trait InteractsWithQueue
{
use InteractsWithTime;
/**
* The underlying queue job instance.
*
* @var \Illuminate\Contracts\Queue\Job|null
*/
public $job;
/**
* Get the number of times the job has been attempted.
*
* @return int
*/
public function attempts()
{
return $this->job ? $this->job->attempts() : 1;
}
/**
* Delete the job from the queue.
*
* @return void
*/
public function delete()
{
if ($this->job) {
return $this->job->delete();
}
}
/**
* Fail the job from the queue.
*
* @param \Throwable|string|null $exception
* @return void
*/
public function fail($exception = null)
{
if (is_string($exception)) {
$exception = new ManuallyFailedException($exception);
}
if ($exception instanceof Throwable || is_null($exception)) {
if ($this->job) {
return $this->job->fail($exception);
}
} else {
throw new InvalidArgumentException('The fail method requires a string or an instance of Throwable.');
}
}
/**
* Release the job back into the queue after (n) seconds.
*
* @param \DateTimeInterface|\DateInterval|int $delay
* @return void
*/
public function release($delay = 0)
{
$delay = $delay instanceof DateTimeInterface
? $this->secondsUntil($delay)
: $delay;
if ($this->job) {
return $this->job->release($delay);
}
}
/**
* Set the base queue job instance.
*
* @param \Illuminate\Contracts\Queue\Job $job
* @return $this
*/
public function setJob(JobContract $job)
{
$this->job = $job;
return $this;
}
}
原来我以为fail方法是每次失败都会调用,failed 是 重试次数用完才会调用,但我没看到failed方法。
希望有大佬能问我解惑。十分感谢。
fail
是手动让任务失败,failed
是任务失败后才执行的