在Horizon文档中提到可以在排队的事件监听器中添加自定义标签。然而,我找不到任何方法来获取包含所需数据的事件实例。给出的示例使用类型提示从服务容器中提取相关模型并将其分配给构造函数中的实例变量,然后在
但是,在排队的事件侦听器中执行此操作时,它不起作用。事实上,由于模型被序列化和“重新激活”时执行,因此似乎根本没有调用构造函数。因此,在构造函数中进行类型提示无效,而
在这种情况下,是否有人知道如何在标记中获取事件信息? 更新: 在控制器中调用的事件:
问题在于
tags()
方法中使用该实例变量来获取有关正在操作的特定模型实例的数据。但是,在排队的事件侦听器中执行此操作时,它不起作用。事实上,由于模型被序列化和“重新激活”时执行,因此似乎根本没有调用构造函数。因此,在构造函数中进行类型提示无效,而
tags()
似乎在handle()
之前调用,因此我无法访问正在侦听的事件对象。在这种情况下,是否有人知道如何在标记中获取事件信息? 更新: 在控制器中调用的事件:
event(new PostWasCreated($user, $post));
事件 PostWasCreated:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;
use App\Post;
class PostWasCreated
{
use InteractsWithSockets, SerializesModels;
public $user;
public $post;
public function __construct(User $user, Post $post)
{
$this->user = $user;
$this->post = $post;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
监听器PostWasCreatedNotificationSend:
<?php
namespace App\Listeners;
use App\Events\PostWasCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class PostWasCreatedNotificationSend implements ShouldQueue
{
protected $event;
public $queue = 'notifications'; // Adds queue name
public function __construct(PostWasCreated $event)
{
$this->event = $event;
// Does NOT add queue tag
$this->queueTags = ['post: ' . $this->event->post->id];
}
public function tags()
{
return $this->queueTags;
}
public function handle(PostWasCreated $event)
{
// handle event here...
}
}
问题在于
$this->queueTags
从未被赋值,因此在Horizon中没有标签用于这个队列的监听器...(队列名称可以显示,但我们也需要标签)。
CallQueuedListener
作业中提取事件以查找任何标签。从这个描述中很难理解正在发生什么。您能否展示您尝试排队的事件的代码?它是否表现出SerializesModels
特性? - Cy Rossignol