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