Laravel队列通知

3

我有两个环境,QA(1个Web服务器)和Prod(2个Web服务器在负载均衡器后面),它们使用数据库队列以相同的方式设置。

在QA上一切正常。

在生产环境中,我遇到了一些奇怪的行为...使用队列时可发送Mailable电子邮件,但使用队列时无法发送电子邮件通知。

如果从通知中删除队列,则可以发送电子邮件。

在QA上,我可以看到jobs表中创建了两个作业。在Prod上,只有Mailable被创建在jobs表中。

以激活电子邮件为例:

<?php

namespace App\Notifications;

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

class SendActivationEmail extends Notification implements ShouldQueue
{
    use Queueable;

    protected $token;

    /**
     * Create a new notification instance.
     *
     * SendActivationEmail constructor.
     * @param $token
     */
    public function __construct($token)
    {
        $this->token = $token;
        $this->onQueue('social');
    }

    /**
     * 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)
            ->subject('Activation email')
            ->greeting('xxxxxx - Hello!')
            ->line('You need to activate your email before you can start using all of our services.')
            ->action('Activate Email', route('authenticated.activate', ['token' => $this->token]))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

我的 .env 文件:

QUEUE_DRIVER=database

文件 config/queue.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Queue Driver
    |--------------------------------------------------------------------------
    |
    | The Laravel queue API supports a variety of back-ends via an unified
    | API, giving you convenient access to each back-end using the same
    | syntax for each one. Here you may set the default queue driver.
    |
    | Supported: "null", "sync", "database", "beanstalkd",
    |            "sqs", "iron", "redis"
    |
    */

    'default' => env('QUEUE_DRIVER', 'database'),

    /*
    |--------------------------------------------------------------------------
    | Queue Connections
    |--------------------------------------------------------------------------
    |
    | Here you may configure the connection information for each server that
    | is used by your application. A default configuration has been added
    | for each back-end shipped with Laravel. You are free to add more.
    |
    */

    'connections' => [

        'database' => [
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'default',
            'expire' => 60,
        ],

我的可与队列一起工作的可邮寄类:

Mail::to($user->email)
                        ->queue(new Welcome($user));

非常感谢您的帮助。

你使用的 Laravel 版本是什么? - aceraven777
我正在使用 Laravel 5.3。 - Andrew
2个回答

0

我猜这与 .env 文件有关

确保两个 env 文件都使用正确的队列驱动程序。

并且您的队列工作程序已启用(Cron、supervisorcrl 等)


0
You just need to change this:

public function __construct($token)
    {
        $this->token = $token;
        $this->onQueue('social');
    }
    

to this:

public function __construct($token)
    {
        $this->token = $token;
        $this->connection = 'social';
    }

你能描述一下这段代码片段在做什么吗? - KargWare
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接