Files
fizzy/app/models/webhook/delinquency_tracker.rb
Mike Dalessio fe6df7085f Finish the rollout of account_id to all core data models
Schema:
- add account_id to tables it was missing from
- make account_id a required column everywhere
- add [account_id] indexes, or add `account_id` to existing indices

Models:
- add `belongs_to :account` to all models (default to using a domain
  model's account whenever possible)
- add account_id in all the necessary fixtures
- add account_id to insert_all hashes
- pass account_id to a few initialize calls

Miscellaneous:
- update the import script to set account_id

Note that I'm not adding account_id to the join tables primarily
because I couldn't think of an easy way to populate it without making
it a full Join model, and that was more work than I have time to take
on right now.
2025-11-17 09:12:40 -05:00

44 lines
1017 B
Ruby

class Webhook::DelinquencyTracker < ApplicationRecord
DELINQUENCY_THRESHOLD = 10
DELINQUENCY_DURATION = 1.hour
belongs_to :account, default: -> { webhook.account }
belongs_to :webhook
def record_delivery_of(delivery)
if delivery.succeeded?
reset
else
mark_first_failure_time if consecutive_failures_count.zero?
increment!(:consecutive_failures_count, touch: true)
webhook.deactivate if delinquent?
end
end
private
def reset
update_columns consecutive_failures_count: 0, first_failure_at: nil
end
def mark_first_failure_time
update_columns first_failure_at: Time.current
end
def delinquent?
failing_for_too_long? && too_many_consecutive_failures?
end
def failing_for_too_long?
if first_failure_at
first_failure_at.before?(DELINQUENCY_DURATION.ago)
else
false
end
end
def too_many_consecutive_failures?
consecutive_failures_count >= DELINQUENCY_THRESHOLD
end
end