How to Work With Laravel Horizon – a Dashboard for Redis Queues

A new Laravel framework – Laravel Horizon – was announced on July 26, 2017. Laravel Horizon is a dashboard for Redis queues.

This tool instantly became popular among developers and within a month of its release had collected more than 1200 stars on GitHub, making it the most popular PHP repository on the site.

Laravel Horizon is a single-page application that has a simple and succinct design that was developed using the Vue.js framework that is often paired with Laravel.

What makes Laravel Horizon so valuable for developers? Let’s see what the tool can do:

1. Laravel Horizon can monitor queues and major KPIs

With Laravel Horizon’s help, we can access general statistics about all queues – how many tasks have been completed within the last hour or minute and how much time they took.

All tasks can be conveniently monitored with tags and thereby divided into logical groups. At the same time, one task can contain multiple tags (you’ll see such an example further in the article).

Laravel Horizon also gives project managers access to performance data so they can learn about task lifecycles. You can create reports on completed tasks and the time they took either by task or by task queue. The framework also forms a general report where managers can monitor current task statuses.

2) Laravel Horizon helps manage failed jobs

This functionality helps developers and managers track all failed jobs where tasks were completed with errors. Here you can see the stack trace of errors and all parameters that were sent to each task; you can also reload tasks.

3) Laravel Horizon has useful notifications

Laravel Horizon can send you notifications if a queue has too long of a wait time. There are two channels of communication available: SMS messages or Slack messages.

Now let’s move on to the practical part and start with the actual installation of the framework.

The Laravel Horizon framework also has a default function for creating new users.

Our next step is to create jobs that will be executed in this queue. In our example, we’ll have two jobs – creating a user and generating a number of articles for this user.

This job is created with php artisan make:

job JobName.

Let’s see what happens when the job CreateUser is created. first of all we need to generate a new user. To do this we will send the User class to our factory() helper. As a result the factory will return an object that belongs to the User class (you can see that on line 18 in the example). After that, we launch a simple cycle where we dispatch a task for creating an article that will be connected to the created user.

The CreatePosts implementation is shown below:

We now are going to move User object that was created by CreateUser. Remember that you need to create a one-to-many connection within the User model if you want to be able to address the $this->user->posts() method.

public function posts()
{
return $this->hasMany(Post::class);
}

Perhaps you’re wondering why you need the tags() method? With the help of tags we can divide all jobs into logical groups. But for such division to work in Laravel Horizon, you need to add tags that we are planning to track. To do this, we can move to the Monitoring section of the admin panel and add the tags post and user.

Having done that, we’ve completed our preparation for working with Laravel. Now all we have to do is add a route and create a controller with a method that will be used for processing a response, which in turn will be generate users and articles through activity queues.

To start generating users and articles, we’ll send a GET request to the route/start that we’ve already created. To do that just follow the link to http://project-name/start in your browser.

If it executes properly, you’ll see a Success notification in the top left corner of your screen.

Now it’s high time we looked at the results presented in the Horizon dashboard. We can see here that 5252 jobs have been processed.

If you move to the Monitoring section, you can see the list of tags that we added and the number of jobs that were completed by tag.

If you click on one of the tags, you’ll see a list of completed jobs with current statuses.

In the Failed section you can trace all jobs that had errors. Horizon shows a full stack trace of each error as well as all incoming parameters that were sent to the Job class constructor.

Finally, I’d like to remind you that at the production stage you should limit access to Horizon. To do this you can regulate access to data using the Horizon::auth method:

Horizon::auth(function ($request) {
// return true / false;
});

4.4/ 5.0

Article rating

81

Reviews

Remember those Facebook reactions? Well, we aren't Facebook but we love reactions too. They can give us valuable insights on how to improve what we're doing. Would you tell us how you feel about this article?