I've got a setup with multiple redundant databases (postgres & redis on Digital Ocean) and when there is an update or an issue Digital Ocean will switch the servers and replace the one that's broken or has an update. That works totally fine, except for the workers. I have 100+ workers and they keep getting connection errors on the "new" server. It almost looks like they are still trying to connect to the old database instance. The only way to fix this is by restarting the workers.
Is there a solution for this that doesn't need me to restart the workers manually?
And what exactly changes in the DB_* configuration from .env file?
You can create a scheduled command which will check if the correction is successful and if not it will purge DB connection and establish a new one based on some new configuration.
So you are saying that with the same credentials and same IP address and same database name it is not able to access the database which by all configuration points is identical?
// You can put this somewhere in the AppServiceProvider
\Illuminate\Support\Facades\Queue::before(function (\Illuminate\Queue\Events\JobProcessing $event) {
// check if there are connections
// then just return (defensive programming)
// if not execute something like below
/** @var \Illuminate\Database\Illuminate\Database\DatabaseManager $databaseManager */
$databaseManager->purge();
$databaseManager->reconnect();
});
Thanks! Could I also do that the other way around? So after a job is processed or failed close the connection? That way all new jobs should reconnect right?
A bit of side information, the workers run on 5-15 different servers (depending on how busy it is). Each server is responsible for their own queue(s) and there are usually more than 100K jobs queued and hundreds of jobs processing at the same time.
A bit of side information, the workers run on 5-15 different servers (depending on how busy it is). Each server is responsible for their own queue(s) and there are usually more than 100K jobs queued and hundreds of jobs processing at the same time.
They are built for that kind of load so no issues there.