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

Home Page:https://tenancy.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

nobuhiroharada opened this issue · comments

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?

  • hyn/multi-tenant version: 5.5
  • laravel version: 7.20.0
  • php version: 7.4.9

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

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.

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