简体   繁体   English

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

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

From the Laravel manual, I understand that I can limit the number of times a queued job is retried using either the command line (when starting the queue), or by setting the $tries property on the job class itself.从 Laravel 手册中,我了解到我可以使用命令行(启动队列时)或通过在作业类本身上设置$tries属性来限制重试排队作业的次数。 https://laravel.com/docs/5.6/queues#max-job-attempts-and-timeout https://laravel.com/docs/5.6/queues#max-job-attempts-and-timeout

I want to set the maximum number of retries within the job itself, not using the command line, however the job is actually a custom class that inherits from Illuminate\\Notifications\\Notification , not an App\\Job .我想在作业本身中设置最大重试次数,而不是使用命令行,但是作业实际上是一个自定义类,它继承自Illuminate\\Notifications\\Notification ,而不是App\\Job In this case, is it possible to limit the number of tries?在这种情况下,是否可以限制尝试次数?

I tried setting the $tries property in my customer notification, but it had no effect.我尝试在客户通知中设置$tries属性,但没有效果。 I am using a custom channel as well, but setting the $tries there had no effect either.我也在使用自定义频道,但在那里设置$tries也没有效果。

In your notification file add the Queueable trait.在您的通知文件中添加Queueable特性。 It's this trait that gives you the possibility to alter the number of tries.正是这种特性使您可以改变尝试次数。

use Illuminate\Bus\Queueable;

class MyNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $tries = 3;

As of Laravel 5.7+, you can easily limit maximum tries by adding $tries property to the Queueable Notification.从 Laravel 5.7+ 开始,您可以通过将$tries属性添加到 Queueable Notification 来轻松限制最大尝试次数。

Usage example from the PR author ( laravel/framework GitHub PR 26493# ): 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