While working on #2762157: [meta] Log to the logging service, when I got debug logging working on my local server I immediately started to notice that Rules was listening to and reacting on some events that were only used by disabled Rules. Although disabled Rules are, correctly, not being evaluated, the events still trigger the Rule and test whether the Rule is enabled or not.

This is not as intended - Rules should not ever be listening to events that are not used and not needed - this just generates unnecessary overhead.

Note this is currently only a problem if you are using D8 Rules Essentials, because you will need that module to enable/disable Rules. Core Rules does not yet have this feature. But it will soon when this feature is moved over from D8RE.

I found this to be a problem on my test site because I have about 50 test Rules, most of which are disabled. Many are triggered by "Drupal is initializing", which happens on every page so is useful when testing Rules. When I got logging working I noticed hundreds of logs in the debug logger and the system log for each page load even though I only had two Rules enabled.

I traced this to the function ReactionRuleStorage::getRegisteredEvents() which is used to keep track of which events to dispatch. Even though the function comment says this function "Returns a list of event names that are used by active reaction rules.", it was actually checking ALL rules, active (enabled) or disabled.

This simple patch fixes the situation so that when a Rule is disabled the Rules event listener recalculates which events to listen to. Likewise when a Rule is enabled again. To force the recalculation, after applying the patch you must add/delete/enable/or disable at least one Rule.

For those interested, a way to check which events Rules is listening to is to use the following drush command:
drush state:get --format=print-r rules.registered_events
You can use this to see the list of registered events before the patch and after the patch.

CommentFileSizeAuthor
register-events-only-for-active-rules.patch723 bytestr

Comments

TR created an issue. See original summary.

tr’s picture

Issue summary: View changes

  • TR committed db8250d on 8.x-3.x
    Issue #3103295 by TR: Rules is still listening to / reacting on events...
tr’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

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