【发布时间】:2018-03-15 07:54:32
【问题描述】:
我目前正在尝试使用 Laravel 5.4 中的队列发送邮件,以加快一些请求。但由于某种原因,我就是不会解决。
我的工作如下所示:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class NotificationEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $doer, $does, $user;
/**
* Create a new job instance.
*
* @param Podcast $podcast
* @return void
*/
public function __construct($doer, $does, $user)
{
$this->doer = $doer;
$this->does = $does;
$this->user = $user;
}
/**
* Execute the job.
*
* @param AudioProcessor $processor
* @return void
*/
public function handle()
{
$actions = [
'accepted.invite' => 'accepted your invited.',
'accepted.requesting' => 'accepted your request.',
'denied.invite' => 'denied your invite.',
'denied.requesting' => 'denied your request'
];
Mail::send('emails.notification', [
'doer' => $this->does,
'action' => $actions[$this->action]
], function ($m) {
$m->from('noreply@bigriss.com', 'Bigriss');
$m->to("myemail@gmail.com", 'Shawn')->subject('New Notification');
echo "SENT";
});
}
}
通过以下方式将其分派到另一个类中:
NotificationEmail::dispatch($doer, $does, $user);
在侦听队列php artisan queue:listen 后,我一分派作业,侦听器就会无休止地运行,试图解析句柄函数。我收到“已发送”消息,但从未发送过电子邮件(正如我在电子邮件提供商上看到的那样),队列实际上从未被删除,尝试次数只会无限增加。我在这里错过了什么吗?这不是队列的好处吗?
【问题讨论】:
-
是否有任何日志记录或 try/catch 以确保句柄函数运行时没有任何错误?
-
我尝试了 try/catch 并且它没有抛出异常。 @JaredChu
标签: php laravel message-queue jobs