Cache::remember() failure in AppServiceProvider during package discovery

25 Views Asked by At

I am using a package to deal with business days in Laravel. The documentation asks to register the holidays in some global bootstrap place of the app. I have chosen the AppServiceProvider boot method to do it. I have additional dates in the database that I want to add to the holiday list.

AppServiceProvider

public function boot()
{
    $additionnalExclusions =  Cache::remember('excludedWebPaymentDates', 60 * 60, function () {
        return ExcludedWebPaymentDate::all()->mapWithKeys(function ($model) {
            return [$model->Motif => Carbon::parse($model->Date)->format('Y-m-d')];
        })->toArray();
    });

    BusinessDay::enable(['Carbon\Carbon', 'Carbon\CarbonImmutable'], 'ca-qc', $additionnalExclusions);
}

Everything works fine when testing my app in the browser. Where it stops working, is when the php artisan package:discover --ansi script is run. It runs when I run composer dump-autoload, for instance, which is called when a new package is installed or when the app is built via the docker file.

I get the following error:

Predis\Connection\ConnectionException

php_network_getaddresses: getaddrinfo for redis failed: Temporary failure in name resolution [tcp://redis:6379]

at vendor/predis/predis/src/Connection/AbstractConnection.php:155

My .env cache config goes as follow:

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

Why does it work in the browser but cause an error when executing the above command? The only thing that made it work was to remove the cache call, which is not ideal because the query would be run on every request.

0

There are 0 best solutions below