A platform for community discussion. Free, open, simple.
FIX: Clear disabled/deleted ProblemChecks automatically (#42243)
David Taylor1a47bbf0af77
diff --git a/app/jobs/scheduled/cleanup_problem_check_trackers.rb b/app/jobs/scheduled/cleanup_problem_check_trackers.rbindex 81eba6ec1cd..751174eaf2f 100644--- a/app/jobs/scheduled/cleanup_problem_check_trackers.rb+++ b/app/jobs/scheduled/cleanup_problem_check_trackers.rb@@ -1,16 +1,21 @@# frozen_string_literal: truemodule Jobs- # This job looks for problem check trackers where the `target` is no- # longer in the list of targets for that problem check and destroys- # them, taking any admin notices with it.+ # Reconciles problem check trackers (and their admin notices) with the checks+ # that currently exist: drops trackers with a stale `target`, and trackers and+ # notices for checks that are disabled or no longer exist.class CleanupProblemCheckTrackers < ::Jobs::Scheduledsidekiq_options retry: falseevery 10.minutesdef execute(_args)- ProblemCheck.checks.each(&:cleanup_trackers)+ checks = ProblemCheck.checks+ checks.each(&:cleanup_trackers)++ known_identifiers = checks.map { |check| check.identifier.to_s }+ ProblemCheckTracker.where.not(identifier: known_identifiers).destroy_all+ AdminNotice.problem.where.not(identifier: known_identifiers).destroy_allendendenddiff --git a/app/models/problem_check.rb b/app/models/problem_check.rbindex 6b39535cc72..1174f9bfeb4 100644--- a/app/models/problem_check.rb+++ b/app/models/problem_check.rb@@ -160,6 +160,12 @@ class ProblemCheckenddef self.cleanup_trackers+ # A disabled check never runs, so it can't clear its own trackers/notices.+ unless enabled?+ ProblemCheckTracker.where(identifier:).destroy_all+ return+ end+current_targets = targets.callreturn if current_targets.empty?diff --git a/spec/jobs/cleanup_problem_check_trackers_spec.rb b/spec/jobs/cleanup_problem_check_trackers_spec.rbindex 3b438b2e2f2..e8e648d0889 100644--- a/spec/jobs/cleanup_problem_check_trackers_spec.rb+++ b/spec/jobs/cleanup_problem_check_trackers_spec.rb@@ -28,4 +28,26 @@ RSpec.describe Jobs::CleanupProblemCheckTrackers do}.by(-1)endend++ context "when a tracker belongs to a check that no longer exists" do+ before do+ ProblemCheckTracker.create!(identifier: "removed_check")+ AdminNotice.create!(+ identifier: "removed_check",+ subject: :problem,+ priority: :low,+ details: {+ target: ProblemCheck::NO_TARGET,+ },+ )+ end++ it "deletes the orphaned tracker together with any admin notices" do+ expect { described_class.new.execute([]) }.to change {+ ProblemCheckTracker.where(identifier: "removed_check").count+ }.from(1).to(0).and change {+ AdminNotice.problem.where(identifier: "removed_check").count+ }.from(1).to(0)+ end+ endenddiff --git a/spec/services/problem_check_spec.rb b/spec/services/problem_check_spec.rbindex dacbf779b42..8149c7372ef 100644--- a/spec/services/problem_check_spec.rb+++ b/spec/services/problem_check_spec.rb@@ -280,5 +280,15 @@ RSpec.describe ProblemCheck doexpect { multi_target_check.cleanup_trackers }.not_to change { ProblemCheckTracker.count }endend++ context "when the check is disabled" do+ before { ProblemCheckTracker.create!(identifier: "disabled_check").problem! }++ it "deletes its trackers together with any admin notices" do+ expect { disabled_check.cleanup_trackers }.to change {+ ProblemCheckTracker.where(identifier: "disabled_check").count+ }.from(1).to(0).and change { AdminNotice.count }.by(-1)+ end+ endendend