Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue driver 'sync' generates "Call to a member function prepare() on null" #444

Closed
libero-developer opened this issue Feb 24, 2023 · 4 comments

Comments

@libero-developer
Copy link

libero-developer commented Feb 24, 2023

Laravel 10
Jetstream / Sanctum / Scout / Socialite
Laravel Multi Tenancy / Permission / Activity Log / Media library Pro / Query Builder

When calling a job with the queue_driver=sync the following error is thrown: Call to a member function prepare() on null. The PDO is null. It looks like it doesnt know which (tenant) connection to use.

When changing session driver to: file, it works. But this is not an option.

When changing the queue driver to redis, the problem is solved.

In multitenancy.php the queues_are_tenant_aware_by_default is set to true. Also defining the jobs in the tenant_aware_jobs array does not work with driver 'sync'.

Are the session_driver and the queues related? (For example, when using session_driver=file, is the tenant connection saved in this session file?).

Summary:

  • When using sync queue with session file it works. (but not an option)
  • When using sync queue with session database it fails
  • When using sync redis with session database it works.

Is this behaviour to be expected? Or is there something wrong in my application (settings).

@masterix21
Copy link
Collaborator

Take a look here: #442

Please add me to your repository: I will help you.

@moisish
Copy link
Contributor

moisish commented Mar 2, 2023

I think this is related to #446

@masterix21
Copy link
Collaborator

Try to create your own SwitchDatabaseTask like so:

class SwitchDatabaseTask implements SwitchTenantTask
{
    use UsesMultitenancyConfig;

    protected ?string $originalDefaultConnection;

    protected ?array $originalTenantDatabase;

    public function makeCurrent(Tenant $tenant): void
    {
        $this->originalDefaultConnection = config('database.default');
        $this->originalTenantDatabase = config('database.connections.tenant.database');

        $this->setup($tenant);
    }

    private function setup(Tenant $tenant)
    {
        config()->set([
            'database.default' => $this->tenantDatabaseConnectionName(),
            'database.connections.tenant.database' => $tenant->database,
        ]);

        DB::purge($this->tenantDatabaseConnectionName());
    }

    public function forgetCurrent(): void
    {
        config([
            'database.default' => $this->originalDefaultConnection,
            'database.connections.tenant.database' => $this->originalTenantDatabase,
        ]);

        DB::purge($this->tenantDatabaseConnectionName());
    }
}

Give me a feedback. Thanks

@masterix21
Copy link
Collaborator

I'm closing here because of inactivity, but please feel free to continue the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants