2017-09-29 01:16:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DeliveryFailureTracker
|
2022-04-28 15:47:34 +00:00
|
|
|
include Redisable
|
|
|
|
|
2017-09-29 01:16:20 +00:00
|
|
|
FAILURE_DAYS_THRESHOLD = 7
|
|
|
|
|
2020-04-15 18:33:24 +00:00
|
|
|
def initialize(url_or_host)
|
2023-02-08 01:06:48 +00:00
|
|
|
@host = url_or_host.start_with?('https://', 'http://') ? Addressable::URI.parse(url_or_host).normalized_host : url_or_host
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def track_failure!
|
2022-04-28 15:47:34 +00:00
|
|
|
redis.sadd(exhausted_deliveries_key, today)
|
2020-04-15 18:33:24 +00:00
|
|
|
UnavailableDomain.create(domain: @host) if reached_failure_threshold?
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def track_success!
|
2022-04-28 15:47:34 +00:00
|
|
|
redis.del(exhausted_deliveries_key)
|
2020-04-15 18:33:24 +00:00
|
|
|
UnavailableDomain.find_by(domain: @host)&.destroy
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
|
|
|
|
2021-05-05 21:39:02 +00:00
|
|
|
def clear_failures!
|
2022-04-28 15:47:34 +00:00
|
|
|
redis.del(exhausted_deliveries_key)
|
2021-05-05 21:39:02 +00:00
|
|
|
end
|
|
|
|
|
2017-09-29 01:16:20 +00:00
|
|
|
def days
|
2022-04-28 15:47:34 +00:00
|
|
|
redis.scard(exhausted_deliveries_key) || 0
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
|
|
|
|
2020-04-15 18:33:24 +00:00
|
|
|
def available?
|
2024-01-24 11:51:09 +00:00
|
|
|
!UnavailableDomain.exists?(domain: @host)
|
2020-04-15 18:33:24 +00:00
|
|
|
end
|
|
|
|
|
2021-05-05 21:39:02 +00:00
|
|
|
def exhausted_deliveries_days
|
2022-04-28 15:47:34 +00:00
|
|
|
@exhausted_deliveries_days ||= redis.smembers(exhausted_deliveries_key).sort.map { |date| Date.new(date.slice(0, 4).to_i, date.slice(4, 2).to_i, date.slice(6, 2).to_i) }
|
2021-05-05 21:39:02 +00:00
|
|
|
end
|
|
|
|
|
2020-04-15 18:33:24 +00:00
|
|
|
alias reset! track_success!
|
|
|
|
|
2017-09-29 01:16:20 +00:00
|
|
|
class << self
|
2022-04-28 15:47:34 +00:00
|
|
|
include Redisable
|
|
|
|
|
2020-04-15 18:33:24 +00:00
|
|
|
def without_unavailable(urls)
|
2021-03-24 09:44:31 +00:00
|
|
|
unavailable_domains_map = Rails.cache.fetch('unavailable_domains') { UnavailableDomain.pluck(:domain).index_with(true) }
|
2017-09-29 01:16:20 +00:00
|
|
|
|
2020-04-15 18:33:24 +00:00
|
|
|
urls.reject do |url|
|
|
|
|
host = Addressable::URI.parse(url).normalized_host
|
|
|
|
unavailable_domains_map[host]
|
|
|
|
end
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def available?(url)
|
2020-04-15 18:33:24 +00:00
|
|
|
new(url).available?
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
|
|
|
|
2020-04-15 18:33:24 +00:00
|
|
|
def reset!(url)
|
|
|
|
new(url).reset!
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
2021-05-05 21:39:02 +00:00
|
|
|
|
|
|
|
def warning_domains
|
2022-04-28 15:47:34 +00:00
|
|
|
domains = redis.keys(exhausted_deliveries_key_by('*')).map do |key|
|
2021-05-05 21:39:02 +00:00
|
|
|
key.delete_prefix(exhausted_deliveries_key_by(''))
|
|
|
|
end
|
|
|
|
|
2023-11-06 15:51:52 +00:00
|
|
|
domains - UnavailableDomain.pluck(:domain)
|
2021-05-05 21:39:02 +00:00
|
|
|
end
|
|
|
|
|
2022-12-01 09:32:10 +00:00
|
|
|
def warning_domains_map(domains = nil)
|
|
|
|
if domains.nil?
|
|
|
|
warning_domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) }
|
|
|
|
else
|
|
|
|
domains -= UnavailableDomain.where(domain: domains).pluck(:domain)
|
|
|
|
domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) }.filter { |_, days| days.positive? }
|
|
|
|
end
|
2021-05-05 21:39:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def exhausted_deliveries_key_by(host)
|
|
|
|
"exhausted_deliveries:#{host}"
|
|
|
|
end
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def exhausted_deliveries_key
|
2020-04-15 18:33:24 +00:00
|
|
|
"exhausted_deliveries:#{@host}"
|
2017-09-29 01:16:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def today
|
|
|
|
Time.now.utc.strftime('%Y%m%d')
|
|
|
|
end
|
|
|
|
|
|
|
|
def reached_failure_threshold?
|
|
|
|
days >= FAILURE_DAYS_THRESHOLD
|
|
|
|
end
|
|
|
|
end
|