A platform for community discussion. Free, open, simple.
FIX: Fix scheduled targeted problem checks (#35696)
Ted Johanssond446cc7318b8
diff --git a/app/jobs/regular/run_problem_check.rb b/app/jobs/regular/run_problem_check.rbindex 4ddab54b093..21acbac7aee 100644--- a/app/jobs/regular/run_problem_check.rb+++ b/app/jobs/regular/run_problem_check.rb@@ -12,12 +12,15 @@ module Jobsdef execute(args)retry_count = args[:retry_count].to_iidentifier = args[:check_identifier].to_sym+ target = args[:target].to_s++ return if target.blank?check = ProblemCheck[identifier]- check.run do |problems|- raise RetrySignal if problems.present? && retry_count < check.max_retries- end+ check+ .new(target)+ .run { |problem| raise RetrySignal if problem.present? && retry_count < check.max_retries }rescue RetrySignalJobs.enqueue_in(check.retry_after,diff --git a/app/jobs/scheduled/run_problem_checks.rb b/app/jobs/scheduled/run_problem_checks.rbindex e664aae3791..96f5936e23a 100644--- a/app/jobs/scheduled/run_problem_checks.rb+++ b/app/jobs/scheduled/run_problem_checks.rb@@ -10,17 +10,19 @@ module Jobsevery 10.minutesdef execute(_args)- ProblemCheck.scheduled.filter_map do |check|- if eligible_for_this_run?(check)- Jobs.enqueue(:run_problem_check, check_identifier: check.identifier.to_s)+ ProblemCheck.scheduled.filter_map do |scheduled_check|+ scheduled_check.each_target do |target|+ check = scheduled_check.new(target)++ if check.enabled? && check.ready_to_run?+ Jobs.enqueue(+ :run_problem_check,+ check_identifier: check.identifier.to_s,+ target: target.to_s,+ )+ endendendend-- private-- def eligible_for_this_run?(check)- check.enabled? && check.scheduled? && check.ready_to_run?- endendenddiff --git a/app/models/problem_check.rb b/app/models/problem_check.rbindex 9e2f1dc2ba1..14ce8f2a4aa 100644--- a/app/models/problem_check.rb+++ b/app/models/problem_check.rb@@ -13,7 +13,7 @@ class ProblemCheckenddef run_all- select(&:enabled?).each(&:run)+ select(&:enabled?).each { |check| check.each_target { |t| check.new(t).run } }endprivate@@ -56,6 +56,11 @@ class ProblemCheck#config_accessor :inline, default: false, instance_writer: false+ # Used to set up multiple targets for the check. For example, a check that+ # operates on groups may need to specify which groups to work on.+ #+ config_accessor :targets, default: -> { [NO_TARGET] }, instance_writer: false+# Problem check classes need to be registered here in order to be enabled.## Note: This list must come after the `config_accessor` declarations.@@ -114,11 +119,6 @@ class ProblemCheckCollection.new(checks.select(&:realtime?))end- def self.tracker(target = NO_TARGET)- ProblemCheckTracker[identifier, target]- end- delegate :tracker, to: :class-def self.identifiername.demodulize.underscore.to_symend@@ -144,85 +144,77 @@ class ProblemCheckenddelegate :inline?, to: :class- def self.ready_to_run?- tracker.ready_to_run?- end- delegate :ready_to_run?, to: :class-- def self.call(data = {})- new(data).call+ def self.targeted?+ targets.call != [ProblemCheck::NO_TARGET]end+ delegate :targeted?, to: :class- def self.run(data = {}, &)- new(data).run(&)+ def self.each_target(&)+ targets.call.each(&)end- def initialize(data = {})- @data = OpenStruct.new(data)+ def initialize(target = NO_TARGET)+ @target = targetend- attr_reader :data+ attr_reader :targetdef callraise NotImplementedErrorenddef run- problems = call+ if targeted? && (target == NO_TARGET || targets.call.exclude?(target))+ tracker.destroy+ return+ end++ problem = call- yield(problems) if block_given?+ yield(problem) if block_given?next_run_at = perform_every&.from_now- if problems.empty?- targets.each { |t| tracker(t).no_problem!(next_run_at:) }+ if problem.blank?+ tracker.no_problem!(next_run_at:)else- problems- .uniq(&:target)- .each do |problem|- problem_translation_data =- problem.target.present? ? translation_data(problem.target) : translation_data-- tracker(problem.target).problem!(- next_run_at:,- details:- problem_translation_data.merge(problem.details).merge(base_path: Discourse.base_path),- )- end+ tracker.problem!(+ next_run_at:,+ details: translation_data.merge(problem.details).merge(base_path: Discourse.base_path),+ )end-- problemsend- private+ def tracker+ ProblemCheckTracker[identifier, target]+ end- def targets- [NO_TARGET]+ def ready_to_run?+ tracker.ready_to_run?end+ private+def problem(target = nil, override_key: nil, override_data: {}, details: {})target_identifier = target.kind_of?(ActiveRecord::Base) ? target.id : target- problem =- Problem.new(- I18n.t(- override_key || translation_key,- base_path: Discourse.base_path,- **override_data.merge(- target.present? ? translation_data(target) : translation_data,- ).symbolize_keys,- ),- priority: self.config.priority,- identifier:,- target: target_identifier,- details:,- )-- target.present? ? problem : [problem]+ Problem.new(+ I18n.t(+ override_key || translation_key,+ base_path: Discourse.base_path,+ **override_data.merge(+ target.present? ? translation_data(target) : translation_data,+ ).symbolize_keys,+ ),+ priority: self.config.priority,+ identifier:,+ target: target_identifier,+ details:,+ )enddef no_problem- []+ nilenddef translation_keydiff --git a/app/services/problem_check/force_https.rb b/app/services/problem_check/force_https.rbindex 19613fd4ca3..3e05f8e12e1 100644--- a/app/services/problem_check/force_https.rb+++ b/app/services/problem_check/force_https.rb@@ -5,7 +5,6 @@ class ProblemCheck::ForceHttps < ProblemCheckdef callreturn no_problem if SiteSetting.force_https- return no_problem if !data.check_force_httpsproblemenddiff --git a/app/services/problem_check/group_email_credentials.rb b/app/services/problem_check/group_email_credentials.rbindex a4986cdaa10..d54c9e65804 100644--- a/app/services/problem_check/group_email_credentials.rb+++ b/app/services/problem_check/group_email_credentials.rb@@ -9,55 +9,54 @@class ProblemCheck::GroupEmailCredentials < ProblemCheckself.priority = "high"self.perform_every = 30.minutes-- def call- [*smtp_errors, *imap_errors]- end-- private-- def targets+ self.targets = -> do[*Group.with_smtp_configured.pluck(:name), *Group.with_imap_configured.pluck(:name)]end- def translation_data(group)- { group_name: group.name, group_full_name: group.full_name }- end+ def call+ if group = Group.with_smtp_configured.find_by(name: target)+ return no_problem if !SiteSetting.enable_smtp- def smtp_errors- return [] if !SiteSetting.enable_smtp+ return(+ try_validate(group) do+ EmailSettingsValidator.validate_smtp(+ host: group.smtp_server,+ port: group.smtp_port,+ username: group.email_username,+ password: group.email_password,+ )+ end+ )+ end++ if group = Group.with_imap_configured.find_by(name: target)+ return no_problem if !SiteSetting.enable_imap- Group.with_smtp_configured.find_each.filter_map do |group|- try_validate(group) do- EmailSettingsValidator.validate_smtp(- host: group.smtp_server,- port: group.smtp_port,- username: group.email_username,- password: group.email_password,- )- end+ return(+ try_validate(group) do+ EmailSettingsValidator.validate_imap(+ host: group.imap_server,+ port: group.imap_port,+ username: group.email_username,+ password: group.email_password,+ )+ end+ )end++ no_problemend- def imap_errors- return [] if !SiteSetting.enable_imap+ private- Group.with_imap_configured.find_each.filter_map do |group|- try_validate(group) do- EmailSettingsValidator.validate_imap(- host: group.imap_server,- port: group.imap_port,- username: group.email_username,- password: group.email_password,- )- end- end+ def translation_data(group)+ { group_name: group.name, group_full_name: group.full_name }enddef try_validate(group, &blk)beginblk.call- nil+ no_problemrescue *EmailSettingsExceptionHandler::EXPECTED_EXCEPTIONS => errerror_message =EmailSettingsExceptionHandler.friendly_exception_message(err, group.smtp_server)@@ -69,7 +68,7 @@ class ProblemCheck::GroupEmailCredentials < ProblemCheckmessage:"Unexpected error when checking SMTP credentials for group #{group.id} (#{group.name}).",)- nil+ no_problemendendenddiff --git a/app/services/problem_check/upcoming_change_stable_opted_out.rb b/app/services/problem_check/upcoming_change_stable_opted_out.rbindex 809a7f0ea13..7c9a85c1b56 100644--- a/app/services/problem_check/upcoming_change_stable_opted_out.rb+++ b/app/services/problem_check/upcoming_change_stable_opted_out.rb@@ -5,7 +5,19 @@ class ProblemCheck::UpcomingChangeStableOptedOut < ProblemCheckdef callreturn no_problem if !SiteSetting.enable_upcoming_changes- status_errors++ # If the site setting is enabled, then the change is opted in, either+ # manually or automatically, so we skip it.+ return no_problem if SiteSetting.send(target)++ # Don't care about any changes that are not yet stable, admins can opt+ # in and out of these without worry.+ return no_problem if UpcomingChanges.not_yet_stable?(target)++ # At this point, we have an upcoming change that is stable or permanent,+ # and the site is opted out of it. Admins need to know that the change+ # will either become permanent or be removed soon.+ problem(target)endprivate@@ -17,23 +29,4 @@ class ProblemCheck::UpcomingChangeStableOptedOut < ProblemCheckdef targetsSiteSetting.upcoming_change_site_settingsend-- def status_errors- targets- .map do |upcoming_change|- # If the site setting is enabled, then the change is opted in, either- # manually or automatically, so we skip it.- next if SiteSetting.send(upcoming_change)-- # Don't care about any changes that are not yet stable, admins can opt- # in and out of these without worry.- next if UpcomingChanges.not_yet_stable?(upcoming_change)-- # At this point, we have an upcoming change that is stable or permanent,- # and the site is opted out of it. Admins need to know that the change- # will either become permanent or be removed soon.- problem(upcoming_change)- end- .compact- endenddiff --git a/plugins/discourse-ai/app/services/problem_check/ai_credit_hard_limit.rb b/plugins/discourse-ai/app/services/problem_check/ai_credit_hard_limit.rbindex b0221335199..f9cd6e893d3 100644--- a/plugins/discourse-ai/app/services/problem_check/ai_credit_hard_limit.rb+++ b/plugins/discourse-ai/app/services/problem_check/ai_credit_hard_limit.rb@@ -3,32 +3,26 @@class ProblemCheck::AiCreditHardLimit < ProblemCheckself.priority = "high"self.perform_every = 1.hour+ self.targets = -> do+ LlmModel.joins(:llm_credit_allocation).where("llm_models.id < 0").pluck("llm_models.id")+ enddef call- return [] if !SiteSetting.discourse_ai_enabled+ return no_problem if !SiteSetting.discourse_ai_enabled- problems = []+ model = LlmModel.where("id < 0").includes(:llm_credit_allocation).find_by(id: target)- LlmModel- .where("id < 0")- .includes(:llm_credit_allocation)- .find_each do |model|- next unless model.llm_credit_allocation+ return no_problem if model.llm_credit_allocation.blank?- allocation = model.llm_credit_allocation+ allocation = model.llm_credit_allocation- problems << hard_limit_problem(model, allocation) if allocation.hard_limit_reached?- end+ return no_problem if !allocation.hard_limit_reached?- problems.compact+ hard_limit_problem(model, allocation)endprivate- def targets- LlmModel.joins(:llm_credit_allocation).where("llm_models.id < 0").pluck("llm_models.id")- end-def hard_limit_problem(model, allocation)override_data = {model_id: model.id,diff --git a/plugins/discourse-ai/app/services/problem_check/ai_credit_soft_limit.rb b/plugins/discourse-ai/app/services/problem_check/ai_credit_soft_limit.rbindex 3b1c31fab75..fdd5a6daf4f 100644--- a/plugins/discourse-ai/app/services/problem_check/ai_credit_soft_limit.rb+++ b/plugins/discourse-ai/app/services/problem_check/ai_credit_soft_limit.rb@@ -3,34 +3,27 @@class ProblemCheck::AiCreditSoftLimit < ProblemCheckself.priority = "low"self.perform_every = 1.hour+ self.targets = -> do+ LlmModel.joins(:llm_credit_allocation).where("llm_models.id < 0").pluck("llm_models.id")+ enddef call- return [] if !SiteSetting.discourse_ai_enabled+ return no_problem if !SiteSetting.discourse_ai_enabled- problems = []+ model = LlmModel.where("id < 0").includes(:llm_credit_allocation).find_by(id: target)- LlmModel- .where("id < 0")- .includes(:llm_credit_allocation)- .find_each do |model|- next unless model.llm_credit_allocation+ return no_problem if model.llm_credit_allocation.blank?- allocation = model.llm_credit_allocation+ allocation = model.llm_credit_allocation- if allocation.soft_limit_reached? && !allocation.hard_limit_reached?- problems << soft_limit_problem(model, allocation)- end- end+ return no_problem if !allocation.soft_limit_reached?+ return no_problem if allocation.hard_limit_reached?- problems.compact+ soft_limit_problem(model, allocation)endprivate- def targets- LlmModel.joins(:llm_credit_allocation).where("llm_models.id < 0").pluck("llm_models.id")- end-def soft_limit_problem(model, allocation)override_data = {model_id: model.id,diff --git a/plugins/discourse-ai/app/services/problem_check/ai_llm_status.rb b/plugins/discourse-ai/app/services/problem_check/ai_llm_status.rbindex e1297b24571..0c2f2727448 100644--- a/plugins/discourse-ai/app/services/problem_check/ai_llm_status.rb+++ b/plugins/discourse-ai/app/services/problem_check/ai_llm_status.rb@@ -6,31 +6,25 @@ class ProblemCheck::AiLlmStatus < ProblemCheckself.max_retries = 2self.retry_after = 1.minuteself.max_blips = 2+ self.targets = -> { LlmModel.in_use.pluck(:id) }def call- return [] if !SiteSetting.discourse_ai_enabled+ return no_problem if !SiteSetting.discourse_ai_enabled- llm_errors- end+ model = LlmModel.in_use.find_by(id: target)- private+ return no_problem if model.blank?+ return no_problem if model.seeded?- def targets- LlmModel.in_use.pluck(:id)+ try_validate(model) { validator.run_test(model) }end- def llm_errors- return [] if !SiteSetting.discourse_ai_enabled- LlmModel.in_use.find_each.filter_map do |model|- next if model.seeded?- try_validate(model) { validator.run_test(model) }- end- end+ privatedef try_validate(model, &blk)beginblk.call- nil+ no_problemrescue => e# Skip problem reporting for rate limiting and temporary service issues# These are expected to resolve on their own@@ -38,7 +32,7 @@ class ProblemCheck::AiLlmStatus < ProblemCheckRails.logger.info("AI LLM Status Check: Rate limit detected for model #{model.display_name} (#{model.id}), skipping problem report",)- return nil+ return no_problemend# Log transient errors but still return a problemdiff --git a/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb b/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rbindex 6be2f628a77..bbb37cffdcb 100644--- a/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb+++ b/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb@@ -1,15 +1,17 @@# frozen_string_literal: trueRSpec.describe ProblemCheck::AiCreditHardLimit do+ subject(:check) { described_class.new(target) }+fab!(:llm_model) { Fabricate(:llm_model, id: -1) }before { SiteSetting.discourse_ai_enabled = true }describe "#call" do- it "returns no problems when no credit allocations exist" do- problems = described_class.new.call+ let(:target) { llm_model.id }- expect(problems).to be_empty+ it "returns no problems when no credit allocations exist" do+ expect(check).to be_chill_about_itendit "returns no problems when credits are not exhausted" do@@ -21,9 +23,7 @@ RSpec.describe ProblemCheck::AiCreditHardLimit dosoft_limit_percentage: 80,)- problems = described_class.new.call-- expect(problems).to be_empty+ expect(check).to be_chill_about_itendit "returns hard limit problem when credits are exhausted" do@@ -35,12 +35,7 @@ RSpec.describe ProblemCheck::AiCreditHardLimit dosoft_limit_percentage: 80,)- problems = described_class.new.call-- expect(problems.size).to eq(1)- expect(problems.first.identifier).to eq(:ai_credit_hard_limit)- expect(problems.first.priority).to eq("high")- expect(problems.first.target).to eq(llm_model.id)+ expect(check).to have_a_problem.with_priority("high").with_target(llm_model.id)endit "returns hard limit problem when credits are over-exhausted" do@@ -52,10 +47,7 @@ RSpec.describe ProblemCheck::AiCreditHardLimit dosoft_limit_percentage: 80,)- problems = described_class.new.call-- expect(problems.size).to eq(1)- expect(problems.first.identifier).to eq(:ai_credit_hard_limit)+ expect(check).to have_a_problem.with_priority("high").with_target(llm_model.id)endit "does not report problem when previous month exceeded limit but current month is new" do@@ -70,9 +62,9 @@ RSpec.describe ProblemCheck::AiCreditHardLimit do)freeze_time(Time.zone.parse("2025-11-05 10:00:00"))- problems = described_class.new.call- expect(problems).to be_empty+ expect(check).to be_chill_about_it+allocation.reloadexpect(allocation.monthly_used).to eq(0)end@@ -86,9 +78,7 @@ RSpec.describe ProblemCheck::AiCreditHardLimit domonthly_used: 1000,)- problems = described_class.new.call-- expect(problems).to be_empty+ expect(check).to be_chill_about_itendit "returns no problems when discourse_ai is disabled" do@@ -100,9 +90,7 @@ RSpec.describe ProblemCheck::AiCreditHardLimit domonthly_used: 1000,)- problems = described_class.new.call-- expect(problems).to be_empty+ expect(check).to be_chill_about_itendendenddiff --git a/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb b/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rbindex 689a45e5b70..430bde4169b 100644--- a/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb+++ b/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb@@ -1,15 +1,17 @@# frozen_string_literal: trueRSpec.describe ProblemCheck::AiCreditSoftLimit do+ subject(:check) { described_class.new(target) }+fab!(:llm_model) { Fabricate(:llm_model, id: -1) }before { SiteSetting.discourse_ai_enabled = true }describe "#call" do- it "returns no problems when no credit allocations exist" do- problems = described_class.new.call+ let(:target) { llm_model.id }- expect(problems).to be_empty+ it "returns no problems when no credit allocations exist" do+ expect(check).to be_chill_about_itendit "returns no problems when credits are not at soft limit" do@@ -21,9 +23,7 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit dosoft_limit_percentage: 80,)- problems = described_class.new.call-- expect(problems).to be_empty+ expect(check).to be_chill_about_itendit "returns soft limit problem when soft limit is reached" do@@ -35,12 +35,7 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit dosoft_limit_percentage: 80,)- problems = described_class.new.call-- expect(problems.size).to eq(1)- expect(problems.first.identifier).to eq(:ai_credit_soft_limit)- expect(problems.first.priority).to eq("low")- expect(problems.first.target).to eq(llm_model.id)+ expect(check).to have_a_problem.with_priority("low").with_target(llm_model.id)endit "does not return soft limit problem when hard limit is reached" do@@ -52,9 +47,7 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit dosoft_limit_percentage: 80,)- problems = described_class.new.call-- expect(problems).to be_empty+ expect(check).to be_chill_about_itendit "does not report problem when previous month exceeded limit but current month is new" do@@ -69,9 +62,9 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit do)freeze_time(Time.zone.parse("2025-11-05 10:00:00"))- problems = described_class.new.call- expect(problems).to be_empty+ expect(check).to be_chill_about_it+allocation.reloadexpect(allocation.monthly_used).to eq(0)end@@ -86,9 +79,7 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit dosoft_limit_percentage: 80,)- problems = described_class.new.call-- expect(problems).to be_empty+ expect(check).to be_chill_about_itendit "returns no problems when discourse_ai is disabled" do@@ -101,9 +92,7 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit dosoft_limit_percentage: 80,)- problems = described_class.new.call-- expect(problems).to be_empty+ expect(check).to be_chill_about_itendendenddiff --git a/plugins/discourse-ai/spec/services/problem_check/ai_llm_status_spec.rb b/plugins/discourse-ai/spec/services/problem_check/ai_llm_status_spec.rbindex 65f7cf5d6ce..0df1392a074 100644--- a/plugins/discourse-ai/spec/services/problem_check/ai_llm_status_spec.rb+++ b/plugins/discourse-ai/spec/services/problem_check/ai_llm_status_spec.rb@@ -1,7 +1,7 @@# frozen_string_literal: trueRSpec.describe ProblemCheck::AiLlmStatus do- subject(:check) { described_class.new }+ subject(:check) { described_class.new(target) }fab!(:llm_model)fab!(:ai_persona) { Fabricate(:ai_persona, default_llm_id: llm_model.id) }@@ -23,6 +23,8 @@ RSpec.describe ProblemCheck::AiLlmStatus do{ message: "API key error! Please check you have supplied the correct API key." }.to_jsonend+ let(:target) { llm_model.id }+before dostub_request(:post, post_url).to_return(status: 200, body: success_response, headers: {})assign_fake_provider_to(:ai_default_llm_model)@@ -50,15 +52,10 @@ RSpec.describe ProblemCheck::AiLlmStatus do},)- expect(described_class.new.call.first).to have_attributes(- identifier: :ai_llm_status,- target: llm_model.id,- priority: "high",- message: message,- details: {- error: JSON.parse(error_response)["message"],- },- )+ expect(check).to have_a_problem+ .with_priority("high")+ .with_target(llm_model.id)+ .with_message(message)endit "does not return a problem if the LLM models are working" do@@ -105,25 +102,13 @@ RSpec.describe ProblemCheck::AiLlmStatus doit "reports problem for network timeout errors" dostub_request(:post, post_url).to_timeout- problems = described_class.new.call- expect(problems.length).to eq(1)- expect(problems.first).to have_attributes(- identifier: :ai_llm_status,- target: llm_model.id,- priority: "high",- )+ expect(check).to have_a_problem.with_priority("high").with_target(llm_model.id)endit "reports problem for authentication errors" dostub_request(:post, post_url).to_return(status: 401, body: error_response, headers: {})- problems = described_class.new.call- expect(problems.length).to eq(1)- expect(problems.first).to have_attributes(- identifier: :ai_llm_status,- target: llm_model.id,- priority: "high",- )+ expect(check).to have_a_problem.with_priority("high").with_target(llm_model.id)endendenddiff --git a/spec/jobs/run_problem_check_spec.rb b/spec/jobs/run_problem_check_spec.rbindex a73feb453d9..75c760da98d 100644--- a/spec/jobs/run_problem_check_spec.rb+++ b/spec/jobs/run_problem_check_spec.rb@@ -11,14 +11,7 @@ RSpec.describe Jobs::RunProblemCheck doself.max_retries = 0def call- [- ProblemCheck::Problem.new("Big problem"),- ProblemCheck::Problem.new(- "Yuge problem",- priority: "high",- identifier: "config_is_a_mess",- ),- ]+ ProblemCheck::Problem.new("Big problem")endend@@ -29,7 +22,11 @@ RSpec.describe Jobs::RunProblemCheck doit "updates the problem check tracker" doexpect {- described_class.new.execute(check_identifier: "test_check", retry_count: 0)+ described_class.new.execute(+ check_identifier: "test_check",+ retry_count: 0,+ target: ProblemCheck::NO_TARGET,+ )}.to change { ProblemCheckTracker.failing.count }.by(1)endend@@ -42,7 +39,7 @@ RSpec.describe Jobs::RunProblemCheck doself.max_retries = 2def call- [ProblemCheck::Problem.new("Yuge problem")]+ ProblemCheck::Problem.new("Yuge problem")endend@@ -53,7 +50,11 @@ RSpec.describe Jobs::RunProblemCheck doit "does not yet update the problem check tracker" doexpect {- described_class.new.execute(check_identifier: "test_check", retry_count: 1)+ described_class.new.execute(+ check_identifier: "test_check",+ retry_count: 1,+ target: ProblemCheck::NO_TARGET,+ )}.not_to change { ProblemCheckTracker.where("blips > ?", 0).count }end@@ -63,8 +64,11 @@ RSpec.describe Jobs::RunProblemCheck doargs: {check_identifier: "test_check",retry_count: 1,+ target: ProblemCheck::NO_TARGET,},- ) { described_class.new.execute(check_identifier: "test_check") }+ ) do+ described_class.new.execute(check_identifier: "test_check", target: ProblemCheck::NO_TARGET)+ endendend@@ -76,7 +80,7 @@ RSpec.describe Jobs::RunProblemCheck doself.max_retries = 1def call- [ProblemCheck::Problem.new("Yuge problem")]+ ProblemCheck::Problem.new("Yuge problem")endend@@ -87,13 +91,21 @@ RSpec.describe Jobs::RunProblemCheck doit "updates the problem check tracker" doexpect {- described_class.new.execute(check_identifier: "test_check", retry_count: 1)+ described_class.new.execute(+ check_identifier: "test_check",+ retry_count: 1,+ target: ProblemCheck::NO_TARGET,+ )}.to change { ProblemCheckTracker.where("blips > ?", 0).count }.by(1)endit "does not schedule a retry" doexpect_not_enqueued_with(job: :run_problem_check) do- described_class.new.execute(check_identifier: "test_check", retry_count: 1)+ described_class.new.execute(+ check_identifier: "test_check",+ retry_count: 1,+ target: ProblemCheck::NO_TARGET,+ )endendenddiff --git a/spec/jobs/run_problem_checks_spec.rb b/spec/jobs/run_problem_checks_spec.rbindex 1057a545e72..9d5d11d17db 100644--- a/spec/jobs/run_problem_checks_spec.rb+++ b/spec/jobs/run_problem_checks_spec.rb@@ -19,16 +19,28 @@ RSpec.describe Jobs::RunProblemChecks dodef call = []end+ ProblemCheck::MultiTargetCheck =+ Class.new(ProblemCheck) do+ self.perform_every = 30.minutes+ self.targets = -> { %w[foo bar] }+ end+stub_const(ProblemCheck,"CORE_PROBLEM_CHECKS",- [ProblemCheck::ScheduledCheck, ProblemCheck::NonScheduledCheck, ProblemCheck::DisabledCheck],+ [+ ProblemCheck::ScheduledCheck,+ ProblemCheck::NonScheduledCheck,+ ProblemCheck::DisabledCheck,+ ProblemCheck::MultiTargetCheck,+ ],&example)ProblemCheck.send(:remove_const, "ScheduledCheck")ProblemCheck.send(:remove_const, "NonScheduledCheck")ProblemCheck.send(:remove_const, "DisabledCheck")+ ProblemCheck.send(:remove_const, "MultiTargetCheck")endcontext "when a tracker hasn't been created yet" do@@ -37,6 +49,7 @@ RSpec.describe Jobs::RunProblemChecks dojob: :run_problem_check,args: {check_identifier: "scheduled_check",+ target: ProblemCheck::NO_TARGET,},) { described_class.new.execute([]) }end@@ -52,6 +65,7 @@ RSpec.describe Jobs::RunProblemChecks dojob: :run_problem_check,args: {check_identifier: "scheduled_check",+ target: ProblemCheck::NO_TARGET,},) { described_class.new.execute([]) }end@@ -110,4 +124,42 @@ RSpec.describe Jobs::RunProblemChecks do) { described_class.new.execute([]) }endend++ context "when dealing with a multi-target check" do+ it "schedules one check per target" do+ expect_enqueued_with(+ job: :run_problem_check,+ args: {+ check_identifier: "multi_target_check",+ target: "foo",+ },+ ) { described_class.new.execute([]) }++ expect_enqueued_with(+ job: :run_problem_check,+ args: {+ check_identifier: "multi_target_check",+ target: "bar",+ },+ ) { described_class.new.execute([]) }+ end++ it "creates a problem tracker for each target" do+ expect { described_class.new.execute([]) }.to change {+ ProblemCheckTracker.where(+ identifier: "multi_target_check",+ target: ProblemCheck::MultiTargetCheck.targets.call,+ ).count+ }.by(2)+ end++ it "does not create any no-target tracker" do+ expect { described_class.new.execute([]) }.not_to change {+ ProblemCheckTracker.where(+ identifier: "multi_target_check",+ target: ProblemCheck::NO_TARGET,+ ).count+ }+ end+ endenddiff --git a/spec/services/problem_check/force_https_spec.rb b/spec/services/problem_check/force_https_spec.rbindex 47ecd7e27e7..18999a057ea 100644--- a/spec/services/problem_check/force_https_spec.rb+++ b/spec/services/problem_check/force_https_spec.rb@@ -1,14 +1,13 @@# frozen_string_literal: trueRSpec.describe ProblemCheck::ForceHttps do- subject(:check) { described_class.new(data) }+ subject(:check) { described_class.new }describe ".call" dobefore { SiteSetting.stubs(force_https: configured) }context "when configured to force SSL" dolet(:configured) { true }- let(:data) { { check_force_https: true } }it { expect(check).to be_chill_about_it }end@@ -16,20 +15,10 @@ RSpec.describe ProblemCheck::ForceHttps docontext "when not configured to force SSL" dolet(:configured) { false }- context "when the request is coming over HTTPS" do- let(:data) { { check_force_https: true } }-- it do- expect(check).to have_a_problem.with_priority("low").with_message(- "Your website is using SSL. But `<a href='/admin/site_settings/category/all_results?filter=force_https'>force_https</a>` is not yet enabled in your site settings.",- )- end- end-- context "when the request is coming over HTTP" do- let(:data) { { check_force_https: false } }-- it { expect(check).to be_chill_about_it }+ it do+ expect(check).to have_a_problem.with_priority("low").with_message(+ "Your website is using SSL. But `<a href='/admin/site_settings/category/all_results?filter=force_https'>force_https</a>` is not yet enabled in your site settings.",+ )endendenddiff --git a/spec/services/problem_check/group_email_credentials_spec.rb b/spec/services/problem_check/group_email_credentials_spec.rbindex 3078dd42ce6..5c9681d3208 100644--- a/spec/services/problem_check/group_email_credentials_spec.rb+++ b/spec/services/problem_check/group_email_credentials_spec.rb@@ -4,104 +4,96 @@ require "net/smtp"require "net/imap"RSpec.describe ProblemCheck::GroupEmailCredentials do- subject(:check) { described_class.new }+ subject(:check) { described_class.new(target) }- fab!(:group1, :group)- fab!(:group2, :smtp_group)- fab!(:group3, :imap_group)+ fab!(:group1) { Fabricate(:group, smtp_enabled: false, imap_enabled: false) }+ fab!(:smtp_group) { Fabricate(:smtp_group, name: "smtp_group", imap_enabled: true) }+ fab!(:imap_group) { Fabricate(:imap_group, name: "imap_group", smtp_enabled: false) }describe "#call" do- it "does nothing if SMTP is disabled for the site" do- expect_no_validate_any- SiteSetting.enable_smtp = false- expect(check).to be_chill_about_it+ context "when SMTP is disabled for the site" do+ before { SiteSetting.enable_smtp = false }++ context "with an SMTP-enabled group" do+ let(:target) { smtp_group.name }++ it "does not report a problem" do+ expect(check).to be_chill_about_it+ end+ end+ end++ context "when IMAP is disabled for the site" do+ before { SiteSetting.enable_imap = false }++ context "with an IMAP-enabled group" do+ let(:target) { imap_group.name }++ it "does not report a problem" do+ expect(check).to be_chill_about_it+ end+ endend- context "with smtp and imap enabled for the site" do+ context "when SMTP and IMAP are enabled for the site" dobefore doSiteSetting.enable_smtp = trueSiteSetting.enable_imap = trueend- it "does nothing if no groups have smtp enabled" do- expect_no_validate_any- group2.update!(smtp_enabled: false)- group3.update!(smtp_enabled: false, imap_enabled: false)- expect(check).to be_chill_about_it- end+ context "when the group has no SMTP or IMAP enabled" do+ let(:target) { group1.name }- it "returns a problem with the group's SMTP settings error" do- EmailSettingsValidator- .expects(:validate_smtp)- .raises(Net::SMTPAuthenticationError.new("bad credentials"))- .then- .returns(true)- .at_least_once- EmailSettingsValidator.stubs(:validate_imap).returns(true)-- expect(described_class.new.call).to contain_exactly(- have_attributes(- identifier: :group_email_credentials,- target: group2.id,- priority: "high",- message:- I18n.t(- "dashboard.problem.group_email_credentials",- base_path: Discourse.base_path,- group_name: group2.name,- group_full_name: group2.full_name,- error:- I18n.t("email_settings.smtp_authentication_error", message: "bad credentials"),- ),- ),- )+ it "does not report a problem" do+ expect(check).to be_chill_about_it+ endend- it "returns an error message and the group ID if the group's IMAP settings error" do- EmailSettingsValidator.stubs(:validate_smtp).returns(true)- EmailSettingsValidator- .expects(:validate_imap)- .raises(Net::IMAP::NoResponseError.new(stub(data: stub(text: "Invalid credentials"))))- .once-- expect(described_class.new.call).to contain_exactly(- have_attributes(- identifier: :group_email_credentials,- target: group3.id,- priority: "high",- message:- I18n.t(- "dashboard.problem.group_email_credentials",- base_path: Discourse.base_path,- group_name: group3.name,- group_full_name: group3.full_name,- error:- I18n.t("email_settings.imap_authentication_error", message: "bad credentials"),- ),- ),- )- end+ context "when SMTP error check fails" do+ let(:target) { smtp_group.name }- it "returns no imap errors if imap is disabled for the site" do- SiteSetting.enable_imap = false- EmailSettingsValidator.stubs(:validate_smtp).returns(true)- EmailSettingsValidator.expects(:validate_imap).never+ it "registers a problem with the group's SMTP settings error" do+ EmailSettingsValidator+ .expects(:validate_smtp)+ .raises(Net::SMTPAuthenticationError.new("bad credentials"))+ .then+ .returns(true)+ .at_least_once+ EmailSettingsValidator.stubs(:validate_imap).returns(true)- expect(described_class.new.call).to eq([])+ expect(check).to have_a_problem.with_priority("high").with_message(+ I18n.t(+ "dashboard.problem.group_email_credentials",+ base_path: Discourse.base_path,+ group_name: smtp_group.name,+ group_full_name: smtp_group.full_name,+ error: I18n.t("email_settings.smtp_authentication_error", message: "bad credentials"),+ ),+ )+ endend- end- end- def expect_no_validate_imap- EmailSettingsValidator.expects(:validate_imap).never- end+ context "when IMAP error check fails" do+ let(:target) { imap_group.name }- def expect_no_validate_smtp- EmailSettingsValidator.expects(:validate_smtp).never- end+ it "registers a problem with the group's IMAP settings error" do+ EmailSettingsValidator.stubs(:validate_smtp).returns(true)+ EmailSettingsValidator+ .expects(:validate_imap)+ .raises(Net::IMAP::NoResponseError.new(stub(data: stub(text: "Invalid credentials"))))+ .once- def expect_no_validate_any- expect_no_validate_smtp- expect_no_validate_imap+ expect(check).to have_a_problem.with_priority("high").with_message(+ I18n.t(+ "dashboard.problem.group_email_credentials",+ base_path: Discourse.base_path,+ group_name: imap_group.name,+ group_full_name: imap_group.full_name,+ error: I18n.t("email_settings.imap_authentication_error", message: "bad credentials"),+ ),+ )+ end+ end+ endendenddiff --git a/spec/services/problem_check/upcoming_change_stable_opted_out_spec.rb b/spec/services/problem_check/upcoming_change_stable_opted_out_spec.rbindex f30cbe4f3a7..fbd69a0d127 100644--- a/spec/services/problem_check/upcoming_change_stable_opted_out_spec.rb+++ b/spec/services/problem_check/upcoming_change_stable_opted_out_spec.rb@@ -1,9 +1,11 @@# frozen_string_literal: trueRSpec.describe ProblemCheck::UpcomingChangeStableOptedOut do- subject(:check) { described_class.new }+ subject(:check) { described_class.new(target) }describe ".call" do+ let(:target) { "enable_upload_debug_mode" }+before domock_upcoming_change_metadata({@@ -33,7 +35,11 @@ RSpec.describe ProblemCheck::UpcomingChangeStableOptedOut doendcontext "when upcoming change is stable and not opted in" do- it { expect(check).to have_a_problem }+ it do+ expect(check).to have_a_problem.with_priority("low").with_target(+ "enable_upload_debug_mode",+ )+ endendcontext "when upcoming change is not yet stable and not opted in" do@@ -65,7 +71,11 @@ RSpec.describe ProblemCheck::UpcomingChangeStableOptedOut do)end- it { expect(check).to have_a_problem }+ it do+ expect(check).to have_a_problem.with_priority("low").with_target(+ "enable_upload_debug_mode",+ )+ endendendenddiff --git a/spec/services/problem_check_spec.rb b/spec/services/problem_check_spec.rbindex 715076c5bf1..1de255afc45 100644--- a/spec/services/problem_check_spec.rb+++ b/spec/services/problem_check_spec.rb@@ -7,6 +7,7 @@ RSpec.describe ProblemCheck doInlineCheck = Class.new(described_class) { self.inline = true }PluginCheck = Class.new(described_class)DisabledCheck = Class.new(described_class) { self.enabled = false }+ MultiTargetCheck = Class.new(described_class) { self.targets = -> { %w[foo bar] } }FailingCheck =Class.new(described_class) dodef call@@ -31,7 +32,15 @@ RSpec.describe ProblemCheck dostub_const(described_class,"CORE_PROBLEM_CHECKS",- [ScheduledCheck, RealtimeCheck, InlineCheck, DisabledCheck, FailingCheck, PassingCheck],+ [+ ScheduledCheck,+ RealtimeCheck,+ InlineCheck,+ DisabledCheck,+ MultiTargetCheck,+ FailingCheck,+ PassingCheck,+ ],&example)@@ -39,6 +48,7 @@ RSpec.describe ProblemCheck doObject.send(:remove_const, RealtimeCheck.name)Object.send(:remove_const, InlineCheck.name)Object.send(:remove_const, DisabledCheck.name)+ Object.send(:remove_const, MultiTargetCheck.name)Object.send(:remove_const, PluginCheck.name)Object.send(:remove_const, FailingCheck.name)Object.send(:remove_const, PassingCheck.name)@@ -49,6 +59,7 @@ RSpec.describe ProblemCheck dolet(:inline_check) { InlineCheck }let(:enabled_check) { RealtimeCheck }let(:disabled_check) { DisabledCheck }+ let(:multi_target_check) { MultiTargetCheck }let(:plugin_check) { PluginCheck }let(:failing_check) { FailingCheck }let(:passing_check) { PassingCheck }@@ -101,6 +112,11 @@ RSpec.describe ProblemCheck doit { expect(disabled_check).not_to be_enabled }end+ describe ".targeted?" do+ it { expect(scheduled_check).not_to be_targeted }+ it { expect(multi_target_check).to be_targeted }+ end+describe "plugin problem check registration" dobefore { DiscoursePluginRegistry.register_problem_check(PluginCheck, stub(enabled?: enabled)) }@@ -121,11 +137,32 @@ RSpec.describe ProblemCheck dodescribe "#run" docontext "when check is failing" do- it { expect { failing_check.run }.to change { ProblemCheckTracker.failing.count }.by(1) }+ it { expect { failing_check.new.run }.to change { ProblemCheckTracker.failing.count }.by(1) }endcontext "when check is passing" do- it { expect { passing_check.run }.to change { ProblemCheckTracker.passing.count }.by(1) }+ it { expect { passing_check.new.run }.to change { ProblemCheckTracker.passing.count }.by(1) }+ end++ context "when targeted check has a no-target tracker" do+ before do+ ProblemCheckTracker.create!(+ identifier: "multi_target_check",+ target: ProblemCheck::NO_TARGET,+ )+ end++ it "deletes the tracker" do+ expect { multi_target_check.new.run }.to change { ProblemCheckTracker.count }.by(-1)+ end+ end++ context "when targeted check has an outdated target" do+ before { ProblemCheckTracker.create!(identifier: "multi_target_check", target: "baz") }++ it "deletes the tracker" do+ expect { multi_target_check.new("baz").run }.to change { ProblemCheckTracker.count }.by(-1)+ endendendenddiff --git a/spec/support/problem_check_matcher.rb b/spec/support/problem_check_matcher.rbindex 623bc1cb389..bc241d3f304 100644--- a/spec/support/problem_check_matcher.rb+++ b/spec/support/problem_check_matcher.rb@@ -1,7 +1,7 @@# frozen_string_literal: trueRSpec::Matchers.define :be_chill_about_it do- match { |service| expect(service.call).to be_empty }+ match { |service| expect(service.call).to be_blank }endRSpec::Matchers.define :have_a_problem do@@ -13,24 +13,29 @@ RSpec::Matchers.define :have_a_problem do@priority = priorityend+ chain :with_target do |target|+ @target = target+ end+match do |service|@result = service.callaggregate_failures do- expect(@result).to include(be_a(ProblemCheck::Problem))- expect(@result.first.priority).to(eq(@priority.to_s)) if @priority.present?- expect(@result.first.message).to(eq(@message)) if @message.present?+ expect(@result).to be_a(ProblemCheck::Problem)+ expect(@result.priority).to(eq(@priority.to_s)) if @priority.present?+ expect(@result.message).to(eq(@message)) if @message.present?+ expect(@result.target).to(eq(@target)) if @target.present?endendfailure_message do |service|- if @result.empty?+ if @result.blank?"Expected check to have a problem, but it was chill about it."- elsif [email protected]?(ProblemCheck::Problem)- "Expected result to contain only instances of `Problem`."- elsif @priority.present? && @result.first.priority != @priority- "Expected problem to have priority `#{@priority}`, but got priority `#{@result.first.priority}`."- elsif @message.present? && @result.first.message != @message+ elsif [email protected]_a?(ProblemCheck::Problem)+ "Expected result to must be an instance of `Problem`."+ elsif @priority.present? && @result.priority != @priority+ "Expected problem to have priority `#{@priority}`, but got priority `#{@result.priority}`."+ elsif @message.present? && @result.message != @message<<~MESSAGEExpected problem to have message:@@ -38,8 +43,10 @@ RSpec::Matchers.define :have_a_problem dobut got message:- > #{@result.first.message}+ > #{@result.message}MESSAGE+ elsif @target.present? && @result.target != @target+ "Expected problem to have target `#{@target}`, but got target `#{@result.target}`."endendend