-
Notifications
You must be signed in to change notification settings - Fork 88
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
Prevent Aggregates that use the same Models from registering multiple observers #239
Prevent Aggregates that use the same Models from registering multiple observers #239
Conversation
Sorry for the my newbie question, but, what is your concern about having multiple jobs? |
They are duplicate jobs that are performing the EXACT same task. Seems a little wasteful. |
You mean that when you save a new model that exists in two aggregators:
Correct? |
I'll try and explain visually. If we put scout-extended/src/Searchable/AggregatorObserver.php Lines 53 to 64 in b6d0094
...then trigger an update on the The first The second This PR solves this problem by figuring out which aggregators should run on which Models, and then attaches just one observer per Model which in turn prevents the duplication. Hope that makes it clearer. Happy to explain further if not. Thanks 👍 |
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.
I just have one remark that should fix the static analysis tool, other than that it looks good to me! Thank you for the contribution, this is a nice improvement!
Co-authored-by: Devin Beeuwkes <46448173+DevinCodes@users.noreply.github.com>
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.
Awesome, this looks great! Would you mind writing tests for this, please?
Ill try and look into writing tests soon, need to get my head back into it as it is a while since I have done any work around this. |
No worries, I fully understand 😄 Please let me know if I can be of any help! |
I'm really at a loss on getting any tests written for this unfortunately. If you would like to work together on moving this forward, just let me know @DevinCodes |
I'll try to schedule some time after my vacations to see if we can come up with some tests for this. |
@JayBizzle I've added some tests, they would fail if we boot the aggregators separately. WDYT? |
I'll take a look when I get back off holiday next week 👍 |
Enjoy! 🌴 |
This looks good to me 👍 Nice work on the tests, don't think I would have ever got there! |
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.
Hello @DevinCodes , LGTM
Just one question, I ran the tests and there are still remaining indices in my Algolia app afterwards, since I'm not familiar with this repo, I was just wondering if it was expected. If yes, then everything seems good :)
@damcou I don't believe there's any cleanup in place: only for the app we use in the CI, but that's not handled by Scout Extended itself. |
@DevinCodes has this been added to the scout extended documentation as well? |
It's not in the documentation, no. I completely forgot to do that, thanks for pointing it out. I've put it on my todo list and will try to add it in the coming days. |
Take the following aggregates as an example
As you can see, both these aggregators use the
Employer
Model and theOpportunity
Model (I have removed some customtoSearchableArray
code for brevity).Now, in the service provider, you would obviously boot these aggregators as follows...
Now, this seems to work as expected, but the problem is, that when we boot the aggregators, what happens is that the
Employer
Model and theOpportunity
Model each get 2 observers registered on them which results in twice as many jobs being sent to the queue when either of those Models is updated.This PR introduces a new way of registering multiple aggregators with one method call
The new
Aggregator
class exposes a newbootSearchables()
method which ensures that for all Models that are referenced across the specified aggregators, we only register one observer for each Model, and this single observer deals with triggering all the aggregators related to that specific Model.This will need tests but wanted some feedback first as the test suite looks a bit tricky to get your head into.