-
-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reinitialize tenancy for queued jobs if tenant id has changed #276
Conversation
Codecov Report
@@ Coverage Diff @@
## 2.x #276 +/- ##
===========================================
- Coverage 87.64% 87.45% -0.2%
- Complexity 378 381 +3
===========================================
Files 58 58
Lines 1101 1108 +7
===========================================
+ Hits 965 969 +4
- Misses 136 139 +3
Continue to review full report at Codecov.
|
@stancl Yes they are both working for me now. The fix ensures that tenancy is reinitialized if the |
@stancl @seanmtaylor well I don't think this solves it. Hence, this design does not support the custom implementation developers create. An example would be Feature Flags. If we look into others that have the issue of resetting, then this package has defined a config to able the developer to add classes implementing an Interface to be reset on each reinitialize. https://github.com/swooletw/laravel-swoole/blob/master/config/swoole_http.php#L92-L94s |
Can you elaborate on feature flags? |
@stancl it was just an example. What i'm trying to say is that whatever a developer chooses to develop on the "master side" of the SaaS needs to be reinitialized on each queue job. Typically tenant-specific config. In my platform, we have things like: CRM Oauth credentials, Email Templates, extensions enabled, extensions settings etc. All these are different from tenant to tenant and need to be reset before the next queue job gets processed. |
I don't see what you mean by this.
Why would you need to reset anything? Those things should be stored in Tenant Storage. Since the Tenant instance that is bound in the container is changed when you initialize tenancy for a different tenant, you don't have to manually reset anything. |
I see... Maybe in but fully aware of the possibilities with Tenant Storage in this package. |
Even if you configure tenant storage-to-config bindings, they are reset after tenancy is ended/reinitialized, so there shouldn't be any issues with global state. |
src/TenancyServiceProvider.php
Outdated
$tenantId !== null && | ||
(! tenancy()->initialized || tenancy('id') !== $tenantId) | ||
) { | ||
tenancy()->initById($tenantId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This basically initializes tenancy if the job payload has a tenant_id and the tenant is different from the current tenant (to avoid re-initializing tenancy in case of dispatchNow
, but to re-initialize tenancy if a new job for a different tenant should be executed), right.
I wonder if this could be refactored somehow. This takes me at least a minute to parse in my head which I don't like. But I can't think of a much better way to write this. Maybe something like this?
$tenantId = $event->job->payload()['tenant_id'] ?? null;
if (! $tenantId) { // The job is not tenant-aware
return;
}
if (tenancy()->initialized && tenant('id') === $tenantId) { // Tenancy is already initialized for the tenant (e.g. dispatchNow was used)
return;
}
// Tenancy was either not initialized, or initialized for a different tenant. Therefore, we initialize it for the correct tenant.
tenancy()->initById($tenantId);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely agree @stancl. I've refactored the logic to be much easier to understand what's going on
Hi
I have put in the fix as mentioned in #274.
Previously the code would only initialise tenancy when processing a queued job if tenancy wasn't already initialised.
This will now initialise the tenancy if the previously initialised tenant id is different to the
tenant_id
in the job payload.Fixes #274