Michael
Michael

Reputation: 921

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.

I've tried adding dd('test') to my listener to see if it is getting called. It's not.

I do have a queue running using redis and supervisor

Upvotes: 1

Views: 1798

Answers (2)

Mtxz
Mtxz

Reputation: 3869

Try:

$ php artisan clear-compiled
$ php artisan optimize
$ composer install
$ composer dumpautoload
$ php artisan cache:clear

Also make sure all is ok in your eventServiceProvider. If you use Queued job, check Kevin Kuiper answer.

Did same as you and had same behavior: event fired, but listener never reached. Worked as expected after those commands. Also used some dd() to test listener.

Source: mix of this and command I already used.

Upvotes: 2

Kelvin Kuiper
Kelvin Kuiper

Reputation: 21

I think you have to run the Queue Worker to process all the jobs in the queue. https://laravel.com/docs/5.3/queues#running-the-queue-worker

try php artisan queue:work

Upvotes: 2

Related Questions