mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 20:44:03 +00:00
QueueTenancyBootstrapper
This commit is contained in:
parent
363b2fc56e
commit
8dc1ef61b8
2 changed files with 53 additions and 27 deletions
53
src/TenancyBootstrappers/QueueTenancyBootstrapper.php
Normal file
53
src/TenancyBootstrappers/QueueTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\TenancyBoostrappers;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
|
|
||||||
|
class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
{
|
||||||
|
/** @var boolean Has tenancy been started. */
|
||||||
|
protected $started = false; // todo var name?
|
||||||
|
|
||||||
|
/** @var Application */
|
||||||
|
protected $app;
|
||||||
|
|
||||||
|
public function __construct(Application $app)
|
||||||
|
{
|
||||||
|
$this->app = $app;
|
||||||
|
|
||||||
|
$this->app['queue']->createPayloadUsing([$this, 'createPayload']);
|
||||||
|
$this->app['events']->listen(\Illuminate\Queue\Events\JobProcessing::class, function ($event) {
|
||||||
|
if (\array_key_exists('tenant_id', $event->job->payload())) {
|
||||||
|
tenancy()->initById($event->job->payload()['tenant_id']); // todo
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function start()
|
||||||
|
{
|
||||||
|
$this->started = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function end()
|
||||||
|
{
|
||||||
|
$this->started = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createPayload()
|
||||||
|
{
|
||||||
|
if (! $this->started) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
[$uuid, $domain] = tenant()->get(['uuid', 'domain']);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'tenant_id' => $uuid,
|
||||||
|
'tags' => [
|
||||||
|
"tenant:$uuid",
|
||||||
|
"domain:$domain",
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -56,7 +56,6 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->registerTenantRedirectMacro();
|
$this->registerTenantRedirectMacro();
|
||||||
$this->makeQueuesTenantAware();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTelescopeTags()
|
public function setTelescopeTags()
|
||||||
|
|
@ -88,32 +87,6 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo should this be a tenancybootstrapper?
|
|
||||||
public function makeQueuesTenantAware()
|
|
||||||
{
|
|
||||||
$this->app['queue']->createPayloadUsing(function () {
|
|
||||||
if (tenancy()->initialized) {
|
|
||||||
[$uuid, $domain] = tenant()->get(['uuid', 'domain']);
|
|
||||||
|
|
||||||
return [
|
|
||||||
'tenant_uuid' => $uuid,
|
|
||||||
'tags' => [
|
|
||||||
"tenant:$uuid",
|
|
||||||
"domain:$domain",
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->app['events']->listen(\Illuminate\Queue\Events\JobProcessing::class, function ($event) {
|
|
||||||
if (\array_key_exists('tenant_uuid', $event->job->payload())) {
|
|
||||||
tenancy()->initById($event->job->payload()['tenant_uuid']);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register services.
|
* Register services.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue