laravel / horizon

Dashboard and code-driven configuration for Laravel queues.
https://laravel.com/docs/horizon
MIT License
3.88k stars 659 forks source link

[5.x] Pass event instance to event listeners tag() method #1361

Closed mateusjatenee closed 11 months ago

mateusjatenee commented 11 months ago

A common problem I have is that it's hard to deal with tags on queued event listeners. On a regular job, you have the payload available on the constructor, so you can use that as usual, e.g:

public function tags()
{
    return ['user_id' => $this->userId];
}

On an event listener, the event instance is passed to the handle method, so when Horizon calls ->tags(), we can't access any context. This PR aims to improve that by storing the event that's being handled when building tags, and passing that event instance to the tags method, so we can do something like this:

<?php

class MyEventListener implements ShouldQueue
{
  public function handle(MyEvent $event)
  {}

  public function tags(MyEvent $event)
  {
    return [
      'context_id' => $event->uniqueId(),
    ];
  }
}