2024-02-22 22:20:32 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ProblemCheck
|
|
|
|
include ActiveSupport::Configurable
|
|
|
|
|
2024-02-25 23:08:12 -05:00
|
|
|
config_accessor :priority, default: "low", instance_writer: false
|
|
|
|
|
2024-02-22 22:20:32 -05:00
|
|
|
# Determines if the check should be performed at a regular interval, and if
|
|
|
|
# so how often. If left blank, the check will be performed every time the
|
|
|
|
# admin dashboard is loaded, or the data is otherwise requested.
|
|
|
|
#
|
|
|
|
config_accessor :perform_every, default: nil, instance_writer: false
|
|
|
|
|
|
|
|
# How many times the check should retry before registering a problem. Only
|
|
|
|
# works for scheduled checks.
|
|
|
|
#
|
|
|
|
config_accessor :max_retries, default: 2, instance_writer: false
|
|
|
|
|
|
|
|
# The retry delay after a failed check. Only works for scheduled checks with
|
|
|
|
# more than one retry configured.
|
|
|
|
#
|
|
|
|
config_accessor :retry_after, default: 30.seconds, instance_writer: false
|
|
|
|
|
|
|
|
def self.[](key)
|
|
|
|
key = key.to_sym
|
|
|
|
|
|
|
|
checks.find { |c| c.identifier == key }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.checks
|
|
|
|
descendants
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.scheduled
|
|
|
|
checks.select(&:scheduled?)
|
|
|
|
end
|
|
|
|
|
2024-03-13 22:55:01 -04:00
|
|
|
def self.realtime
|
|
|
|
checks.reject(&:scheduled?)
|
|
|
|
end
|
|
|
|
|
2024-02-22 22:20:32 -05:00
|
|
|
def self.identifier
|
|
|
|
name.demodulize.underscore.to_sym
|
|
|
|
end
|
|
|
|
delegate :identifier, to: :class
|
|
|
|
|
|
|
|
def self.scheduled?
|
|
|
|
perform_every.present?
|
|
|
|
end
|
|
|
|
delegate :scheduled?, to: :class
|
|
|
|
|
2024-03-13 22:55:01 -04:00
|
|
|
def self.realtime?
|
|
|
|
!scheduled?
|
|
|
|
end
|
|
|
|
delegate :realtime?, to: :class
|
|
|
|
|
|
|
|
def self.call(data = {})
|
|
|
|
new(data).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(data = {})
|
|
|
|
@data = OpenStruct.new(data)
|
2024-02-22 22:20:32 -05:00
|
|
|
end
|
|
|
|
|
2024-03-13 22:55:01 -04:00
|
|
|
attr_reader :data
|
|
|
|
|
2024-02-22 22:20:32 -05:00
|
|
|
def call
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2024-02-25 23:08:12 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-03-13 22:55:01 -04:00
|
|
|
def problem(override_key = nil)
|
2024-02-25 23:08:12 -05:00
|
|
|
[
|
|
|
|
Problem.new(
|
2024-03-13 22:55:01 -04:00
|
|
|
message ||
|
|
|
|
I18n.t(
|
|
|
|
override_key || translation_key,
|
|
|
|
base_path: Discourse.base_path,
|
|
|
|
**translation_data.symbolize_keys,
|
|
|
|
),
|
2024-02-25 23:08:12 -05:00
|
|
|
priority: self.config.priority,
|
|
|
|
identifier:,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def no_problem
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2024-03-13 22:55:01 -04:00
|
|
|
def message
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2024-02-25 23:08:12 -05:00
|
|
|
def translation_key
|
|
|
|
# TODO: Infer a default based on class name, then move translations in locale file.
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2024-03-13 22:55:01 -04:00
|
|
|
|
|
|
|
def translation_data
|
|
|
|
{}
|
|
|
|
end
|
2024-02-22 22:20:32 -05:00
|
|
|
end
|