Right now, this module uses brute force to rebuild discount rules when a discount is inserted / updated:

  entity_defaults_rebuild(array('rules_config'));

It should be possible just to rebuild the rule for the discount that was updated by duplicating the few pertinent lines in _entity_defaults_rebuild().

Comments

rszrama created an issue. See original summary.

joelpittet’s picture

Status: Active » Needs review
StatusFileSize
new5.31 KB

Would this affect this issue in any way? #2555419: Limit length of the rules names

Here's a kick start patch but I don't know where to prune.

joelpittet’s picture

Status: Needs review » Needs work

Needs Pruning

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new5.84 KB
new3.76 KB

Here's updated patch that only builds the single rule.. Instead of _commerce_discount_rebuild_rules_config() allowing null and optionally rebuilding entire rules_config, I've made it require a discount entity. The discount entity controller invokes regular rules_config rebuild.

torgospizza’s picture

Status: Needs review » Reviewed & tested by the community

This patch worked for me, saving a Discount now only took a few seconds instead of 30 :) Very happy to see this change. Thanks!

P.S. - in the early days we always had a bunch of discount offers that suddenly were unlinked from their discount entities, for reasons we could never figure out. Essentially it seemed like the Rule was created anew, with a new identifier and everything. I'm not sure if the brute-force rebuilding of Discount Rules was the cause of this (we actually haven't seen this happen in a while, anyway) but in any event this is a very welcome change, because I'm sure it will help cut down more issues like the one I described.

jkuma’s picture

Status: Reviewed & tested by the community » Needs work

Hello mglaman,

A quick observation about the last patch you have submitted.

function _commerce_discount_rebuild_rules_config(CommerceDiscount $commerce_discount_entity) 

The variable name "commerce_discount_entity" seems too long and not really conventional compared to other variable name used for a CommerceDiscount entity. Usually, we use the variable name: $discount. May you update your patch in order to keep standardized commerce_discount's code ?

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new14.23 KB
new1.04 KB

Sure, done.

mglaman’s picture

StatusFileSize
new5.79 KB

Bah I forgot to merge 7.x-1.x into my issue branch. Disregard patch from #7.

The last submitted patch, 7: rebuild_only_the-2557569-7.patch, failed testing.

jkuma’s picture

Status: Needs review » Reviewed & tested by the community

Thank you mglaman !

joelpittet’s picture

Assigned: Unassigned » rszrama

Since I worked on this patch a bit and not comfortable blind committing it(though I would) I'm assigning it to @rszrama for the honours and to check over that it's doing what his original request has intended.

Thanks @mglaman and @jkuma for finishing this up.

joelpittet’s picture

Issue tags: +Commerce Sprint

Adding to the borg.

joelpittet’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/includes/commerce_discount.controller.inc
@@ -77,7 +77,8 @@ class CommerceDiscountControllerExportable extends EntityAPIControllerExportable
     parent::delete($ids, $transaction);
-    // Rebuild entities.
+
+    // Rebuild rules config.
     entity_defaults_rebuild(array('rules_config'));

Can/Should we use our custom function here too? Also, should we avoid the rebuild if the transaction fails?

mglaman’s picture

I left it to rebuild all as multiple could be deleted. The helper function doesn't support multiple to reduce complexity, as insert and update only affect a single instance. Delete is multiple. Figure we could open an enhancement ticket to work around delete. I felt it was out of scope of the original ticket.

smccabe’s picture

Status: Needs work » Reviewed & tested by the community

@mglaman: I don't know if the multiple part is the problem, as you could just do a little foreach on the $ids before you passed them into the helper function. The problem is the helper won't support deletes at all as far as I know. It always assumes a create or update and then saves. The helper function would have to be significantly modified or a second delete one created if I understand things correctly. I'd agree with mglaman that it is probably best for a follow-up issue.

joelpittet’s picture

Issue summary: View changes
Issue tags: +Performance
StatusFileSize
new221.87 KB

Here's the /admin/modules page before and after this patch!

joelpittet’s picture

It shaved off 3 seconds on my admin/modules page! My only concern is this logic looks like it should belong to rules. Maybe we can open up a rules issue to see if we can't get this in there?

fago’s picture

Right now, this module uses brute force to rebuild discount rules when a discount is inserted / updated:

hm, I'm missing why it is doing that in the first place. Rebuilding is the process of applying defaults exported to code - how does that relate to the creation of further discount rules?

mglaman’s picture

fago, discounts generate rules. So the default configuration needs to be re-imported for that rule to reflect changes.

joelpittet’s picture

Fago's done some work in rules that has some benefits to this issue it seems. There is a patch over in #2189645: Avoid full cache clear whenever a rules component or reaction rule is edited that would be good to review.

joelpittet’s picture

StatusFileSize
new8.87 KB
new5.76 KB

Just a small refactor, shouldn't change code. I'll commit this after it passes.

  • joelpittet committed 56f3e0a on 7.x-1.x authored by mglaman
    Issue #2557569 by mglaman, joelpittet, jkuma, rszrama, torgosPizza,...
joelpittet’s picture

Assigned: rszrama » Unassigned
Status: Reviewed & tested by the community » Fixed

Thanks, I've committed this to -dev and if something comes of change in rules or entity to help do this without duplicating the code then I'm all for ripping this out later.

torgospizza’s picture

Even though I didn't write any code for this issue I'm thankful for the commit credit ;) Glad to see this in! It's made a positive impact on our discount creation workflow.

joelpittet’s picture

It's an appreciation of the company;)

rszrama’s picture

w00t w00t!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

jsacksick’s picture

Created a follow-up issue #2919251: Improve the rebuilding of discount rules instead of reopening this one which is old.