0

I have a multi-tenant application that is hosted on an Azure Web app instance that is load balancing. The issue I am facing is that for the 90% of jobs, the code works perfectly fine. The 10% fail for reasons that I cannot yet understand but do not fall short of the probability that scaling is the issue.

Inside of my database service, I configure the connection (done in Middleware).

config()->set('database.connections.company', [
    'driver' => 'pgsql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => $db_name,
    'username' => env('DB_USERNAME', ''),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
    'schema' => 'public',
    'sslmode' => env('DB_SSLMODE', 'prefer'),
]);

I am yet to have in issue with any requests with this issue. The issue mainly occurs when I am then dispatching jobs from the request where I recieve this issue:

SQL Exception

I've read resources on dotenv issues in PHP but I feel like this is a race condition. Currently, there is a CI/CD process that copies the .env files over to the Azure server. The startup script executes the following:

php artisan optimize:clear
php artisan cache:clear

Which stops any image related issues previously experienced with caching. It seems to me that the server the job was picked up by did not have the .env file hence why it returns the default 127.0.0.1 values.

Has anybody come across scaling issues in Azure with laravel like this? Is there an alternative way I can store this information to achieve the same solution that is not stateful? I have tried to use the Server properties in Azure configuration to store my environmental variables but this hasn't fixed this issue.

Any help appreciated.

Stack is PHP 8.0 running Laravel 8.83.23 on Azure with nginx:latest (upgraded to 8.0 as of a few months ago).

2
  • I do not think the .env file is missing, if that was the case it would have thrown an error when you return 'url' => env('DATABASE_URL'), since that should also be missing. The only thing that comes to mind is that the server does not have everything installed correctly when it comes to the DB. Have you checked if every server has the necessary handlers and packages installed? Commented Nov 21, 2022 at 14:48
  • env() returns null if the key is not found so it wouldn't error on the DATABASE_URL key, the issue is they're returning null always. I digged into the docs and found that the --env flag must be passed which I assume Azure is not doing during this scaling/load balancing @dz0nika Commented Nov 21, 2022 at 14:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.