From d9fb3c1a8babf3cc4a882415f40e75d5b72f6b6c Mon Sep 17 00:00:00 2001 From: Ray Anthony Madrona Date: Thu, 11 May 2023 00:30:45 +0800 Subject: [PATCH 1/2] Bind or forget current tenant. --- src/Actions/MakeQueueTenantAwareAction.php | 25 +++++++++++++--------- src/Actions/MakeTenantCurrentAction.php | 14 +++--------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Actions/MakeQueueTenantAwareAction.php b/src/Actions/MakeQueueTenantAwareAction.php index 5a91e75..358e93b 100644 --- a/src/Actions/MakeQueueTenantAwareAction.php +++ b/src/Actions/MakeQueueTenantAwareAction.php @@ -5,6 +5,7 @@ use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Events\JobRetryRequested; use Illuminate\Support\Arr; +use Spatie\Multitenancy\Concerns\BindAsCurrentTenant; use Spatie\Multitenancy\Exceptions\CurrentTenantCouldNotBeDeterminedInTenantAwareJob; use Spatie\Multitenancy\Jobs\NotTenantAware; use Spatie\Multitenancy\Jobs\TenantAware; @@ -14,6 +15,7 @@ class MakeQueueTenantAwareAction { use UsesTenantModel; + use BindAsCurrentTenant; public function execute(): void { @@ -41,11 +43,7 @@ protected function listenForJobsBeingQueued(): static protected function listenForJobsBeingProcessed(): static { app('events')->listen(JobProcessing::class, function (JobProcessing $event) { - $this->getTenantModel()::forgetCurrent(); - - if (array_key_exists('tenantId', $event->job->payload())) { - $this->findTenant($event)->makeCurrent(); - } + $this->bindOrForgetCurrentTenant($event); }); return $this; @@ -54,11 +52,7 @@ protected function listenForJobsBeingProcessed(): static protected function listenForJobsRetryRequested(): static { app('events')->listen(JobRetryRequested::class, function (JobRetryRequested $event) { - $this->getTenantModel()::forgetCurrent(); - - if (array_key_exists('tenantId', $event->payload())) { - $this->findTenant($event)->makeCurrent(); - } + $this->bindOrForgetCurrentTenant($event); }); return $this; @@ -131,4 +125,15 @@ protected function getJobFromQueueable(object $queueable) return $queueable->$job; } + + protected function bindOrForgetCurrentTenant(JobProcessing|JobRetryRequested $event): void + { + if (array_key_exists('tenantId', $this->getEventPayload($event))) { + $this->bindAsCurrentTenant($this->findTenant($event)->makeCurrent()); + + return; + } + + $this->getTenantModel()::forgetCurrent(); + } } diff --git a/src/Actions/MakeTenantCurrentAction.php b/src/Actions/MakeTenantCurrentAction.php index 1db0ea0..25a8894 100644 --- a/src/Actions/MakeTenantCurrentAction.php +++ b/src/Actions/MakeTenantCurrentAction.php @@ -2,6 +2,7 @@ namespace Spatie\Multitenancy\Actions; +use Spatie\Multitenancy\Concerns\BindAsCurrentTenant; use Spatie\Multitenancy\Events\MadeTenantCurrentEvent; use Spatie\Multitenancy\Events\MakingTenantCurrentEvent; use Spatie\Multitenancy\Models\Tenant; @@ -10,6 +11,8 @@ class MakeTenantCurrentAction { + use BindAsCurrentTenant; + public function __construct( protected TasksCollection $tasksCollection ) { @@ -34,15 +37,4 @@ protected function performTasksToMakeTenantCurrent(Tenant $tenant): self return $this; } - - protected function bindAsCurrentTenant(Tenant $tenant): self - { - $containerKey = config('multitenancy.current_tenant_container_key'); - - app()->forgetInstance($containerKey); - - app()->instance($containerKey, $tenant); - - return $this; - } } From 292fbcc6e730680c0999610aba56b433940e824a Mon Sep 17 00:00:00 2001 From: Ray Anthony Madrona Date: Thu, 11 May 2023 17:55:41 +0800 Subject: [PATCH 2/2] Add BindAsCurrentTenant trait. --- src/Concerns/BindAsCurrentTenant.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/Concerns/BindAsCurrentTenant.php diff --git a/src/Concerns/BindAsCurrentTenant.php b/src/Concerns/BindAsCurrentTenant.php new file mode 100644 index 0000000..e12ff74 --- /dev/null +++ b/src/Concerns/BindAsCurrentTenant.php @@ -0,0 +1,19 @@ +forgetInstance($containerKey); + + app()->instance($containerKey, $tenant); + + return $this; + } +}