-
Notifications
You must be signed in to change notification settings - Fork 34
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
Scout indexes synced with stale data during transaction #1906
Comments
This is a tough one since there's no real way or knowing when a transaction has ended within scout. So we don't know when we should persist the data except for model events. I don't see any way we could save this in scout. The workaround seems to do the trick though? Maybe not the most beautiful solution but when working with transactions it seems like the best approach to me. |
There is a chance this could be done in Scout if we were to use events with the changed data which then could be synced to algolia in that way. But that would require a major refactoring of the Scout internals. |
laravel core should implement events for transactions and then scout could listed that events and act when the transaction is rolled back. PS : I can do it with the external package like this one https://github.com/fntneves/laravel-transactional-events but I don't think that scout team will accept that PR.... |
Hello, @driesvints. This is an old issue that I've faced several times ago, not only with Nova but also with Laravel. Unfortunately, consistency between transactions and events/jobs and so on is not easy to achieve without additional logic. In the past I created a package that holds events until the transaction commits. We could discuss a bit more on how to integrate this behavior in Laravel in order to make it more robust and predictable. |
The source code is open source so anyone is free to attempt a PR. |
Hello everyone, based on an existing package and with permission of its original author, I've drafted a possible solution to tackle the "transactional events" problem. At the moment it's a draft PR in my Laravel forked repo to first gather feedback from the community before approaching to create an official PR, to give things time to unfold / develop. I invite everyone to participate in mfn/laravel-framework#1 and share your feedback. Thank you! |
I think this is resolved for real now in Laravel v8.19.0. If an app has set See laravel/framework#35422 and https://divinglaravel.com/better-management-of-database-transactions-in-laravel-8. |
As of v8.5.1 Scout now uses |
Does not seem to be using config/scout.php
|
With default Nova and Scout setups, model data is sometimes synced to the search index before it's done being saved, which results in the old data being indexed.
Description
Because Nova updates resources inside a transaction, the
saved
model event is sometimes fired before the new data has been committed and persisted to the database. Scout uses thesaved
event to trigger updating the search index, so when that update happens, if the new model data hasn't been persisted yet, the search index will be updated with the old data.See #1759, laravel/scout#152, laravel/framework#8627, laravel/framework#29710, and laravel/ideas#1441.
I would think this is more of a Scout issue, since it would make sense to me to only bother persisting data to a search index once we're absolutely certain that that data is what's saved in our records (i.e. after all transactions are committed), cc @driesvints. Scout hooking updates into the
saved
event makes that kind of impossible if you use database transactions. Settingscout.queue
tofalse
doesn't help here either.Is it really unreasonable to suggest not using a database transaction to update Nova resources?? Not sure how else we'd get around this.
Steps To Reproduce
saved
method to dump something out.Laravel\Scout\Searchable
and dump something from within thequeueMakeSearchable
method (or fromLaravel\Scout\Jobs\MakeSearchable
).Watch dumps:
queueMakeSearchable
is executed before your own model observer'ssaved
event.Ugly workaround
I don't know why this works, but if I register an observer to run only during Nova requests rather than globally, things execute in the order I want. For now, this workaround seems to be holding:
I'm turning off search syncing on that model during Nova requests, and then making the model searchable manually after the fact.
🙃
The text was updated successfully, but these errors were encountered: