【问题标题】:Laravel Notification Facade Queue Response TimeLaravel 通知外观队列响应时间
【发布时间】: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


【解决方案1】:

Notification::send 很慢(> 2 秒),很有可能您有数千个通知实体推送到数据库队列,这很慢,因为在数据库上执行了数千个插入语句。要改进,您可以:

  1. 使用其他队列驱动程序,例如 Amazon SQS、Beanstalkd、Redis 等。它们针对低延迟的工作队列进行了优化。与作为工作队列的数据库相比,它们快如闪电。

  2. 再创建一个job,让worker将所有通知排队,例如:

    php artisan make:job QueueUserNotificationsJob
    

    YourController.php

    dispatch(new QueueUserNotificationsJob(Auth::user()->id));
    

    QueueUserNotificationsJob.php

    public $authUserId = null;
    
    public function __construct($authUserId) {
    
        $this->authUserId = $authUserId;
    
    }
    
    public function handle() {
    
        $users = User::where("id", "!=", $this->authUserId)->get();
    
        Notification::send($users, new NewAsset($asset, $this->authUserId));
    
    }
    

【讨论】:

  • 我认为implements ShouldQueue 上的QueueableQueueable 的全部意义在于无需这样做就可以排队。以这种方式运行 Notification::send 是不是应该是异步的,或者不是?
  • 创建新工作和在通知本身上实现ShouldQueue 有什么区别?
  • 延迟用于将作业推入队列(创建作业),而不是消耗作业。如果使用数据库作为队列驱动,它并没有优化为工作队列,它只是一个一个地插入作业,这是非常慢的操作。这就是我们将使用替代队列驱动程序的原因
  • 因此,通过使用一项作业来对各种通知进行排队,我避免了延迟...此时我无法使用其他驱动程序。那么通知是否需要ShouldQueueQueueable 或者我可以摆脱这些吗?还有一些关于creatingconsuming 作业和队列以及相关时间安排的文档或源代码吗?这对我来说很有趣。
  • ShouldQueue 如果你想将工作分派给不同的工人,应该实现。例如,您有 4 个工作人员,并且没有 ShouldQueue,新实现一个工作人员用于创建和消费工作,但实现了ShouldQueue,一个工作人员用于创建工作,之后所有工作人员都用于消费工作.这就是区别。
猜你喜欢
  • 2017-05-31
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
相关资源
最近更新 更多