-
-
Notifications
You must be signed in to change notification settings - Fork 153
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
Current tenant model not refreshed in container before each job (stale model properties) #422
Comments
Hi @ionutantohi, please create a repository to test the issue. Thanks |
Can you try this out: https://github.com/ionutantohi/spatie-multitenancy-issue-422
Then in 2 terminal windows run
In the DispatchJobs command, I dispatch 2 jobs, but between them I change the tenant name to a different value. Notice that when the second job is processed, the updated name is still the old value. |
@freekmurze, one fix to this issue is to change the class MakeQueueTenantAwareAction
{
/// ...
protected function listenForJobsBeingProcessed(): static
{
app('events')->listen(JobProcessing::class, function (JobProcessing $event) {
$this->getTenantModel()::forgetCurrent();
if (array_key_exists('tenantId', $event->job->payload())) {
$this->findTenant($event)->makeCurrent();
}
});
return $this;
}
protected function listenForJobsRetryRequested(): static
{
app('events')->listen(JobRetryRequested::class, function (JobRetryRequested $event) {
$this->getTenantModel()::forgetCurrent();
if (array_key_exists('tenantId', $event->payload())) {
$this->findTenant($event)->makeCurrent();
}
});
return $this;
}
/// ...
} What do you think about? (PR: #438 - Tests are ok!) |
@ionutantohi: PR's merge is on the way to fixing the issue. I'll keep you in the loop. |
@ionutantohi issue fixed by 3.0.2. Thanks for your time on it. |
Thanks @masterix21. I confirm it works as expected on 3.0.2 |
On the second job the framework will boot again which means the container does not have anything loaded. Is my understanding wrong? |
Right, makes sense. The solution produced a breaking change. Can you check my proposed solution on the comment bellow ? |
Try again with the release 3.0.3. Thanks! |
When a tenant is made current, there is a check to see if tenant we want to make current is already loaded.
While this somehow makes sense, it might cause an issue in jobs, because jobs are processed by long running processes.
There was a back and forth around this check. Initially was added in #111 then removed in #115 and added back in 85c4e5c (cc @masterix21)
@telkins you asked in your pr #111 whether or not is a good idea. Let me give you an example.
Say we have the following tenant
id: 1
name: My tenant
stripe_plan_id: abc
Now, if we dispatch a job, for this tenant, then
Now, let's say that some info at tenant level are changed (by an user in his account etc)
stripe_plan_id: xyz
When a second job is dispatched for the same tenant
Tenant::current
inside the job, on second jobstripe_plan_id
is stillabc
.Note: The tenant is queried every time and before each job in
MakeQueueTenantAwareAction@listenForJobsBeingProcessed
but the object returned byfindTenant
(fresh from db) is never set to container because of id match.Possible workarounds?
isCurrent
check entirely, but I am not sure if that's ok.isCurrent
check to also look at the tenant/model properties and return true only if all model properties are identicalforceMakeCurrent
method which should be called fromMakeQueueTenantAwareAction
which should bypassisCurrent
checkThe text was updated successfully, but these errors were encountered: