Tagging jobs for monitoring in Laravel Horizon

Laravel Horizon is shipped with many amazing features that help you understand what goes on with your queue workers, my personal favorite feature is the ability to tag jobs for further investigation.

For example you can tag a group of jobs as reports, later on you can see how many report-related jobs ran and what's the status of each of them.


You may assign any number of tags to your jobs by defining a tags() method in the job class, this method should return an array of tag names you wish to assign to this job.

class TotalSalesReport implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        // Generate the report and store the results...
    }

    public function tags()
    {
        return ['reports'];
    }
}

This doesn't only work for Job classes, you can assign tags to your Notifications, Mailables, Events, and Listeners.

Tagging listeners

When you define a tags method in your listener, Horizon will assign the given tags to your queued listener alongside with tags assigned to the event class itself:

class OrderWasMade
{
    public function tags()
    {
        return ['new-orders'];
    }
}

class SendOrderInvoice
{
    public function handle()
    {
        // ...
    }

    public function tags()
    {
        return ['invoices'];
    }
}

In this example, the new-orders and invoices tags will be assigned to your SendOrderInvoice job so if you monitor any of these tags you'll be able to find information about this listener there.

Auto-tagging

One very powerful feature is that Horizon auto tags jobs that has a class property that holds an Eloquent Model, this is extremely helpful when you need to track a job that's causing troubles with a specific entity in your system.

For example user with id = 123 complains that he doesn't receive emails anymore, all you need to do is start monitoring the App\User:123 tag and all jobs having this user as a property will be listed for you. This feature also works for Mailables, Notifications, and Events.

class SendInvoice implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function handle()
    {
        // ...
    }
}