繁体   English   中英

Laravel:如何限制对排队通知的重试

[英]Laravel: How to limit retries on queued notifications

从 Laravel 手册中,我了解到我可以使用命令行(启动队列时)或通过在作业类本身上设置$tries属性来限制重试排队作业的次数。 https://laravel.com/docs/5.6/queues#max-job-attempts-and-timeout

我想在作业本身中设置最大重试次数,而不是使用命令行,但是作业实际上是一个自定义类,它继承自Illuminate\\Notifications\\Notification ,而不是App\\Job 在这种情况下,是否可以限制尝试次数?

我尝试在客户通知中设置$tries属性,但没有效果。 我也在使用自定义频道,但在那里设置$tries也没有效果。

在您的通知文件中添加Queueable特性。 正是这种特性使您可以改变尝试次数。

use Illuminate\Bus\Queueable;

class MyNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $tries = 3;

从 Laravel 5.7+ 开始,您可以通过将$tries属性添加到 Queueable Notification 来轻松限制最大尝试次数。

PR作者的使用示例( laravel/framework GitHub PR 26493# ):

<?php

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class TestNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $tries = 3; // Max tries

    public $timeout = 15; // Timeout seconds

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM