Setting Up Different Horizon Configs Based on Server in Laravel
Автор: vlogize
Загружено: 2025-08-07
Просмотров: 0
Описание:
Discover how to configure Laravel Horizon to dedicate specific queues to designated servers using environment variables for optimal performance and maintenance.
---
This video is based on the question https://stackoverflow.com/q/77430451/ asked by the user 'TKoL' ( https://stackoverflow.com/u/1959054/ ) and on the answer https://stackoverflow.com/a/77430869/ provided by the user 'Marcin Orlowski' ( https://stackoverflow.com/u/1235698/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Different horizon configs based on server
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Managing Multiple Horizon Configurations for Laravel
When managing a Laravel application that utilizes Horizon, a common challenge developers face is configuring different Horizon setups across multiple servers. Specifically, you might want to allocate specific queues to designated servers. For instance, you have a queue named Webhooks that you want to assign exclusively to a second server dedicated to processing these jobs, while ensuring that your main server avoids processing anything from the Webhooks queue. How can you achieve this setup without much hassle?
The Challenge
In your scenario, you have two servers:
Main Server: Used for processing requests but should not handle any jobs from the Webhooks queue.
Second Server: Specifically set up to process jobs from the Webhooks queue.
Goal:
Ensure the Main Server has zero processes for the Webhooks queue.
Allocate, for example, 20 processes on the Second Server for the same queue.
Possible Solutions
1. Using Separate Horizon Configurations (Not Recommended)
Initially, one might consider maintaining separate horizon.php configuration files across the servers. Here's how this would look:
Main Server would have a horizon.php file with 0 processes assigned to the Webhooks queue.
Second Server would feature a version of the horizon.php file with 20 processes assigned.
Though this method is straightforward, it can quickly become cumbersome. Maintaining two separate files means that any change to the configuration needs to be mirrored in both files, leading to potential inconsistencies and a maintenance headache.
2. Using a Unified Configuration with Environment Variables (Recommended)
A more efficient approach is to utilize a single shared horizon.php file while delegating specific parameters that differ between servers to environment variables. This not only simplifies maintenance but also allows for flexible configuration across your environments.
Step-by-Step Configuration:
Edit the config/horizon.php file. Update the queue configuration to utilize environment variables as shown below:
[[See Video to Reveal this Text or Code Snippet]]
Set up the .env file on each server. To assign the appropriate number of processes, modify the .env files as follows:
For the Main Server:
[[See Video to Reveal this Text or Code Snippet]]
For the Second Server:
[[See Video to Reveal this Text or Code Snippet]]
Benefits of Using Environment Variables:
Simplicity: Eliminate the need for managing multiple configuration files.
Flexibility: Quickly adjust configurations per server without code changes.
Consistency: Centralize your configuration logic, reducing the risk of errors.
Conclusion
Using environment variables in your Laravel Horizon configuration is a smart way to manage dedicated jobs across different servers. This method reduces maintenance hassle while allowing for easy adjustments as your application grows. By following the steps outlined above, you’ll set up your servers efficiently, ensuring that your job processing is both effective and organized.
With the right setup, you can optimize how your Laravel application processes jobs in a scalable manner, allowing your team to focus on what truly matters – delivering value to your users.
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: