Skip to content

Commit

Permalink
Close tenant connections after jobs (#1128)
Browse files Browse the repository at this point in the history
* Test that the context is central after jobs get processed

* Revert to central context when jobs get processed, delete `$previousTenant` logic

* Test that the tenant connections get closed after jobs get processed

* Bring back the previous tenant logic

* Adapt tests to the previous tenant logic

* Delete assertion
  • Loading branch information
lukinovec authored Aug 18, 2023
1 parent bd9bbe8 commit 9d6356a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/Bootstrappers/QueueTenancyBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
*/
public static function __constructStatic(Application $app): void
{
static::setUpJobListener($app->make(Dispatcher::class), $app->runningUnitTests());
static::setUpJobListener($app->make(Dispatcher::class));
}

public function __construct(Repository $config, QueueManager $queue)
Expand All @@ -52,7 +52,7 @@ public function __construct(Repository $config, QueueManager $queue)
$this->setUpPayloadGenerator();
}

protected static function setUpJobListener(Dispatcher $dispatcher, bool $runningTests): void
protected static function setUpJobListener(Dispatcher $dispatcher): void
{
$previousTenant = null;

Expand All @@ -69,10 +69,8 @@ protected static function setUpJobListener(Dispatcher $dispatcher, bool $running
});

// If we're running tests, we make sure to clean up after any artisan('queue:work') calls
$revertToPreviousState = function ($event) use (&$previousTenant, $runningTests) {
if ($runningTests) {
static::revertToPreviousState($event, $previousTenant);
}
$revertToPreviousState = function ($event) use (&$previousTenant) {
static::revertToPreviousState($event, $previousTenant);
};

$dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds
Expand Down
29 changes: 28 additions & 1 deletion tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,16 @@

pest()->artisan('queue:work --once');

expect(! tenancy()->initialized)->toBe($shouldEndTenancy);

expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0);

expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id);

$tenant->run(function () use ($user) {
expect($user->fresh()->name)->toBe('Bar');
});
})->with([true, false]);;
})->with([true, false]);

test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy) {
withFailedJobs();
Expand All @@ -144,12 +146,21 @@

pest()->artisan('queue:work --once');

expect(! tenancy()->initialized)->toBe($shouldEndTenancy);

expect(DB::connection('central')->table('failed_jobs')->count())->toBe(1);
expect(pest()->valuestore->get('tenant_id'))->toBeNull(); // job failed

pest()->artisan('queue:retry all');

if ($shouldEndTenancy) {
tenancy()->end();
}

pest()->artisan('queue:work --once');

expect(! tenancy()->initialized)->toBe($shouldEndTenancy);

expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0);

expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id); // job succeeded
Expand Down Expand Up @@ -182,6 +193,22 @@
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: acme');
});

test('tenant connections do not persist after tenant jobs get processed', function() {
withTenantDatabases();

$tenant = Tenant::create();

tenancy()->initialize($tenant);

dispatch(new TestJob(pest()->valuestore));

tenancy()->end();

pest()->artisan('queue:work --once');

expect(collect(DB::select('SHOW FULL PROCESSLIST'))->pluck('db'))->not()->toContain($tenant->database()->getName());
});

function createValueStore(): void
{
$valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json';
Expand Down

0 comments on commit 9d6356a

Please sign in to comment.