【发布时间】:2018-03-31 15:26:02
【问题描述】:
Laravel 文档解释说,在调度/发送通知时,您可能希望将它们排队以加快应用程序响应时间。
https://laravel.com/docs/5.5/notifications#queueing-notifications
这正是我想要做的,但是我使用通知外观而不是通知特性来调用它。我担心的是前者是绕过队列,我需要它来一次通知一组用户。
正如文档中所说:
或者,您可以通过 Notification 门面发送通知。 这主要在您需要向 多个应通知实体,例如用户集合。
但是当我通过外观调用我的通知时,它不会排队。我知道这一点是因为当我监控我的网络请求并注释掉外观调用时,我的请求会从超过 2 秒(使用通知调用)到不到 0.5 秒(当我注释掉它时)。
这是我使用队列的通知类的开始 (NewAsset):
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class NewAsset extends Notification implements ShouldQueue
{
use Queueable;
来电:
$asset = new Asset;
$asset->user_id = Auth::user()->id;
$asset->type = "Text";
$asset->content = $content;
$asset->forum_id = 1;
$asset->save();
$users = User::where("id","!=",Auth::user()->id)->get();
Notification::send($users, new NewAsset($asset,Auth::user()));
//if i comment out the notification call above, response time decreases dramatically
return;
我做错了什么?
哦……好像触发了队列:
php artisan queue:listen
[2018-03-31 15:48:22] Processing: App\Notifications\NewAsset
[2018-03-31 15:48:22] Processed: App\Notifications\NewAsset
[2018-03-31 15:48:23] Processing: App\Notifications\NewAsset
[2018-03-31 15:48:23] Processed: App\Notifications\NewAsset
[2018-03-31 15:48:24] Processing: App\Notifications\NewAsset
[2018-03-31 15:48:24] Processed: App\Notifications\NewAsset
[2018-03-31 15:48:25] Processing: App\Notifications\NewAsset
[2018-03-31 15:48:25] Processed: App\Notifications\NewAsset
那为什么这么慢呢? :(
【问题讨论】:
-
你是否在你的用户模型中包含了可通知的特征?
-
@WhyDoesMyCodeWork 是的,确认它在那里。
-
@WhyDoesMyCodeWork 查看命令行输出编辑...
-
你用哪个驱动来排队?
-
@WhyDoesMyCodeWork 数据库
标签: laravel laravel-5.5