2017-09-29 10:16:20 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DeliveryFailureTracker
|
|
|
|
FAILURE_DAYS_THRESHOLD = 7
|
|
|
|
|
2020-04-16 03:33:24 +09:00
|
|
|
def initialize(url_or_host)
|
|
|
|
@host = url_or_host.start_with?('https://') || url_or_host.start_with?('http://') ? Addressable::URI.parse(url_or_host).normalized_host : url_or_host
|
2017-09-29 10:16:20 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def track_failure!
|
|
|
|
Redis.current.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!
|
|
|
|
Redis.current.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!
|
|
|
|
Redis.current.del(exhausted_deliveries_key)
|
|
|
|
end
|
|
|
|
|
2017-09-29 10:16:20 +09:00
|
|
|
def days
|
|
|
|
Redis.current.scard(exhausted_deliveries_key) || 0
|
|
|
|
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
|
|
|
|
Redis.current.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) }
|
|
|
|
end
|
|
|
|
|
2020-04-16 03:33:24 +09:00
|
|
|
alias reset! track_success!
|
|
|
|
|
2017-09-29 10:16:20 +09:00
|
|
|
class << self
|
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
|
|
|
|
domains = Redis.current.keys(exhausted_deliveries_key_by('*')).map do |key|
|
|
|
|
key.delete_prefix(exhausted_deliveries_key_by(''))
|
|
|
|
end
|
|
|
|
|
|
|
|
domains - UnavailableDomain.all.pluck(:domain)
|
|
|
|
end
|
|
|
|
|
|
|
|
def warning_domains_map
|
|
|
|
warning_domains.index_with { |domain| Redis.current.scard(exhausted_deliveries_key_by(domain)) }
|
|
|
|
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
|