From 27f916c3239fb4dbad44f1cfa74fbde4bd9d656d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 19 Feb 2022 16:12:38 +0100 Subject: [PATCH] end tenancy in queue if the next job is not tenant aware --- src/Bootstrappers/QueueTenancyBootstrapper.php | 10 ++++++++-- tests/Etc/tmp/queuetest.json | 1 - tests/QueueTest.php | 5 +++++ 3 files changed, 13 insertions(+), 3 deletions(-) delete mode 100644 tests/Etc/tmp/queuetest.json diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index d2ce52d0..2e9aa051 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -75,14 +75,20 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper protected static function initializeTenancyForQueue($tenantId) { - // The job is not tenant-aware if (! $tenantId) { + // The job is not tenant-aware + if (tenancy()->initialized) { + // Tenancy was initialized, so we revert back to the central context + tenancy()->end(); + } + return; } if (tenancy()->initialized) { + // Tenancy is already initialized if (tenant()->getTenantKey() === $tenantId) { - // Tenancy is already initialized for the tenant (e.g. dispatchNow was used) + // It's initialized for the same tenant (e.g. dispatchNow was used, or the previous job also ran for this tenant) return; } } diff --git a/tests/Etc/tmp/queuetest.json b/tests/Etc/tmp/queuetest.json deleted file mode 100644 index dc158c6b..00000000 --- a/tests/Etc/tmp/queuetest.json +++ /dev/null @@ -1 +0,0 @@ -{"userName":"Bar","shouldFail":false,"tenant_id":"The current tenant id is: a7f73c10-9879-40ae-b7b0-1ded7c1f7b1b"} \ No newline at end of file diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 158ad56b..afe64fea 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -55,6 +55,11 @@ class QueueTest extends TestCase $this->valuestore = Valuestore::make(__DIR__ . '/Etc/tmp/queuetest.json')->flush(); } + public function tearDown(): void + { + $this->valuestore->flush(); + } + protected function withFailedJobs() { Schema::connection('central')->create('failed_jobs', function (Blueprint $table) {