mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 17:24:03 +00:00
Queue tests
This commit is contained in:
parent
00bb0d06b3
commit
86a98b2bc8
13 changed files with 216 additions and 124 deletions
|
|
@ -5,28 +5,63 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\TenancyBootstrappers;
|
||||
|
||||
use Illuminate\Config\Repository;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Queue\Events\JobProcessing;
|
||||
use Illuminate\Queue\QueueManager;
|
||||
use Illuminate\Support\Testing\Fakes\QueueFake;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
// todo rewrite this
|
||||
class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/** @var bool Has tenancy been started. */
|
||||
public $started = false;
|
||||
public $tenancyInitialized = false;
|
||||
|
||||
/** @var Repository */
|
||||
protected $config;
|
||||
|
||||
public function __construct(Repository $config, QueueManager $queue)
|
||||
/** @var QueueManager */
|
||||
protected $queue;
|
||||
|
||||
/** @var Dispatcher */
|
||||
protected $event;
|
||||
|
||||
public function __construct(Repository $config, QueueManager $queue, Dispatcher $event)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->queue = $queue;
|
||||
$this->event = $event;
|
||||
|
||||
$this->setUpJobListener();
|
||||
$this->setUpPayloadGenerator();
|
||||
}
|
||||
|
||||
protected function setUpJobListener()
|
||||
{
|
||||
$this->event->listen(JobProcessing::class, function ($event) {
|
||||
$tenantId = $event->job->payload()['tenant_id'] ?? null;
|
||||
|
||||
// The job is not tenant-aware
|
||||
if (!$tenantId) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Tenancy is already initialized for the tenant (e.g. dispatchNow was used)
|
||||
if (tenancy()->initialized && tenant('id') === $tenantId) {
|
||||
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));
|
||||
});
|
||||
}
|
||||
|
||||
protected function setUpPayloadGenerator()
|
||||
{
|
||||
$bootstrapper = &$this;
|
||||
|
||||
if (! $queue instanceof QueueFake) {
|
||||
$queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
|
||||
if (! $this->queue instanceof QueueFake) {
|
||||
$this->queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
|
||||
return $bootstrapper->getPayload($connection);
|
||||
});
|
||||
}
|
||||
|
|
@ -34,17 +69,17 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
|||
|
||||
public function start(Tenant $tenant)
|
||||
{
|
||||
$this->started = true;
|
||||
$this->tenancyInitialized = true;
|
||||
}
|
||||
|
||||
public function end()
|
||||
{
|
||||
$this->started = false;
|
||||
$this->tenancyInitialized = false;
|
||||
}
|
||||
|
||||
public function getPayload(string $connection)
|
||||
{
|
||||
if (! $this->started) {
|
||||
if (! $this->tenancyInitialized) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue