laravel5.5 (未queue:restart导致的)laravel-echo在Event Broadcat变动的时候在channel public property没有相应的变化的问题

场景

  • 实现一个仿知乎的私信功能
  • 技术栈
    • laravel-echo
    • pusher
    • MessageCreatedEvent(被created事件触发)
    • supervisor
  • 问题
    • 当我试图让MessageCreatedEvent broadcast由1个专成多个private channel 的时候, 前端代码没有监听到事件, 查了下Pusher Debug Console 也没有收到第二条广播

参考文档

分析

  • 尝试清除一起可能的缓存(自然不是这个原因了)
    • clear-compiled
    • cache:clear
    • view:clear
  • 重启队列 ok!

Since queue workers are long-lived processes, they will not pick up changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. You may gracefully restart all of the workers by issuing the queue:restart command:
php artisan queue:restart
This command will instruct all queue workers to gracefully “die” after they finish processing their current job so that no existing jobs are lost. Since the queue workers will die when the queue:restart command is executed, you should be running a process manager such as Supervisor to automatically restart the queue workers.

简单翻译一下

queue workers是long-lived的进程,更改代码后,queue workers是不能捕捉到变化的; php artisan queue:retsrat 会在通知所有的queue workers 在完成当前job后, die掉进程; 如果你配合supervisor进行队列管理,那么supervisor自然会重启queue workers

解决

  • 重启队列解决问题, 不太了解supervisor的原理是啥,以后研究下
    • 方法 1
      • kill 1769
    • 方法2
      • sudo supervisorctl status
      • sudo supervisorctl restart program_name
    • 方法3
      • php artisan queue:restart 推荐这种; 但是需要注意的是,这个命令会重启所有队列; 通知进程完成当前任务的之后,优雅的断开进程; 但是仅此而已,所以需要supervisor来管理队列,只有这样才会在执行过php artisan queue:restart 命令之后, 重启队列queue worker

MessageCreatedEvent源码

<?php

namespace App\Events;

use App\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class MessageCreateEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $from_user;
    public $message;

    public $broadcastQueue = 'private-message-to-created';

    public function broadcastWhen()
    {
        return $this->message->user_id == $this->message->to_user_id;
    }

    /**
     * Create a new event instance.
     *
     * @param Message $message
     */
    public function __construct(Message $message)
    {

        $this->message = $message;
        $this->from_user = $this->message->fromUser;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        $private_counter_channel = new PrivateChannel('message-to-counter.' . $this->message->user_id);
        $private_created_channel = new PrivateChannel('message-to-created.' . $this->message->user_id);
        return [
            $private_counter_channel,
            $private_created_channel
        ];
    }
}

猜你喜欢

转载自blog.csdn.net/cominglately/article/details/85055514