【问题标题】:Laravel: How to limit retries on queued notificationsLaravel:如何限制排队通知的重试
【发布时间】:2021-06-04 17:03:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: laravel


    【解决方案1】:

    在您的通知文件中添加 Queueable 特征。正是这个特性让您可以更改尝试次数。

    use Illuminate\Bus\Queueable;
    
    class MyNotification extends Notification implements ShouldQueue
    {
        use Queueable;
    
        public $tries = 3;
    

    【讨论】:

    • 嗨@Ole-Haugset,我试过了,但没有效果。有趣的是,jobs 表中的有效负载显示 maxTries 为空。 (我尝试使用 maxTries 而不是尝试,但同样没有效果)。
    • 我使用'使用 Dispatchable、InteractsWithQueue、Queueable、SerializesModels;'和'公共 $tries = 3;'干活
    • $tries 不是InteractsWithQueue 的一部分
    【解决方案2】:

    从 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!');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-11
      • 1970-01-01
      • 2021-07-03
      • 2021-02-21
      • 2017-08-12
      • 1970-01-01
      • 2018-03-25
      • 2021-04-05
      • 1970-01-01
      相关资源
      最近更新 更多