1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 05:14:03 +00:00

simplify persistent queue bootstrapper

This commit is contained in:
Samuel Štancl 2025-01-09 18:02:44 +01:00
parent 8f4cd186f9
commit e6f564c47a
2 changed files with 13 additions and 35 deletions

View file

@ -63,6 +63,10 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper
if ($runningTests) { if ($runningTests) {
static::revertToPreviousState($event, $previousTenant); static::revertToPreviousState($event, $previousTenant);
} }
// If we're not running tests, we remain in the tenant's context. This makes other JobProcessed
// listeners able to deserialize the job, including with SerializesModels, since the tenant connection
// remains open.
}; };
$dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds $dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds
@ -81,27 +85,10 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper
return; return;
} }
if (true) { // todo0 refactor // Re-initialize tenancy between all jobs even if the tenant is the same
// Re-initialize tenancy between all jobs // so that we don't work with an outdated tenant() instance in case it
if (tenancy()->initialized) { // was updated outside the queue worker.
tenancy()->end(); tenancy()->end();
}
tenancy()->initialize(tenancy()->find($tenantId));
return;
}
if (tenancy()->initialized) {
// Tenancy is already initialized
if (tenant()->getTenantKey() === $tenantId) {
// It's initialized for the same tenant (e.g. dispatchNow was used, or the previous job also ran for this tenant)
return;
}
}
// Tenancy was either not initialized, or initialized for a different tenant.
// Therefore, we initialize it for the correct tenant.
tenancy()->initialize(tenancy()->find($tenantId)); tenancy()->initialize(tenancy()->find($tenantId));
} }
@ -136,16 +123,6 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper
} }
} }
public function bootstrap(Tenant $tenant): void
{
//
}
public function revert(): void
{
//
}
public function getPayload(string $connection) public function getPayload(string $connection)
{ {
if (! tenancy()->initialized) { if (! tenancy()->initialized) {
@ -156,10 +133,11 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper
return []; return [];
} }
$id = tenant()->getTenantKey();
return [ return [
'tenant_id' => $id, 'tenant_id' => tenant()->getTenantKey(),
]; ];
} }
public function bootstrap(Tenant $tenant): void {}
public function revert(): void {}
} }

View file

@ -225,7 +225,7 @@ test('the tenant used by the job doesnt change when the current tenant changes',
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant1->getTenantKey()); expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant1->getTenantKey());
})->with('queue_bootstrappers'); })->with('queue_bootstrappers');
// todo0 confirm if this should be passing with the persistent bootstrapper // todo0 false positive -- remove after reproducing original issue
test('tenant connections do not persist after tenant jobs get processed', function (string $bootstrapper) { test('tenant connections do not persist after tenant jobs get processed', function (string $bootstrapper) {
withQueueBootstrapper($bootstrapper); withQueueBootstrapper($bootstrapper);
withTenantDatabases(); withTenantDatabases();