我有一个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(&#39; test&#39;)添加到我的监听器中,看看它是否被调用。不是。
我确实有一个使用redis和supervisor运行的队列
答案 0 :(得分:1)
尝试:
$ php artisan clear-compiled
$ php artisan optimize
$ composer install
$ composer dumpautoload
$ php artisan cache:clear
同时确保eventServiceProvider
中的一切正常。如果您使用排队的工作,请检查Kevin Kuiper的答案。
与您相同并且具有相同的行为:事件被触发,但是侦听器从未到达。在这些命令之后按预期工作。还使用了一些dd()
来测试监听器。
来源:this与我已经使用过的命令的混合。
答案 1 :(得分:0)
我认为您必须运行Queue Worker来处理队列中的所有作业。 https://laravel.com/docs/5.3/queues#running-the-queue-worker
尝试php artisan队列:工作