Giter Site home page Giter Site logo

Comments (12)

stancl avatar stancl commented on June 12, 2024 2

Then my guess is that it's this:

It might be that in the example above, the connection is removed from Laravel's DatabaseManager but is actually still kept open on MySQL.

We'll look into it 👍🏻 @lukinovec could you try to reproduce this in a testing app?

from tenancy.

stancl avatar stancl commented on June 12, 2024

Currently problem is solved using queue:listen instead but the performance is better with queue:work command

What's the difference between those two in how the DB connections work?

The problem sounds like the DB connections don't get cleaned up, but $tenant->run() should be properly cleaning up the old connection.

from tenancy.

jacksnchz avatar jacksnchz commented on June 12, 2024

Currently problem is solved using queue:listen instead but the performance is better with queue:work command

What's the difference between those two in how the DB connections work?

The problem sounds like the DB connections don't get cleaned up, but $tenant->run() should be properly cleaning up the old connection.

Hi,

Main difference between queue:listen and queue:work is that first one the framework is booted each job (closing connection also) while queue:work framework is booted only one time, so it is much more efficient.

I believe that connection need to be closed by tenant run or manually but I am stuck.

from tenancy.

stancl avatar stancl commented on June 12, 2024
> isset(app('db')->getConnections()['tenant']);
= false

> $tenant->run(fn () => app('db')->getConnections()['tenant']);
= Illuminate\Database\MySqlConnection {#3828} // tenant connection exists inside the run() Closure

> isset(app('db')->getConnections()['tenant']);
= false // cleaned up afterwards

Could it be that you're using tenants:run instead of $tenant->run()? Could you show the exact code?

It might be that in the example above, the connection is removed from Laravel's DatabaseManager but is actually still kept open on MySQL.

from tenancy.

jacksnchz avatar jacksnchz commented on June 12, 2024

Hi, I am using tenants:run and database connection is open in MySQL (listing from SHOW FULL PROCESSLIST)

Here you have an example:

Command class:

class MyCommand extends Command
{
    /**
     * {@inheritdoc}
     * @var string
     */
    protected $signature = 'myCompany:my-command';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle(): void
    {
        MyJob::dispatch();
    }
}

Job class:

class MyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        logger()->info("running");
    }
}

Steps to reproduce:

  1. php artisan queue:work --tries=3 --sleep=3 --daemon
  2. tenants:run myCompany:my-command
  3. Every time I run tenants:run, a new connection (per tenant) is open keeping previous one open forever

from tenancy.

stancl avatar stancl commented on June 12, 2024

What if you did:

Tenant::all()->runForEach(function () {
    MyJob::dispatch();
});

and called myCompany:my-command without tenants:run.

Does that also reproduce the issue?

from tenancy.

jacksnchz avatar jacksnchz commented on June 12, 2024

Hi again,

Yes, following your advice produce same result

from tenancy.

stancl avatar stancl commented on June 12, 2024

FWIW regarding the solution, I'm not sure if this will be feasible:

Keep connections open by tenant and use it instead of create new one.

We may have to close the connections completely and then reopen them as needed.

from tenancy.

jacksnchz avatar jacksnchz commented on June 12, 2024

Yes maybe keep connections open is not feasible, close connects will be a good choise.

from tenancy.

chinmaypurav avatar chinmaypurav commented on June 12, 2024

@jacksnchz Can you try usingTenant Aware Commands?

I am not entirely sure it will work, but I was facing similar problem on my staging (PHP 8).

And this helped. further testing pending

from tenancy.

jacksnchz avatar jacksnchz commented on June 12, 2024

Hi @chinmaypurav,

Thank you for your help but the behaviour using TenantAwareCommand is worst than original idea, more connections are kept open.

My temporary solution was replace queue:work by queue:listen command (loosing performance but it does not overloads the database connections).

from tenancy.

stancl avatar stancl commented on June 12, 2024

@jacksnchz This PR should fix the issue #1128

Could you try testing the change locally to see if it resolves the issue for you?

Essentially just open vendor/stancl/tenancy/src/Bootstrappers/QueueTenancyBootstrapper.php and change line ~73 like this:

- if ($runningTests) {
+ if (true) {

to test the changes from the PR in a simpler way.

from tenancy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.