网页
综合
更新时间 2026-11-13 17:31:45
Laravel Horizon | Laravel 13.x - The clean stack for Artisans and agents
自动蜘蛛
laravel.com
正文约 6000 字
Before digging into Laravel Horizon, you should familiarize yourself with Laravel's base queue services. Horizon augments Laravel's queue with additional features that may be confusing if you are not already familiar with the basic queue features offered by Laravel.
Laravel Horizon provides a beautiful dashboard and code-driven configuration for your Laravel powered Redis queues. Horizon allows you to easily monitor key metrics of your queue system such as job throughput, runtime, and job failures.
When using Horizon, all of your queue worker configuration is stored in a single, simple configuration file. By defining your application's worker configuration in a version controlled file, you may easily scale or modify your application's queue workers when deploying your application.
Laravel Horizon requires that you use Redis to power your queue. Therefore, you should ensure that your queue connection is set to redis in your application's config/queue.php configuration file. Horizon is not compatible with Redis Cluster at this time.
You may install Horizon into your project using the Composer package manager:
After installing Horizon, publish its assets using the horizon:install Artisan command:
After publishing Horizon's assets, its primary configuration file will be located at config/horizon.php. This configuration file allows you to configure the queue worker options for your application. Each configuration option includes a description of its purpose, so be sure to thoroughly explore this file.
Horizon uses a Redis connection named horizon internally. This Redis connection name is reserved and should not be assigned to another Redis connection in the database.php configuration file or as the value of the use option in the horizon.php configuration file.
If you would like to use a nonce attribute on the script and style tags used in Horizon views as part of your Content Security Policy, you may use the Horizon::cspNonce method to specify the nonce to use. This method should typically be invoked within middleware so that a new nonce is assigned for each request:
You may add this middleware to the middleware option in your application's config/horizon.php configuration file:
After installation, the primary Horizon configuration option that you should familiarize yourself with is the environments configuration option. This configuration option is an array of environments that your application runs on and defines the worker process options for each environment. By default, this entry contains a production and local environment. However, you are free to add more environments as needed:
You may also define a wildcard environment (*) which will be used when no other matching environment is found:
When you start Horizon, it will use the worker process configuration options for the environment that your application is running on. Typically, the environment is determined by the value of the APP_ENV environment variable. For example, the default local Horizon environment is configured to start three worker processes and automatically balance the number of worker processes assigned to each queue. The default production environment is configured to start a maximum of 10 worker processes and automatically balance the number of worker processes assigned to each queue.
You should ensure that the environments portion of your horizon configuration file contains an entry for each environment on which you plan to run Horizon.
As you can see in Horizon's default configuration file, each environment can contain one or more "supervisors". By default, the configuration file defines this supervisor as supervisor-1; however, you are free to name your supervisors whatever you want. Each supervisor is essentially responsible for "supervising" a group of worker processes and takes care of balancing worker processes across queues.
You may add additional supervisors to a given environment if you would like to define a new group of worker processes that should run in that environment. You may choose to do this if you would like to define a different balancing strategy or worker process count for a given queue used by your application.
While your application is in maintenance mode, queued jobs will not be processed by Horizon unless the supervisor's force option is defined as true within the Horizon configuration file:
Within Horizon's default configuration file, you will notice a defaults configuration option. This configuration option specifies the default values for your application's supervisors. The supervisor's default configuration values will be merged into the supervisor's configuration for each environment, allowing you to avoid unnecessary repetition when defining your supervisors.
The Horizon dashboard may be accessed via the /horizon route. By default, you will only be able to access this dashboard in the local environment. However, within your app/Providers/HorizonServiceProvider.php file, there is an authorization gate definition. This authorization gate controls access to Horizon in non-local environments. You are free to modify this gate as needed to restrict access to your Horizon installation:
Remember that Laravel automatically injects the authenticated user into the gate closure. If your application is providing Horizon security via another method, such as IP restrictions, then your Horizon users may not need to "login". Therefore, you will need to change function (User $user) closure signature above to function (User $user = null) in order to force Laravel to not require authentication.
Before refining these options, make sure you are familiar with Laravel's default queue services and the concept of 'attempts'.
You can define the maximum number of attempts a job can consume within a supervisor's configuration:
This option is similar to the --tries option when using the Artisan command to process queues.
Adjusting the tries option is essential when using middlewares such as WithoutOverlapping or Ra
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。