我有两个环境,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));
非常感谢您的帮助。