spatie / laravel-multitenancy

Make your Laravel app usable by multiple tenants
https://spatie.be/docs/laravel-multitenancy
MIT License
1.08k stars 154 forks source link

Current tenant not determined when dispatching queued closures #467

Closed hanicab closed 11 months ago

hanicab commented 1 year ago

Hello,

Current tenant could not be determined when dispatching queued closure from tenant context, even if jobs are configured to be tenant aware by default.

$podcast = App\Podcast::find(1);

dispatch(function () use ($podcast) {
    $podcast->publish();
});

Can you please help to resolve this problem ?

How to make queued closure tenant aware ?

Best regards

benddailey commented 1 year ago

@hanicab How are you running your queue workers?

I found that a similar issue is caused because of the long running worker queue. I ended up on rather simple fix for this issue. Switch production to run worker:listen instead of worker:queue. There is a very small performance penalty for booting the framework for each job but it is better to get the right and proper result.

hanicab commented 1 year ago

@benddailey Thank you for your reply,

I use horizon,

The problem is only when dispatching queued closure from tenant context (as described)

Works perfectly :

MyCustomJob::dispatch();

Not working (current tenant not dtermined exception) :

dispatch(function(){
   Order::where('id',123)->delete();
});
masterix21 commented 1 year ago

Try again with the release 3.0.3. Thanks!

bcalik commented 1 year ago

Try again with the release 3.0.3. Thanks!

I'm on 3.0.3 and I encounter the same error.

masterix21 commented 11 months ago

@bcalik, please try adding CallQueuedClosure::class to queueable_to_job in config/multitenancy.php, so finally, you should have the config like so:

'queueable_to_job' => [
        // ...
        CallQueuedClosure::class => 'closure', // <-- ADD THIS
        // ...
    ],

Thanks

masterix21 commented 11 months ago

Solved with release 3.1.0.