Skip email notifications for inactive rules

Merged
JA
2mo ago
+87-2

Closes #4196

Fork pre-flight: jaysbeekay/sure#447 (pull/447), same branch, same commit. Its CI and review history are there.

This should land before or with #4089. #4089 switches an email rule off when its baseline query times out, and without this change Apply all would still run that rule's email action and send the history the switch-off is meant to hold back.

Problem

"Apply all" (ApplyAllRulesJob, and rake rules:apply_all) passes family.rules to Rule::Runner, which executes every requested rule whatever its active?, so that its transaction changes apply. Rule::ActionExecutor::SendEmailNotification#execute never checked the rule's own active flag, so an email rule the user had switched off recorded its new matches in NotificationDelivery and enqueued RuleEmailNotificationJob. With #4089, a rule switched off after a timed-out baseline would email its whole history the same way.

Change

SendEmailNotification#execute returns 0 before NotificationDelivery.record_for when rule.active? is false. Nothing else changes: not ApplyAllRulesJob, Rule::Runner or the other executors. An inactive rule's transaction-changing actions still apply under Apply all.

The inactive run records nothing, so matches made while the rule is off stay unrecorded. Without #4089, the rule's next active run emails them, the same as switching it on and running it today. With #4089, switching it on takes a baseline first.

Blast radius

Who else hits this line, and what do they pass? execute is reached only through Rule::Action#apply (app/models/rule/action.rb:55) ← Rule#apply (rule.rb:153) ← Rule::Runner#execute (runner.rb:115). The runner is built by:

CallerRules passedEffect of the guard
ApplyAllRulesJob (apply_all_rules_job.rb:7), from RulesController#apply_allfamily.rules, inactive includedinactive email rules stop emailing (the fix)
lib/tasks/rules.rake:24 (rules:apply_all)family.rules, inactive includedsame as above
ApplyRulesJob (apply_rules_job.rb:10), sync and nightlyfamily.rules.where(active: true)none
RuleJob (rule_job.rb:9), from Rule#apply_later ← RulesController#apply (:55-56) and the assistant's apply_rule (apply_rule.rb:84,95)[rule], after both callers switch it onnone, unless the rule is switched off again before the job runs; then it sends nothing, which is intended
Rules above the requested ones (runner.rb:59)active onlynone

Sibling grep:

  • SendEmailNotification is constructed only in app/models/rule/registry/transaction_resource.rb:35.
  • NotificationDelivery.record_for is called at send_email_notification.rb:20 (guarded now) and rule/action.rb:92 (seed_notification_baseline). The baseline is left alone deliberately: it records without enqueueing, and it is what makes a re-enabled rule skip its history.
  • RuleEmailNotificationJob is enqueued only by this executor.
  • executor.execute has one caller, Rule::Action#apply.

Tests

test/jobs/apply_all_rules_job_test.rb. Each email rule has its existing match recorded, as its baseline would leave it, and then one new match:

  • does not email for an inactive rule: no RuleEmailNotificationJob, and the NotificationDelivery count for the rule is unchanged. Fails on unfixed code: 0 jobs expected, 1 enqueued.
  • applies an inactive rule's category action but does not email: the new match's category goes from nil to Groceries, and no email is sent or recorded. Fails on unfixed code: 1 job enqueued.
  • emails new matches for an active rule: one job with [rule.id, [new_match.id]], and the delivery count goes up by 1. Passes before and after.
  • does not re-send an active rule's already recorded matches: no job, and the count is unchanged. Passes before and after.

test/models/rule/action_executor/send_email_notification_test.rb:

  • an inactive rule records and enqueues nothing: no job, delivery count unchanged, returns 0. Fails on unfixed code: 1 job enqueued.
  • The existing tests used rules(:one), which is inactive by the column default (active default: false), so setup now switches it on. Without that, three existing tests fail on the guard. The watermark test's rule is created active: true for the same reason. Without that, its "sends nothing" assertion would pass because of the guard rather than the baseline.

Mutations, each run against both files:

  • Drop the guard: the three inactive tests fail (1 job enqueued).
  • Invert it (return 0 if rule.active?): 7 fail, including the active-rule job test and the three existing executor send tests.
  • Remove the setup switch-on: the three existing executor send tests fail.
  • Disable seed_notification_baseline: the watermark test fails.

Merge with the open rule PRs

Checked with git merge-tree, then with a trial merge and the tests:

  • #4089 (c9f239baf): merges cleanly, and the tests pass (546 runs, 0 failures), including notification_baseline_on_enable_test.rb.
  • #4150 (e17833f5c): merges cleanly (523 runs, 0 failures).
  • #4176 (3b4a64d4c): one textual conflict, in the watermark test in send_email_notification_test.rb. To resolve it, take #4176's version and add active: true to its rules.create!. There was also a semantic conflict, in #4176's update_rule_test.rb test "resending an unchanged email action keeps the baseline, so a pending match is still emailed": update_rule switches the rule off when its actions are replaced, and the test called Rule#apply on the inactive rule. #4176 now switches the rule on first in that test (f50d054f3), as apply_rule does in production, so only the textual conflict is left. #4176's description gives the same resolution.

Validation

  • bin/rails test on cffa5576b: 11826 runs, 54575 assertions, 0 failures, 0 errors, 34 skips.
  • bin/rubocop -f github on the changed files: no offences.
  • bin/brakeman --no-pager -q: 0 warnings.

Migration notes

None. There is no schema, data or configuration change. No UI change, so no screenshots.

Summary by CodeRabbit

  • Bug Fixes
    • Inactive rules no longer record or enqueue email notifications, while their category actions still apply.
    • Active rules send notifications for new matches without resending notifications for matches already recorded.