簡體   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