delivery_failure_tracker.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # frozen_string_literal: true
  2. class DeliveryFailureTracker
  3. FAILURE_DAYS_THRESHOLD = 7
  4. def initialize(inbox_url)
  5. @inbox_url = inbox_url
  6. end
  7. def track_failure!
  8. Redis.current.sadd(exhausted_deliveries_key, today)
  9. Redis.current.sadd('unavailable_inboxes', @inbox_url) if reached_failure_threshold?
  10. end
  11. def track_success!
  12. Redis.current.del(exhausted_deliveries_key)
  13. Redis.current.srem('unavailable_inboxes', @inbox_url)
  14. end
  15. def days
  16. Redis.current.scard(exhausted_deliveries_key) || 0
  17. end
  18. class << self
  19. def filter(arr)
  20. arr.reject(&method(:unavailable?))
  21. end
  22. def unavailable?(url)
  23. Redis.current.sismember('unavailable_inboxes', url)
  24. end
  25. def available?(url)
  26. !unavailable?(url)
  27. end
  28. def track_inverse_success!(from_account)
  29. new(from_account.inbox_url).track_success! if from_account.inbox_url.present?
  30. new(from_account.shared_inbox_url).track_success! if from_account.shared_inbox_url.present?
  31. end
  32. end
  33. private
  34. def exhausted_deliveries_key
  35. "exhausted_deliveries:#{@inbox_url}"
  36. end
  37. def today
  38. Time.now.utc.strftime('%Y%m%d')
  39. end
  40. def reached_failure_threshold?
  41. days >= FAILURE_DAYS_THRESHOLD
  42. end
  43. end