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.

To install Horizon, you need to execute the following command:

composer require laravel/horizon

After installation, Horizon will be available via the link http://project-name/horizon.

As you can see, Horizon is currently inactive and queue monitoring is off. To activate Horizon you need to execute this command:

php artisan horizon

This command sends the configuration file Horizon to the directory with other Laravel configuration files:

​php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"

After that, you’ll have access to the config/horizon.php file with settings:

Laravel Horizon app development

To check how Laravel Horizon works, we’ll implement a simple example with a few jobs that will be processed with the help of queues.

In our example, we’ll generate new users with a set of posts. Each newly created user will have 50 new articles. All this data will be saved in the database.

Let’s start with data migration. Laravel has built-in migration capabilities for database tables, so all we have to do is create a migration for our Posts table.

Laravel Horizon

Next, we’ll prepare our factories that will generate articles and users.

You can create a factory with the command:

​php artisan make:factory NameFactory

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

Laravel Horizon

Horizon Laravel

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.

Laravel Horizon for app development Yalantis

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:

CreatePosts implementation by Laravel Horizon

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.

Laravel Horizon monitoring Yalantis

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.

Laravel Horizon app development at Yalantis

Laravel Horizon

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.

Снимок экрана 2017-08-19 в 17.37.31.png

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.

Monitoring Laravel horizon Yalantis app development

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.3/ 5.0
Article rating
57
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. Whould you tell us how you feel about this article?