mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 16:34:04 +00:00
Fix #1229
This commit is contained in:
parent
8f9c7efa45
commit
99eabfb28f
1 changed files with 12 additions and 33 deletions
|
|
@ -31,6 +31,8 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
* This is useful when you're changing the tenant's state (e.g. properties in the `data` column) and want the next job to initialize tenancy again
|
* This is useful when you're changing the tenant's state (e.g. properties in the `data` column) and want the next job to initialize tenancy again
|
||||||
* with the new data. Features like the Tenant Config are only executed when tenancy is initialized, so the re-initialization is needed in some cases.
|
* with the new data. Features like the Tenant Config are only executed when tenancy is initialized, so the re-initialization is needed in some cases.
|
||||||
*
|
*
|
||||||
|
* @deprecated This now has no effect, tenancy is always ended between queued jobs.
|
||||||
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
public static $forceRefresh = false;
|
public static $forceRefresh = false;
|
||||||
|
|
@ -42,7 +44,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
*/
|
*/
|
||||||
public static function __constructStatic(Application $app)
|
public static function __constructStatic(Application $app)
|
||||||
{
|
{
|
||||||
static::setUpJobListener($app->make(Dispatcher::class), $app->runningUnitTests());
|
static::setUpJobListener($app->make(Dispatcher::class));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(Repository $config, QueueManager $queue)
|
public function __construct(Repository $config, QueueManager $queue)
|
||||||
|
|
@ -53,7 +55,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
$this->setUpPayloadGenerator();
|
$this->setUpPayloadGenerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function setUpJobListener($dispatcher, $runningTests)
|
protected static function setUpJobListener($dispatcher)
|
||||||
{
|
{
|
||||||
$previousTenant = null;
|
$previousTenant = null;
|
||||||
|
|
||||||
|
|
@ -69,11 +71,8 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
static::initializeTenancyForQueue($event->payload()['tenant_id'] ?? null);
|
static::initializeTenancyForQueue($event->payload()['tenant_id'] ?? null);
|
||||||
});
|
});
|
||||||
|
|
||||||
// If we're running tests, we make sure to clean up after any artisan('queue:work') calls
|
$revertToPreviousState = function ($event) use (&$previousTenant) {
|
||||||
$revertToPreviousState = function ($event) use (&$previousTenant, $runningTests) {
|
|
||||||
if ($runningTests) {
|
|
||||||
static::revertToPreviousState($event, $previousTenant);
|
static::revertToPreviousState($event, $previousTenant);
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds
|
$dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds
|
||||||
|
|
@ -83,36 +82,14 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
protected static function initializeTenancyForQueue($tenantId)
|
protected static function initializeTenancyForQueue($tenantId)
|
||||||
{
|
{
|
||||||
if (! $tenantId) {
|
if (! $tenantId) {
|
||||||
// The job is not tenant-aware
|
// The job is not tenant-aware, so we make sure tenancy isn't initialized.
|
||||||
if (tenancy()->initialized) {
|
|
||||||
// Tenancy was initialized, so we revert back to the central context
|
|
||||||
tenancy()->end();
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (static::$forceRefresh) {
|
|
||||||
// Re-initialize tenancy between all jobs
|
|
||||||
if (tenancy()->initialized) {
|
if (tenancy()->initialized) {
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
tenancy()->initialize(tenancy()->find($tenantId));
|
|
||||||
|
|
||||||
return;
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -120,18 +97,20 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
$tenantId = $event->job->payload()['tenant_id'] ?? null;
|
$tenantId = $event->job->payload()['tenant_id'] ?? null;
|
||||||
|
|
||||||
// The job was not tenant-aware
|
|
||||||
if (! $tenantId) {
|
if (! $tenantId) {
|
||||||
|
// The job was not tenant-aware, so there's nothing to revert
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revert back to the previous tenant
|
|
||||||
if (tenant() && $previousTenant && $previousTenant->isNot(tenant())) {
|
if (tenant() && $previousTenant && $previousTenant->isNot(tenant())) {
|
||||||
|
// Revert back to the previous tenant (since Tenancy v3.8.5 this should should *likely* not happen)
|
||||||
tenancy()->initialize($previousTenant);
|
tenancy()->initialize($previousTenant);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// End tenancy
|
|
||||||
if (tenant() && (! $previousTenant)) {
|
if (tenant() && (! $previousTenant)) {
|
||||||
|
// No previous tenant = previous context was central
|
||||||
|
// NOTE: Since Tenancy v3.8.5 this should *likely* not happen
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue