Skip to content

Commit

Permalink
[2.1.0] Central queue connections (#181)
Browse files Browse the repository at this point in the history
* Central queues

* Better dependency injection

* tenancy=false => central=true; move config to env setup

* Apply fixes from StyleCI
  • Loading branch information
stancl authored Oct 17, 2019
1 parent d5b0121 commit cfcb257
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/TenancyBootstrappers/QueueTenancyBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Stancl\Tenancy\TenancyBootstrappers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Config\Repository;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Testing\Fakes\QueueFake;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Tenant;
Expand All @@ -14,19 +15,18 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
/** @var bool Has tenancy been started. */
public $started = false;

/** @var Application */
protected $app;
/** @var Repository */
protected $config;

public function __construct(Application $app)
public function __construct(Repository $config, QueueManager $queue)
{
$this->app = $app;
$this->config = $config;

$bootstrapper = &$this;

$queue = $this->app['queue'];
if (! $queue instanceof QueueFake) {
$queue->createPayloadUsing(function () use (&$bootstrapper) {
return $bootstrapper->getPayload();
$queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
return $bootstrapper->getPayload($connection);
});
}
}
Expand All @@ -41,12 +41,16 @@ public function end()
$this->started = false;
}

public function getPayload()
public function getPayload(string $connection)
{
if (! $this->started) {
return [];
}

if ($this->config["queue.connections.$connection.central"]) {
return [];
}

$id = tenant('id');

return [
Expand Down
13 changes: 13 additions & 0 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function tenancy_is_initialized_inside_queues()
return $event->job->payload()['tenant_id'] === tenant('id');
});
}

/** @test */
public function tenancy_is_not_initialized_in_non_tenant_queues()
{
$this->loadLaravelMigrations(['--database' => 'tenant']);
Event::fake();

dispatch(new TestJob())->onConnection('central');

Event::assertDispatched(JobProcessing::class, function ($event) {
return ! isset($event->job->payload()['tenant_id']);
});
}
}

class TestJob implements ShouldQueue
Expand Down
8 changes: 4 additions & 4 deletions tests/TenantStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Stancl\Tenancy\Tests;

use Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver;
use Stancl\Tenancy\StorageDrivers\Database\TenantModel;
use Stancl\Tenancy\StorageDrivers\RedisStorageDriver;
use Stancl\Tenancy\Tenant;

class TenantStorageTest extends TestCase
Expand Down Expand Up @@ -84,10 +82,12 @@ public function associative_arrays_can_be_stored()
/** @test */
public function correct_storage_driver_is_used()
{
if (config('tenancy.storage_driver') == DatabaseStorageDriver::class) {
if (config('tenancy.storage_driver') == 'db') {
$this->assertSame('DatabaseStorageDriver', class_basename(tenancy()->storage));
} elseif (config('tenancy.storage_driver') == RedisStorageDriver::class) {
} elseif (config('tenancy.storage_driver') == 'redis') {
$this->assertSame('RedisStorageDriver', class_basename(tenancy()->storage));
} else {
dd(class_basename(config('tenancy.storage_driver')));
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ protected function getEnvironmentSetUp($app)
'tenancy.migrations_directory' => database_path('../migrations'),
'tenancy.storage_drivers.db.connection' => 'central',
'tenancy.bootstrappers.redis' => \Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper::class,
'queue.connections.central' => [
'driver' => 'sync',
'central' => true,
],
]);

$app->singleton(\Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper::class);
Expand Down

0 comments on commit cfcb257

Please sign in to comment.