我有一个 Laravel 事件,在本地运行良好,但在生产环境中运行不佳。我需要它向 Redis 以及 Laravel 监听器广播。广播到 Redis 按预期工作,但 Laravel 监听器似乎无法工作。
这是活动代码:
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class UserAlert extends Event implements ShouldBroadcast
{
public $email;
public $type;
public $message;
public $data;
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($email, $type, $message, $data = [])
{
$this->email = $email;
$this->type = $type;
$this->message = $message;
$this->data = $data;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['user-alert'];
}
}
这是监听器的代码
<?php
namespace App\Listeners;
use App\Events\UserAlert;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Redis;
use Log;
class UserAlertListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
public function subscribe($events)
{
$events->listen(
'App\Events\UserAlert',
'App\Listeners\UserAlertListener@handle'
);
}
/**
* Handle the event.
*
* @param UserAlert $event
* @return void
*/
public function handle(UserAlert $event)
{
$key = $event->email;
$arr[] = $event;
$current = json_decode(Redis::get($key));
if(isset($current)){
$arr = array_merge($arr, $current);
}
Redis::set($key, json_encode($arr));
Redis::expireat($key, strtotime('+1 week'));
}
}
这是我的活动服务提供商:
<?php
namespace Illuminate\Foundation\Support\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use App\Events\UserAlert;
use App\Listeners\UserAlertListener;
class EventServiceProvider extends ServiceProvider
{
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\UserAlert' => [
'App\Listeners\UserAlertListener'
],
];
/**
* The subscriber classes to register.
*
* @var array
*/
protected $subscribe = [
'App\Listeners\UserAlertListener'
];
/**
* Register the application's event listeners.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function boot(DispatcherContract $events)
{
foreach ($this->listens() as $event => $listeners) {
foreach ($listeners as $listener) {
$events->listen($event, $listener);
}
}
foreach ($this->subscribe as $subscriber) {
$events->subscribe($subscriber);
}
}
/**
* {@inheritdoc}
*/
public function register()
{
//
}
/**
* Get the events and handlers.
*
* @return array
*/
public function listens()
{
return $this->listen;
}
}
我尝试过运行
composer dumpautoload
和 php artisan clear-compiled
,但没有成功。
我尝试将 dd('test') 添加到我的监听器中以查看它是否被调用。不是这样的。
我确实有一个使用 Redis 和 Supervisor 运行的队列
我认为你必须运行队列工作程序来处理队列中的所有作业。 https://laravel.com/docs/5.3/queues#running-the-queue-worker
尝试 php artisan 队列:工作
尝试:
$ php artisan clear-compiled
$ php artisan optimize
$ composer install
$ composer dumpautoload
$ php artisan cache:clear
还要确保您的
eventServiceProvider
一切正常。如果您使用队列作业,请查看 Kevin Kuiper 的回答。
与您做了相同的操作并具有相同的行为:事件已触发,但侦听器从未到达。在这些命令之后按预期工作。还使用了一些
dd()
来测试监听器。
来源:this和我已经使用过的命令的混合。