Skip to content

A platform for community discussion. Free, open, simple.

DEV: Clean up trackers with outdated targets (#35978)

Ted Johansson3e45989b364f
diff --git a/app/jobs/scheduled/cleanup_problem_check_trackers.rb b/app/jobs/scheduled/cleanup_problem_check_trackers.rb
new file mode 100644
index 00000000000..81eba6ec1cd
--- /dev/null
+++ b/app/jobs/scheduled/cleanup_problem_check_trackers.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+module 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.
+ class CleanupProblemCheckTrackers < ::Jobs::Scheduled
+ sidekiq_options retry: false
+
+ every 10.minutes
+
+ def execute(_args)
+ ProblemCheck.checks.each(&:cleanup_trackers)
+ end
+ end
+end
diff --git a/app/models/problem_check.rb b/app/models/problem_check.rb
index 4efce3e045e..0e77bdc9919 100644
--- a/app/models/problem_check.rb
+++ b/app/models/problem_check.rb
@@ -153,6 +153,10 @@ class ProblemCheck
targets.call.each(&)
end
+ def self.cleanup_trackers
+ ProblemCheckTracker.where(identifier:).where.not(target: targets.call).destroy_all
+ end
+
def initialize(target = NO_TARGET)
@target = target
end
diff --git a/spec/jobs/cleanup_problem_check_trackers_spec.rb b/spec/jobs/cleanup_problem_check_trackers_spec.rb
new file mode 100644
index 00000000000..3b438b2e2f2
--- /dev/null
+++ b/spec/jobs/cleanup_problem_check_trackers_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+RSpec.describe Jobs::CleanupProblemCheckTrackers do
+ around do |example|
+ ProblemCheck::MultiTargetCheck =
+ Class.new(ProblemCheck) do
+ self.perform_every = 30.minutes
+ self.targets = -> { %w[foo bar] }
+ end
+
+ stub_const(ProblemCheck, "CORE_PROBLEM_CHECKS", [ProblemCheck::MultiTargetCheck], &example)
+
+ ProblemCheck.send(:remove_const, "MultiTargetCheck")
+ end
+
+ context "when a tracker has an outdated target" do
+ before do
+ ProblemCheckTracker.create!(identifier: "multi_target_check", target: "foo").problem!
+ ProblemCheckTracker.create!(identifier: "multi_target_check", target: "bar").problem!
+ ProblemCheckTracker.create!(identifier: "multi_target_check", target: "baz").problem!
+ end
+
+ it "deletes trackers with non-existing targets together with any admin notices" do
+ expect { described_class.new.execute([]) }.to change {
+ ProblemCheckTracker.pluck(:target)
+ }.from(contain_exactly("foo", "bar", "baz")).to(contain_exactly("foo", "bar")).and change {
+ AdminNotice.count
+ }.by(-1)
+ end
+ end
+end
diff --git a/spec/services/problem_check_spec.rb b/spec/services/problem_check_spec.rb
index e9425c3c158..b7b57580b9d 100644
--- a/spec/services/problem_check_spec.rb
+++ b/spec/services/problem_check_spec.rb
@@ -173,4 +173,18 @@ RSpec.describe ProblemCheck do
end
end
end
+
+ describe "#cleanup_trackers" do
+ before do
+ ProblemCheckTracker.create!(identifier: "multi_target_check", target: "foo")
+ ProblemCheckTracker.create!(identifier: "multi_target_check", target: "bar")
+ ProblemCheckTracker.create!(identifier: "multi_target_check", target: "baz")
+ end
+
+ it "deletes trackers with non-existing targets" do
+ expect { multi_target_check.cleanup_trackers }.to change {
+ ProblemCheckTracker.pluck(:target)
+ }.from(contain_exactly("foo", "bar", "baz")).to(contain_exactly("foo", "bar"))
+ end
+ end
end