1 # frozen_string_literal: true
3 class DeliveryFailureTracker
4 FAILURE_DAYS_THRESHOLD
= 7
6 def initialize(url_or_host
)
7 @host = url_or_host
.start_with
?('https://') || url_or_host
.start_with
?('http://') ? Addressable
::URI.parse(url_or_host
).normalized_host
: url_or_host
11 Redis
.current
.sadd(exhausted_deliveries_key
, today
)
12 UnavailableDomain
.create(domain
: @host) if reached_failure_threshold
?
16 Redis
.current
.del(exhausted_deliveries_key
)
17 UnavailableDomain
.find_by(domain
: @host)&.destroy
21 Redis
.current
.scard(exhausted_deliveries_key
) || 0
25 !UnavailableDomain
.where(domain
: @host).exists
?
28 alias reset! track_success!
31 def without_unavailable(urls
)
32 unavailable_domains_map
= Rails
.cache
.fetch('unavailable_domains') { UnavailableDomain
.pluck(:domain).index_with(true) }
35 host
= Addressable
::URI.parse(url
).normalized_host
36 unavailable_domains_map
[host
]
51 def exhausted_deliveries_key
52 "exhausted_deliveries:#{@host}"
56 Time
.now
.utc
.strftime('%Y%m%d')
59 def reached_failure_threshold
?
60 days
>= FAILURE_DAYS_THRESHOLD