Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Promote inline problem checks to problem check classes #26711

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def favicon

file&.read || ""
rescue => e
AdminDashboardData.add_problem_message("dashboard.bad_favicon_url", 1800)
ProblemCheckTracker[:bad_favicon_url].problem!
Rails.logger.debug("Failed to fetch favicon #{favicon.url}: #{e}\n#{e.backtrace}")
""
ensure
Expand Down
11 changes: 4 additions & 7 deletions app/jobs/scheduled/poll_mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def poll_pop3
if count > 3
Discourse.redis.del(POLL_MAILBOX_TIMEOUT_ERROR_KEY)
mark_as_errored!
add_admin_dashboard_problem_message("dashboard.poll_pop3_timeout")
track_problem(:poll_pop3_timeout)
Discourse.handle_job_exception(
e,
error_context(
Expand All @@ -79,7 +79,7 @@ def poll_pop3
end
rescue Net::POPAuthenticationError => e
mark_as_errored!
add_admin_dashboard_problem_message("dashboard.poll_pop3_auth_error")
track_problem(:poll_pop3_auth_error)
Discourse.handle_job_exception(e, error_context(@args, "Signing in to poll incoming emails."))
end

Expand All @@ -104,11 +104,8 @@ def mark_as_errored!
Discourse.redis.zadd(POLL_MAILBOX_ERRORS_KEY, now, now.to_s)
end

def add_admin_dashboard_problem_message(i18n_key)
AdminDashboardData.add_problem_message(
i18n_key,
SiteSetting.pop3_polling_period_mins.minutes + 5.minutes,
)
def track_problem(identifier)
ProblemCheckTracker[identifier].problem!
end
end
end
18 changes: 0 additions & 18 deletions app/models/admin_dashboard_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,6 @@ def self.load_found_scheduled_check_problems
end
end

##
# We call this method in the class definition below
# so all of the problem checks in this class are registered on
# boot. These problem checks are run when the problems are loaded in
# the admin dashboard controller.
#
# This method also can be used in testing to reset checks between
# tests. It will also fire multiple times in development mode because
# classes are not cached.
def self.reset_problem_checks
@@problem_messages = %w[
dashboard.bad_favicon_url
dashboard.poll_pop3_timeout
dashboard.poll_pop3_auth_error
]
end
reset_problem_checks

def self.fetch_stats
new.as_json
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/problem_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ProblemCheck
# Note: This list must come after the `config_accessor` declarations.
#
CORE_PROBLEM_CHECKS = [
ProblemCheck::BadFaviconUrl,
ProblemCheck::EmailPollingErroredRecently,
ProblemCheck::FacebookConfig,
ProblemCheck::FailingEmails,
Expand All @@ -45,6 +46,8 @@ class ProblemCheck
ProblemCheck::ImageMagick,
ProblemCheck::MissingMailgunApiKey,
ProblemCheck::OutOfDateThemes,
ProblemCheck::PollPop3Timeout,
ProblemCheck::PollPop3AuthError,
ProblemCheck::RailsEnv,
ProblemCheck::Ram,
ProblemCheck::S3BackupConfig,
Expand Down
5 changes: 5 additions & 0 deletions app/services/problem_check/bad_favicon_url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ProblemCheck::BadFaviconUrl < ProblemCheck::InlineProblemCheck
self.priority = "low"
end
10 changes: 10 additions & 0 deletions app/services/problem_check/inline_problem_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class ProblemCheck::InlineProblemCheck < ProblemCheck
def call
# The logic of this problem check is performed inline, so this class is
# purely here to support its configuration.
#
no_problem
end
end
5 changes: 5 additions & 0 deletions app/services/problem_check/poll_pop3_auth_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ProblemCheck::PollPop3AuthError < ProblemCheck::InlineProblemCheck
self.priority = "low"
end
5 changes: 5 additions & 0 deletions app/services/problem_check/poll_pop3_timeout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ProblemCheck::PollPop3Timeout < ProblemCheck::InlineProblemCheck
self.priority = "low"
end
8 changes: 2 additions & 6 deletions spec/jobs/poll_mailbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@

i18n_key = "dashboard.poll_pop3_auth_error"

expect(AdminDashboardData.problem_message_check(i18n_key)).to eq(
I18n.t(i18n_key, base_path: Discourse.base_path),
)
expect(ProblemCheckTracker[:poll_pop3_auth_error].blips).to eq(1)
end

it "logs an error on pop connection timeout error" do
Expand All @@ -59,9 +57,7 @@

i18n_key = "dashboard.poll_pop3_timeout"

expect(AdminDashboardData.problem_message_check(i18n_key)).to eq(
I18n.t(i18n_key, base_path: Discourse.base_path),
)
expect(ProblemCheckTracker[:poll_pop3_timeout].blips).to eq(1)
end

it "logs an error when pop fails and continues with next message" do
Expand Down
8 changes: 8 additions & 0 deletions spec/requests/static_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
expect(response.media_type).to eq("image/png")
expect(response.body.bytesize).to eq(upload.filesize)
end

context "when favicon fails to load" do
before { FileHelper.stubs(:download).raises(SocketError) }

it "creates an admin notice" do
expect { get "/favicon/proxied" }.to change { AdminNotice.problem.count }.by(1)
end
end
end
end

Expand Down