Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Throw out problem

When we release a new version of the code, we can gracefully kill an asynchronous task in progress to avoid killing it in the middle of a run.

The solution

After investigating, laravel’s Horizon extension solves this problem by gracefully terminating the process with the following command:

  1. Ensure that a process in progress is not killed and can be killed only after the execution is complete.
  2. Non-ongoing tasks wait and are not added to the queue.

Elegant solution to this problem, the idea is so simple.

php artisan horizon:terminate
Copy the code

Now let’s introduce Horizon

Introduction to the

Horizon provides a beautiful dashboard that allows you to code the Laravel Redis queue, while allowing you to easily monitor key metrics such as task throughput, uptime and failed tasks in your queue system.

The installation

Note: Make sure the Redis queue driver is set in the queue configuration file.

Install Horizon for the Laravel project using Composer:

composer require laravel/horizon
Copy the code

Once the installation is complete, issue the Artisan command using horizon:install:

php artisan horizon:install
Copy the code

The failed_JOBS table needs to be created, and Laravel will use this table to store any failed queue jobs:

php artisan queue:failed-table

php artisan migrate
Copy the code

Upgrade the Horizon

Before upgrading to a new major version of Horizon, be sure to take a good look at the update announcement, familiarize yourself with the new features, and consider any pitfalls in light of the business scenario.

Note, be sure to republish Horizon resource files after the upgrade:

php artisan horizon:assets
Copy the code

configuration

After publishing the Horizon file, its main configuration file will be placed in config/ Horizon.

Queue-related options can be configured in this file, with detailed instructions for each configuration item.

Note: Ensure that the Environments section of the Horizon profile contains entries for each environment on which you plan to run Horizon.

'environments' => [
    'production' => [
        'supervisor-1' => [
            'connection' => 'redis',
            'queue' => [
                'high_{0}',
            ],
            'balance' => 'auto',
            'minProcesses' => 1,
            'maxProcesses' => 2,
            'tries' => 1
        ],
    ],

    'test' => [
        'supervisor-1' => [
            'connection' => 'redis',
            'queue' => [
                'high_{0}',
            ],
            'balance' => 'auto',
            'minProcesses' => 1,
            'maxProcesses' => 2,
            'tries' => 1
        ],
    ],

    'local' => [
        'supervisor-1' => [
            'connection' => 'redis',
            'queue' => [
                'high_{0}',
            ],
            'balance' => 'auto',
            'minProcesses' => 3,
            'maxProcesses' => 11,
            'tries' => 1
        ],
    ],
],
Copy the code

Equilibrium configuration

Horizon offers three equalization strategies: Simple, Auto, and False.

  1. The default is simple, which evenly distributes the received tasks to the queue process:
'balance' => 'simple',
Copy the code
  1. The Auto policy adjusts the number of worker process tasks in each queue based on the current workload.

For example, if queue A has 1000 pending tasks, but your queue B is empty, Horizon will allocate more worker processes to queue B until all tasks in queue A are completed.

  1. When the balance configuration item is set to false, Horizon uses the Laravel default execution behavior, which processes the queue tasks in the order listed in the configuration.

When using the Auto policy, you can define minProcesses and maxProcesses configuration options to control how the minimum and maximum number of processes ranges should be extended up and down to:

'environments' => [
    'production' => [
        'supervisor-1' => [
            'connection' => 'redis',
            'queue' => ['default'],
            'balance' => 'auto',
            'minProcesses' => 1,
            'maxProcesses' => 10,
            'tries' => 3,
        ],
    ],
],
Copy the code

Failed Task Record

The Horizon profile allows you to configure how long, in minutes, recent and failed tasks should be retained. By default, the most recent task is kept for one hour and failed tasks are kept for a week:

'trim' => [
    'recent' => 60,
    'failed' => 10080,
],
Copy the code

Verify dashboard permission

Horizon dashboard routing is/Horizon.

By default, you can only access the dashboard in a local environment.

In your app/will/HorizonServiceProvider. PHP file, there is a gate way.

This authorization controls access to Horizon in non-local environments.

Feel free to modify this facade as needed to limit access to the Horizon installation:

/**
 * Register the Horizon gate.
 *
 * This gate determines who can access Horizon in non-local environments.
 *
 * @return void
 */
protected function gate()
{
    Gate::define('viewHorizon', function ($user) {
        return in_array($user->email, [
            '[email protected]',
        ]);
    });
}
Copy the code

Note: Remember that Laravel automatically injects Authenticated users into the Gate method.

Horizon users may not need to “log in” if the application provides Horizon security through other means, such as IP restrictions.

conclusion

The question I asked at the beginning of the article was something I didn’t consider during development,

The beauty of PHP Artisan Horizon: Terminate was also missing from the initial documentation.

As the project continued to grow, so did my technical level and programming ideas.

Next time bye

The next article will summarize the Horizon advancements, including: installation, deployment, tagging, notification, Metrics, etc.