0

I have one event with two listeners. I need to stop listener propagation when the first listener fails. For example:

Event: RegisterUserEvent Listeners: StoreUserListener and SendVerificationEmailListener

If, for any reason, the user can't be stored in database, I want that SendVerificationEmailListener doesn't execute.

I'm I using Lumen 8.x and the events are processed with Redis.

Part of my code is (Registering Events and Listeners):

`protected $listen = [
   RegisterReservationEvent::class => [
      RegisterReservationInDatabase::class,
      SendReservationEmail::class
   ],
];`

First listener executed:

`public function handle(RegisterReservationEvent $event): bool
        {
            try {
                if (formReservationExists($event->formId, $event->guestReservationId)) {
                    throw new FormException("Reservation {$event->guestReservationId} already registered in form {$event->formId}");
                }
    
                $data = [
                    'form_id' => $event->formId,
                    'guest_reservation_id' => $event->guestReservationId,
                    'token' => Str::uuid()->toString(),
                    'status' => 'ready_to_send',
                ];
    
                $reservation = FormReservation::create($data);
    
                if ($reservation === null) {
                    throw new FormException("Error saving reservation {$event->guestReservationId} in form {$event->formId}");
                }
    
            } catch (Exception $e) {
                dump($e->getMessage());
                return false;
            }
        }`

And the listener that I don't want to execute is:

`public function handle(RegisterReservationEvent $event)
    {
        dump('Executed for ' . $event->guestReservationId);
    }`

But it was executed anyway..

I am using Redis to process the listeners.

And this is the result when queue runs

Thanks.

1 Answers1

0

I don't know about lumen however in laravel according to their documentation.

Sometimes, you may wish to stop the propagation of an event to other listeners. You may do so by returning false from your listener's handle method.

Maybe it is the same for lumen. source

Edit: just found the same in lumen's documentation.

Stopping The Propagation Of An Event Sometimes, you may wish to stop the propagation of an event to other listeners. You may do so using by returning false from your listener's handle method.

LoaiAkram
  • 81
  • 4
  • Okay, first, thanks for your answer. Second, I've tried return false, but I don't see any change. The listeners are executed equally... But I'll keep trying and if I can solve this, I will tell you. Thank you. – Federico Juretich Nov 24 '21 at 13:21
  • @FedericoJuretich it might be a good idea to update the question with a bit of code. also the listeners run in the same order as in the array given in the `EventServiceProvider`. – LoaiAkram Nov 24 '21 at 13:52