Laravel Horizon is Laravel's Redis queue management tool that provides visual monitoring and performance optimization capabilities. 1. Before installation, you need to make sure that you use the Redis driver, have installed Laravel projects and configure the PHP and Redis environment; 2. After installing and publishing resources through Composer, correctly configure the .env and queue.php files; 3. Set access permissions to protect the monitoring panel and only authorize access by specific users; 4. Use the web interface to view the queue status, including the number of tasks, execution time, and failed tasks, etc.; 5. Configure multiple work pools to listen to different queues, and reasonably set the number of processes and retry times to improve efficiency; 6. When handling failed tasks, it is recommended to set the retry logic, add a notification mechanism, and clean up the failure records regularly, and run the migration command if necessary to create a failed task table.
Of course, Laravel Horizon is a powerful queue management tool designed for Laravel's Redis driver queue systems. It provides a beautiful dashboard to monitor and manage your backend tasks (such as email sending, data processing, etc.), and can also help you optimize queue performance.

Here are some practical suggestions and how to use Horizon:

Installation and configuration are the key first steps
Before you start using Horizon, make sure you have met the following conditions:
- Redis as queue driver
- Laravel project installed
- Both PHP and Redis environments work properly
Install Horizon through Composer:

composer requires laravel/horizon
Then run the Publish command:
php artisan horizon:install
This generates the necessary configuration files and resources. Next, set QUEUE_CONNECTION
to redis
in the .env
file and make sure the redis connection is configured correctly in config/queue.php
.
Monitoring panel allows you to grasp queue status
Horizon provides a web interface to show the current queue's running status, including the number of tasks, execution time, failed tasks and other information.
To access this interface, you need to set access permissions first. In app/Providers/HorizonServiceProvider.php
, modify the gate
method to control which users can access the dashboard. For example, only allow login for specific email addresses:
Gate::define('viewHorizon', function ($user) { return in_array($user->email, ['you@example.com']); });
Open the browser to access the /horizon
path to see real-time monitoring data. This is very useful for troubleshooting queue backlogs, delays and other issues.
Balance the number of processes and queue priority to improve efficiency
Horizon allows you to configure multiple "supervisors", each of which can listen to different queues and specify how many worker processes to start.
For example, you can configure it like this:
'environments' => [ 'production' => [ 'supervisor-1' => [ 'connection' => 'redis', 'queue' => ['default'], 'processes' => 10, 'tries' => 3, ], 'supervisor-2' => [ 'connection' => 'redis', 'queue' => ['high'], 'processes' => 5, 'tries' => 3, ], ], ],
In the example above, we set up two supervisors to handle the queues with default and high priority respectively. Note that the number of processes is not as large as possible , and it should be allocated reasonably in combination with server resources.
Don't ignore failed tasks
When the task fails, Horizon will record it automatically. You can view the reasons for failure, retry tasks, or delete them on its interface.
To better handle failed tasks, it is recommended:
- Add a reasonable number of retry times to the task (
tries
) - Add failed callback logic (such as sending notifications)
- Regularly clean up long-term failed tasks to avoid accumulation
If you use a database to store failed tasks, remember to run the migration:
php artisan queue:failed-table php artisan migrate
Basically that's it. Using Laravel Horizon well can greatly improve your ability to control background tasks. Although the configuration is not complicated, details are easily overlooked, especially the permissions and queue priority.
The above is the detailed content of Queuing Background Jobs with Laravel Horizon?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

InLaravel,policiesorganizeauthorizationlogicformodelactions.1.Policiesareclasseswithmethodslikeview,create,update,anddeletethatreturntrueorfalsebasedonuserpermissions.2.Toregisterapolicy,mapthemodeltoitspolicyinthe$policiesarrayofAuthServiceProvider.

To create new records in the database using Eloquent, there are four main methods: 1. Use the create method to quickly create records by passing in the attribute array, such as User::create(['name'=>'JohnDoe','email'=>'john@example.com']); 2. Use the save method to manually instantiate the model and assign values ??to save one by one, which is suitable for scenarios where conditional assignment or extra logic is required; 3. Use firstOrCreate to find or create records based on search conditions to avoid duplicate data; 4. Use updateOrCreate to find records and update, if not, create them, which is suitable for processing imported data, etc., which may be repetitive.

Artisan is a command line tool of Laravel to improve development efficiency. Its core functions include: 1. Generate code structures, such as controllers, models, etc., and automatically create files through make: controller and other commands; 2. Manage database migration and fill, use migrate to run migration, and db:seed to fill data; 3. Support custom commands, such as make:command creation command class to implement business logic encapsulation; 4. Provide debugging and environment management functions, such as key:generate to generate keys, and serve to start the development server. Proficiency in using Artisan can significantly improve Laravel development efficiency.

Yes,youcaninstallLaravelonanyoperatingsystembyfollowingthesesteps:1.InstallPHPandrequiredextensionslikembstring,openssl,andxmlusingtoolslikeXAMPPonWindows,HomebrewonmacOS,oraptonLinux;2.InstallComposer,usinganinstalleronWindowsorterminalcommandsonmac

ToruntestsinLaraveleffectively,usethephpartisantestcommandwhichsimplifiesPHPUnitusage.1.Setupa.env.testingfileandconfigurephpunit.xmltouseatestdatabaselikeSQLite.2.Generatetestfilesusingphpartisanmake:test,using--unitforunittests.3.Writetestswithmeth

Defining a method (also known as an action) in a controller is to tell the application what to do when someone visits a specific URL. These methods usually process requests, process data, and return responses such as HTML pages or JSON. Understanding the basic structure: Most web frameworks (such as RubyonRails, Laravel, or SpringMVC) use controllers to group related operations. Methods within each controller usually correspond to a route, i.e. the URL path that someone can access. For example, there may be the following methods in PostsController: 1.index() – display post list; 2.show() – display individual posts; 3.create() – handle creating new posts; 4.u

The main role of the controller in Laravel is to process HTTP requests and return responses to keep the code neat and maintainable. By concentrating the relevant request logic into a class, the controller makes the routing file simpler, such as putting user profile display, editing and deletion operations in different methods of UserController. The creation of a controller can be implemented through the Artisan command phpartisanmake:controllerUserController, while the resource controller is generated using the --resource option, covering methods for standard CRUD operations. Then you need to bind the controller in the route, such as Route::get('/user/{id

Laravel allows custom authentication views and logic by overriding the default stub and controller. 1. To customize the authentication view, use the command phpartisanvendor:publish-tag=laravel-auth to copy the default Blade template to the resources/views/auth directory and modify it, such as adding the "Terms of Service" check box. 2. To modify the authentication logic, you need to adjust the methods in RegisterController, LoginController and ResetPasswordController, such as updating the validator() method to verify the added field, or rewriting r
