lundi 18 novembre 2019

Laravel Multi Tenancy Queue Worker - Works in Command Line But Not Artisan:call

I have a Laravel multi tenancy set up, so that each tenant has their own database with the Jobs/Failed Jobs Tables in, And a default database with the list of tenants and their database names. (when a user signs up they get their own database).

I am trying to create a way of running all the jobs in each tenant database with a queue:worker. At the moment the best thing I can think of is to create a Scheduler that will check every minute all the tenants in the default database, and then dispatch a job with that tenant database name. That job then gets picked up by another queue worker, swaps the database connection to the correct database, and then I am trying to run the artisan command:

php artisan queue:work --stop-when-empty --tries=1

When I run this command in the console, it works perfectly and works through the jobs on all the tenant databases.

However, when I fire off the following command using Artisan in the Job:

Artisan::call('queue:work', ['--stop-when-empty' => true, '--timeout' => 30, '--tries' => 1]);

the job always times out and doesn't pick up any jobs in the tenant database.

The way I am setting the database connection dynamically is as follows:

\Config::set('database.connections.tenant.database','tenant_1');
\DB::purge('tenant');
\Config::set('queue.connections.database.connection','tenant');

Is there any reason why the Artisan:call does not work, but the console command does? Or is there a better way of dynamically running jobs on all the databases other than manually running them.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire