mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 00:34:04 +00:00
Revert to central context when jobs get processed, delete $previousTenant logic
This commit is contained in:
parent
d5d2c2475e
commit
42dc85666f
1 changed files with 10 additions and 23 deletions
|
|
@ -41,7 +41,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
*/
|
*/
|
||||||
public static function __constructStatic(Application $app): void
|
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)
|
public function __construct(Repository $config, QueueManager $queue)
|
||||||
|
|
@ -52,31 +52,23 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
$this->setUpPayloadGenerator();
|
$this->setUpPayloadGenerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function setUpJobListener(Dispatcher $dispatcher, bool $runningTests): void
|
protected static function setUpJobListener(Dispatcher $dispatcher): void
|
||||||
{
|
{
|
||||||
$previousTenant = null;
|
$dispatcher->listen(JobProcessing::class, function ($event) {
|
||||||
|
|
||||||
$dispatcher->listen(JobProcessing::class, function ($event) use (&$previousTenant) {
|
|
||||||
$previousTenant = tenant();
|
|
||||||
|
|
||||||
static::initializeTenancyForQueue($event->job->payload()['tenant_id'] ?? null);
|
static::initializeTenancyForQueue($event->job->payload()['tenant_id'] ?? null);
|
||||||
});
|
});
|
||||||
|
|
||||||
$dispatcher->listen(JobRetryRequested::class, function ($event) use (&$previousTenant) {
|
$dispatcher->listen(JobRetryRequested::class, function ($event) {
|
||||||
$previousTenant = tenant();
|
|
||||||
|
|
||||||
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
|
// If we're running tests, we make sure to clean up after any artisan('queue:work') calls
|
||||||
$revertToPreviousState = function ($event) use (&$previousTenant, $runningTests) {
|
$revertToCentralContext = function ($event) {
|
||||||
if ($runningTests) {
|
static::revertToCentralContext($event);
|
||||||
static::revertToPreviousState($event, $previousTenant);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds
|
$dispatcher->listen(JobProcessed::class, $revertToCentralContext); // artisan('queue:work') which succeeds
|
||||||
$dispatcher->listen(JobFailed::class, $revertToPreviousState); // artisan('queue:work') which fails
|
$dispatcher->listen(JobFailed::class, $revertToCentralContext); // artisan('queue:work') which fails
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function initializeTenancyForQueue(string|int|null $tenantId): void
|
protected static function initializeTenancyForQueue(string|int|null $tenantId): void
|
||||||
|
|
@ -120,7 +112,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function revertToPreviousState(JobProcessed|JobFailed $event, ?Tenant &$previousTenant): void
|
protected static function revertToCentralContext(JobProcessed|JobFailed $event): void
|
||||||
{
|
{
|
||||||
$tenantId = $event->job->payload()['tenant_id'] ?? null;
|
$tenantId = $event->job->payload()['tenant_id'] ?? null;
|
||||||
|
|
||||||
|
|
@ -129,13 +121,8 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revert back to the previous tenant
|
|
||||||
if (tenant() && $previousTenant && $previousTenant->isNot(tenant())) {
|
|
||||||
tenancy()->initialize($previousTenant);
|
|
||||||
}
|
|
||||||
|
|
||||||
// End tenancy
|
// End tenancy
|
||||||
if (tenant() && (! $previousTenant)) {
|
if (tenant()) {
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue