-
Notifications
You must be signed in to change notification settings - Fork 638
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
Bulk element operation tracking + event #14032
Bulk element operation tracking + event #14032
Conversation
I had some objections, but in thinking it through, if I just listen on So I don't need to have an event before the bulk operation starts (though I suspect someone, somewhere may want an Obviously, this would only be possible when refactoring things for Craft 5, but it looks great to me, thanks! (he says, in a vacuum, having never used it in the real world) |
Yeah exactly. Keeping track of elements as things happen felt like the right approach for a couple reasons:
Yeah maybe. Added it just in case. But in general I suspect |
I assume Craft is then going to move to use this for the resave elements stuff, propagation method changes, etc., and in FeedMe, and other bulk element operations ya? |
Yes, everything in core already uses it in this PR, and we would start using it in Feed Me when porting it to Craft 5. |
I wonder if this can be leveraged by the template Anyway, nifty, well done! |
b70b60a
to
cec68f9
Compare
This looks great, will definitely be taking advantage of it in Blitz 5, thanks @brandonkelly and @khalwat for raising this! |
[ci skip]
Thanks for taking a look @khalwat and @bencroker! Merged 🚀 |
This is a great addition, something I can use in the v5 version of Scout too 👍 |
Eh, I think I misunderstood. This is only for bulk operations. i.e. This functionality does not cover normal run-of-the-mill atomic element saves/deletes. So then, is there a way for me to tell, from |
Also, curious how this would work in multi-site use cases... Currently, we can listen for By contrast, these new events don't have the actual elements attached; Instead, they carry a key for querying those elements after-the-fact. We presumably need that key to facilitate querying all the site-elements that changed during an operation. Capturing element ID alone would not be sufficient... right? |
This feels like it might get tricky. A batch operation might save elements of many types, some of which may be third-party... So, how do I know, listening to For example:
How do I know, from the event, that I need to build an |
@michaelrog Your first take was correct. As I said:
Correct, this is only concerned with which elements were updated, regardless of site. I considered tracking the site, but decided against it since most element saves will propagate across all sites anyway.
You would just need to check each individual element type you care about. Or continue listening for |
In that case, naming these as "bulk" does feel a bit too specific... It feels conceptually weird that every element save/delete action is definitionally a bulk action, whether or not multiple elements are involved. This feels more analogous to "transactions" and I wish I could think of term that felt more precise, to make documentation/teaching about this a bit less cumbersome. ...
Edit: I am a silly goose, and ...
The challenge is, I may not know ahead-of-time which element types I care about, especially if 3p plugins are involved. So then do I have to check every possible element type? I guess I could try to query the |
So if we're hung up on naming...
It probably more accurately describes what's going on this way... but honestly, the naming doesn't bother me terribly, as long as the functionality is there. |
Trust me, 90% of the time I spent on this change was trying to think of a better name for it than “bulk operation”. “Transaction” is definitely the closest thing conceptually. But I don’t want to overload that word. |
It occurs to me that deleted elements may be difficult to work with in this context: Soft-deleted elements can still be queried using e.g. If I hard-delete a bunch of entries, and now need to remove those entries from an external search index, there's no native mechanism for me to batch that, right? (I could see this being especially frustrating since Delete For Site behavior for multi-site entries is inconsistent between hard-/soft-delete.) So it seems like the best practice will still be to listen to |
@michaelrog yeah, no way to keep track of hard deletions after-the-fact, including site-specific hard-deletions, so if you care about those, you would still want to listen to |
Description
Adds the concept of bulk element operation tracking, giving plugins an opportunity to do things only after a bulk operation is fully completed, fetching all affected elements at once.
This could be useful for expensive actions like sitemap generation, static page generation, updating Algolia indexes, etc.
All built-in element (multi-)save and deletion actions will ensure that a bulk operation is active, even if only impacting a single element. So plugins could potentially stop listening to
Elements::EVENT_AFTER_SAVE_ELEMENT
/Element::EVENT_AFTER_SAVE
altogether, and could go all-in onEVENT_AFTER_BULK_OP
.Plugins that are performing bulk operations on multiple elements can define their own bulk operations like so:
There’s also a new
BaseBatchedElementJob
queue job class, which extendsBaseBatchedJob
, but ensures that all of the individual batches are wrapped in a single bulk element operation.Related issues