Support the ongoing development of Laravel.io →
posted 2 months ago
Queues
0
moderator

The problem is that you don't switch back to the default connection on the end of your job. The result is that the next job use the connection from your tenant.

0

I don't understand, because what I do is get the current tenant in the central connection and then set the connection for this tenant, where all the processing must be done.

if (isset($event->job->payload()['tenant'])) { $company = DB::connection('mysql')->table('tenants')->where('domain', $event->job->payload()['tenant'])->first(); $manager = app(ManagerTenant::class); $manager->setConnection($company); }

Any suggestions or could you tell me how to solve this?

Thanks in advance.

0
moderator

I personal should use a second connection for the tenant models. But that result in more work (see: https://laravel.com/docs/9.x/eloquent#database-connections for the work with Eloquent)

Or you can use the JobProcessed event to roll back your database connection change.

Maybe is this enough for that:

    $this->app['events']->listen(\Illuminate\Queue\Events\JobProcessed ::class, function ($event) {
        if (isset($event->job->payload()['tenant'])) {
            Config::set('database.default', '<yourdefault>');
            // maybe disconnect your tenant database connection.
            DB::purge('tenant');
            DB::disconnect('tenant');
            Schema::connection('tenant')->getConnection()->disconnect();
        }
    });
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

Carlos Alberto cavajr Joined 26 Sep 2022

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.