Module: ActiveWebhook::Models::SubscriptionAdditions

Extended by:
ActiveSupport::Concern
Included in:
Subscription
Defined in:
lib/active_webhook/models/subscription_additions.rb

Instance Method Summary collapse

Instance Method Details

#clean_error_logObject



27
28
29
# File 'lib/active_webhook/models/subscription_additions.rb', line 27

def clean_error_log
  error_logs.delete_all if previous_changes.key?(:disabled_at) && enabled?
end

#disable(reason = nil) ⇒ Object



43
44
45
46
# File 'lib/active_webhook/models/subscription_additions.rb', line 43

def disable(reason = nil)
  self.disabled_at = Time.current
  self.disabled_reason = reason
end

#disable!(reason = nil) ⇒ Object



48
49
50
51
# File 'lib/active_webhook/models/subscription_additions.rb', line 48

def disable!(reason = nil)
  disable reason
  save!
end

#disabled?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_webhook/models/subscription_additions.rb', line 63

def disabled?
  !enabled?
end

#enableObject



53
54
55
56
# File 'lib/active_webhook/models/subscription_additions.rb', line 53

def enable
  self.disabled_at = nil
  self.disabled_reason = nil
end

#enable!Object



58
59
60
61
# File 'lib/active_webhook/models/subscription_additions.rb', line 58

def enable!
  enable
  save!
end

#enabled?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/active_webhook/models/subscription_additions.rb', line 67

def enabled?
  disabled_at.nil?
end

#ensure_error_log_requirement_is_met!(max_errors_per_hour) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_webhook/models/subscription_additions.rb', line 31

def ensure_error_log_requirement_is_met! max_errors_per_hour
  return false if disabled?

  if max_errors_per_hour.present? && error_logs.where('created_at > ?', 1.hour.ago).count > max_errors_per_hour
    disable! "Exceeded max_errors_per_hour of (#{max_errors_per_hour})"
    return true
  end
rescue StandardError
  # intentionally squash errors so that we don't end up in a loop where queue adapter retries and locks table
  false
end

#set_disabled_reasonObject



23
24
25
# File 'lib/active_webhook/models/subscription_additions.rb', line 23

def set_disabled_reason
  self.disabled_reason = nil if self.disabled_at.nil?
end