mardi 3 janvier 2017

Laravel 5.2 event not firing in production

I have a laravel event that works fine locally but not in production. I need it to both broadcast to redis as well as to a laravel listener. Broadcasting to redis works as expected, but the laravel listener seems to not be working.

Here is the code for the event :

<?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'];
    }

}

Here is the code for the listener

<?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'));
   }
 }

And here is my Event Service Provider:

<?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;
    }
}

I've tried running composer dumpautoload and php artisan clear-compiled with no luck.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire