tenancy icon indicating copy to clipboard operation
tenancy copied to clipboard

[4.x] Add batch tenancy queue bootstrapper

Open Riley19280 opened this issue 3 years ago • 5 comments

This PR adds a bootstrapper class for working with Laravel job batches. This fixes issues related to #547, where a Call to a member function prepare() on null exception gets thrown when the queue worker attempts to get a batch from the database for a different tenant than the previous one the queue worker processed. This is fixed by temporarily replacing the BatchRepository connection with the correct tenant connection, and then reverting it back when exiting the tenant context.

Riley19280 avatar Jun 08 '22 02:06 Riley19280

I don't think #547 is related, it's simply the same error message

stancl avatar Jun 08 '22 09:06 stancl

Yeah, that is entirely possible. I still believe there is value in supporting the batch system though.

Riley19280 avatar Jun 14 '22 01:06 Riley19280

Thank you @Riley19280, this was a lifesaver for me trying to figure out why batch jobs would fail with the Call to a member function prepare() on null exception on subsequent dispatches when the first and second dispatch are from different tenants.

For what it's worth @stancl, I added this as a bootstrapper on v3 and it's been work flawless so far.

tonning avatar Aug 19 '22 11:08 tonning

Codecov Report

Merging #874 (395d859) into master (f2c6408) will decrease coverage by 0.01%. The diff coverage is 100.00%.

@@             Coverage Diff              @@
##             master     #874      +/-   ##
============================================
- Coverage     87.34%   87.32%   -0.02%     
- Complexity      408      415       +7     
============================================
  Files           112      114       +2     
  Lines          1130     1144      +14     
============================================
+ Hits            987      999      +12     
- Misses          143      145       +2     
Impacted Files Coverage Δ
src/Bootstrappers/BatchTenancyBootstrapper.php 100.00% <100.00%> (ø)
...e/TenantDatabaseManagers/SQLiteDatabaseManager.php 76.47% <0.00%> (-8.15%) :arrow_down:
src/Listeners/DeleteTenantStorage.php 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov-commenter avatar Aug 31 '22 12:08 codecov-commenter

@abrardev99 Seems like GH was bugged again and some of my reviews didn't get submitted, they were "Pending". I submitted them now.

stancl avatar Sep 12 '22 21:09 stancl

  • Keep the property name $batchRepository
  • Since we're using dependency injection, we shouldn't be using the DB:: facade in the code. Use DI for that as well. It should be something like Laravel's DatabaseManager I think

stancl avatar Sep 22 '22 16:09 stancl

Keep the property name $batchRepository Since we're using dependency injection, we shouldn't be using the DB:: facade in the code. Use DI for that as well. It should be something like Laravel's DatabaseManager I think

Done.

abrardev99 avatar Sep 23 '22 06:09 abrardev99

Thanks for your work on this guys!

stancl avatar Sep 26 '22 12:09 stancl