-
-
Notifications
You must be signed in to change notification settings - Fork 441
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
Job not running as tenant #1277
Comments
If you don't use this |
@stancl It's also missing the tenant's detail when I don't use the wrapper and dispatch it from a tenant's tinker session. I just did some more tests with
|
Then that means your queue setup is broken overall and this is unrelated to the |
@stancl some background detail of our setup:
I do not specify a connection when dispatching the job: dispatch(new AssignCustomerProducts); If there are any details that can help find the issue let me know. |
Try getting tenant-aware queued jobs to work in a fresh app, then compare what's different in your setup. Here's a recording of me setting everything up from scratch, testing various drivers etc. Bit unstructured so feel free to skip through it https://asciinema.org/a/DLjf9W6IGChWQKctesTev7yMc |
@stancl there's a small difference in our setups. You're using This works: $tenant = Tenant::first();
$tenant->enter();
dispatch(new AssignCustomerProducts); This doesn't: $tenant = Tenant::first();
$tenant->run(fn () => dispatch(new AssignCustomerProducts)); When I use |
That observation indicates this is related to how jobs get dispatched in function my_function($tenant) {
$tenant->run(function () {
dispatch(new FooJob);
// PendingDispatch destroyed here
});
}
function my_function($tenant) {
// implicit return value
/* $_ = */ $tenant->run(fn() => dispatch(new FooJob));
// PendingDispatch destroyed here - once we're back in the central context
} This should work as well |
I'm inclined to close this since this is just how Laravel and PHP work, but the snippet you were trying to get to work here was written by me. Meaning it's easy even for me to make a mistake here and forget how dispatches work with closure return values. So I'm thinking to avoid wasting people's time we could take some steps in the package to automatically resolve this behind the scenes. E.g. |
I would have never expected this to not work with short closures. Learning new things every day.
That's probably the easiest to prevent questions down the line. I'm sorry if it felt like I'm wasting your time with this. |
No worries, again since it's code I wrote I would've expected it to work too. And I think many people who use the package would too, so to avoid wasting their time going down this rabbit hole I'll probably just add special handling to |
Bug description
Even though a job is dispatched with:
The
tenant()
inside the job is set to null.Steps to reproduce
We have the following code:
DispatchAsTenant
loops over the existing tenants and it is meant to dispatch theAssignCustomerProducts
-job for each one of them. The code ofDispatchAsTenant
is:Inside the
handle
method of theAssignCustomerProducts
-job I have:This returns:
null // app/Jobs/AssignCustomerProducts.php:29
Expected behavior
I expect
tenant()
to be the tenant that was active when dispatching the job.Laravel version
v11.29.0
stancl/tenancy version
dev-master, but not the latest version. This app is on PHP 8.2 and the beta release requires PHP 8.4. We're on the version that was available on the 20th of December.
The text was updated successfully, but these errors were encountered: