tenancy / multi-tenant

Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously github.com/hyn/multi-tenant
https://tenancy.dev
MIT License
2.56k stars 394 forks source link

Laravel jobs in tenant database not execute by artisan command queue:work #961

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi, I am using this package for developing software.

I stored Laravel jobs in tenant database.

And then I execute php artisan queue:work from my console.

Then error message execute like below.

[2020-09-12T05:24:24.727872+00:00][ERROR]SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tenancy.queue_jobs' doesn't exist (SQL: select * from `queue_jobs` where `queue` = default and ((`reserved_at` is null and `available_at` <= 1599888264) or (`reserved_at` <= 1599888174)) order by `id` asc limit 1 for update)

I set the config/queue.php connections -> connect -> 'tenant'

Is ther any way to output jobs in tenant database? Or is queue in hyn/multi-tenant 5.5 not yet compatible with the laravel 7.20.0?

Controller

public function index() {
    $website_id = 1;
    TestMailJob::dispatch($website_id);
}

TestMailJob.php

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

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

    public $website_id;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(int $website_id)
    {
        $this->website_id = $website_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::raw('test mail',function($message){$message->to('test@example.com')->subject('test');});
    }
}

config/queue.php

    'connections' => [

        'database' => [
            'driver' => 'database',
            'table' => 'queue_jobs',
            'queue' => 'default',
            'retry_after' => 90,
            'connect' => 'tenant',
        ],

    ],

.env

QUEUE_CONNECTION=database
fletch3555 commented 4 years ago

How is Laravel supposed to know what tenant to use? The tenant connection is dynamically populated based on the identified tenant (by hostname, console argument, or manually), so simply running artisan queue:work would have no way of knowing what the tenant connection is.

Instead, we strongly recommend keeping your queued jobs table on the system database, and letting your job objects themselves track what tenant they're related to.

ghost commented 4 years ago

fletch3555, Thank you for your reply. I rethink how to deal with my queued jobs table.