1
0

delivery_failure_tracker.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. class DeliveryFailureTracker
  3. include Redisable
  4. FAILURE_DAYS_THRESHOLD = 7
  5. def initialize(url_or_host)
  6. @host = url_or_host.start_with?('https://', 'http://') ? Addressable::URI.parse(url_or_host).normalized_host : url_or_host
  7. end
  8. def track_failure!
  9. redis.sadd(exhausted_deliveries_key, today)
  10. UnavailableDomain.create(domain: @host) if reached_failure_threshold?
  11. end
  12. def track_success!
  13. redis.del(exhausted_deliveries_key)
  14. UnavailableDomain.find_by(domain: @host)&.destroy
  15. end
  16. def clear_failures!
  17. redis.del(exhausted_deliveries_key)
  18. end
  19. def days
  20. redis.scard(exhausted_deliveries_key) || 0
  21. end
  22. def available?
  23. !UnavailableDomain.exists?(domain: @host)
  24. end
  25. def exhausted_deliveries_days
  26. @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) }
  27. end
  28. alias reset! track_success!
  29. class << self
  30. include Redisable
  31. def without_unavailable(urls)
  32. unavailable_domains_map = Rails.cache.fetch('unavailable_domains') { UnavailableDomain.pluck(:domain).index_with(true) }
  33. urls.reject do |url|
  34. host = Addressable::URI.parse(url).normalized_host
  35. unavailable_domains_map[host]
  36. end
  37. end
  38. def available?(url)
  39. new(url).available?
  40. end
  41. def reset!(url)
  42. new(url).reset!
  43. end
  44. def warning_domains
  45. domains = redis.keys(exhausted_deliveries_key_by('*')).map do |key|
  46. key.delete_prefix(exhausted_deliveries_key_by(''))
  47. end
  48. domains - UnavailableDomain.pluck(:domain)
  49. end
  50. def warning_domains_map(domains = nil)
  51. if domains.nil?
  52. warning_domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) }
  53. else
  54. domains -= UnavailableDomain.where(domain: domains).pluck(:domain)
  55. domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) }.filter { |_, days| days.positive? }
  56. end
  57. end
  58. private
  59. def exhausted_deliveries_key_by(host)
  60. "exhausted_deliveries:#{host}"
  61. end
  62. end
  63. private
  64. def exhausted_deliveries_key
  65. "exhausted_deliveries:#{@host}"
  66. end
  67. def today
  68. Time.now.utc.strftime('%Y%m%d')
  69. end
  70. def reached_failure_threshold?
  71. days >= FAILURE_DAYS_THRESHOLD
  72. end
  73. end