Jose Jimenez
Jose Jimenez
Software Architect & Developer
> >

Troubleshooting Laravel Horizon Jobs Stuck in Pending

Published in Laravel, PHP, Laravel Horizon on Jan 20, 2024

If you are running a Laravel project with multiple Laravel Horizon instances and you find that your Horizon jobs are not executing and getting stuck in the pending state, there are a few settings you need to configure. This article will guide you through the necessary steps to resolve this issue and ensure smooth job execution.

Configuring the Settings

To fix the problem of Horizon jobs not executing and being stuck in pending, you need to configure the following settings:

HORIZON_PREFIX

This setting defines the prefix used for Horizon's internal Redis keys. By default, it is set to horizon:, but it is recommended to set a unique prefix specific to your environment. To configure this setting, replace SOMEKEY in the example below with your desired prefix:

1HORIZON_PREFIX=SOMEKEY_horizon:

Setting a unique prefix will help avoid conflicts with other Redis keys and ensure proper functioning of your Horizon instances.

CACHE_PREFIX

The CACHE_PREFIX setting determines the prefix used for Laravel's cache keys. Similar to the HORIZON_PREFIX, it is recommended to set a unique prefix for your environment. Replace SOMEKEY in the example below with your desired prefix:

1CACHE_PREFIX=SOMEKEY_cache_

Using a unique prefix for your cache keys will prevent any potential clashes with other applications or instances running on the same Redis server.

REDIS_PREFIX

If you are using Redis as your database, the REDIS_PREFIX setting defines the prefix used for Redis keys. It is crucial to set a unique prefix to avoid conflicts with other Redis keys. Replace SOMEKEY in the example below with your desired prefix:

1REDIS_PREFIX=SOMEKEY_database_

By setting a unique prefix for your Redis keys, you can ensure that your Laravel Horizon instances work seamlessly without any issues related to key conflicts.

Note on Redis Databases

If you are using Redis for your database, it is important to note that Redis supports 16 databases numbered from 0 to 15. Each database is identified by a number, starting from 0. When configuring your Laravel project to use Redis, make sure to specify a valid database number in your .env file using the REDIS_CACHE_DB setting.

For example, to use the first database (database 0), you can add the following line to your .env file:

1REDIS_CACHE_DB=0

If you need to use a different database, replace the number accordingly. Remember that each database is independent, so your Redis keys will only be accessible within the specified database.

Conclusion

By configuring the HORIZON_PREFIX, CACHE_PREFIX, and REDIS_PREFIX settings with unique prefixes specific to your environment, you can resolve the issue of Laravel Horizon jobs getting stuck in pending. These settings will ensure that your Horizon instances function correctly and that there are no conflicts with other Redis keys or cache keys.

Remember to replace SOMEKEY with your desired prefix in the examples provided. With these settings in place, your Laravel project with multiple Horizon instances will run smoothly, and your jobs will execute without any interruptions.

If you encounter any further issues or have questions, feel free to refer to the Laravel documentation or seek assistance from the Laravel community. Happy coding!