DEV Community

Ariel Mejia
Ariel Mejia

Posted on

Fix commond Laravel Horizon issues

Ok you have some jobs to process in the background using Redis, the first problem that you can get is:

php_network_getaddresses: getaddrinfo for redis failed: nodename nor servname provided, or not known

We are going to tackle this in different steps, first check if you have installed Redis locally, in your terminal run:


Step 1: Install Redis

brew install redis
Enter fullscreen mode Exit fullscreen mode

Check Redis installation

php -m | grep redis
Enter fullscreen mode Exit fullscreen mode

You should be able to see that the redis module is installed

Step 2: Set Redis variables

In the .env file set or replace this values:

# Driver configuration
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis

# Redis setup
REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=6379
Enter fullscreen mode Exit fullscreen mode

If you are using Herd everything would work, if you are using Herd Pro there is a services section, ONLY if you are using that specific feature this variable should change REDIS_PORT=6138

Step 3: Install Laravel Horizon

In the terminal execute:

composer require laravel/horizon
Enter fullscreen mode Exit fullscreen mode

And this other command to publish the files:

php artisan horizon:install
Enter fullscreen mode Exit fullscreen mode

Step 4: Check the connection

In the terminal execute:

php artisan horizon
Enter fullscreen mode Exit fullscreen mode

You should be able to see the horizon response:

Horizon started successfully.

Step 5: Check Horizon Dashboard

Go to the browser and add to your project url your-project.test/horizon

Then you should be able to see the Horizon dashboard with status: active

Now, you are able to run jobs and monitor them in horizon.

Bonus

Step 6: Set Queues for different Jobs:

You can go to config/horizon.php and set specific queues and even add more supervisors, by default it checks only "defaul" queue, but as it is an array you can add multiple of them:

    'defaults' => [
        'supervisor-1' => [
            'connection' => 'redis',
            'queue' => ['default', 'sendEmailInvites'],
Enter fullscreen mode Exit fullscreen mode

Now you only need to set a specific queue for your emails with something like this:

Mail::to('user@example.com')
    ->queue((new InvitationEmail($data))->onQueue('sendEmailInvites'));
Enter fullscreen mode Exit fullscreen mode

You can even go further and set Queue Configuration (not necessary)

Make sure your config/queue.php has the queue configured:

'connections' => [
    'redis' => [
        'driver' => 'redis',
        // ... other config
        'queue' => env('REDIS_QUEUE', 'default'),
    ],
],

// You can also define named queues
'sendEmailInvites' => [
    'driver' => 'redis',
    'queue' => 'sendEmailInvites',
],
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.