Module: Webhooks::Outgoing::EndpointDeactivatable

Extended by:
ActiveSupport::Concern
Included in:
Endpoint
Defined in:
app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 4

def active?
  deactivated_at.nil?
end

#deactivate!Object



20
21
22
23
24
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 20

def deactivate!
  return if deactivated?

  update!(deactivated_at: Time.current)
end

#deactivated?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 8

def deactivated?
  deactivated_at.present?
end

#handle_exhausted_delivery_attemptsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 39

def handle_exhausted_delivery_attempts
  return unless BulletTrain::OutgoingWebhooks::Engine.config.outgoing_webhooks[:automatic_endpoint_deactivation_enabled]
  return if deactivated?

  increment!(:consecutive_failed_deliveries)

  # If the endpoint is marked for deactivation, we check if the cooling-off period (deactivation_in setting) has passed.
  # If so, we mark it as deactivated.
  if should_be_deactivated?
    deactivate!
    notify_deactivated
  elsif should_be_marked_for_deactivation?
    mark_for_deactivation!
    notify_deactivation_limit_reached
  end
end

#mark_for_deactivation!Object



26
27
28
29
30
31
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 26

def mark_for_deactivation!
  return if marked_for_deactivation?
  return if deactivated?

  update!(deactivation_limit_reached_at: Time.current)
end

#marked_for_deactivation?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 12

def marked_for_deactivation?
  deactivation_limit_reached_at.present? && deactivated_at.nil?
end

#notify_deactivatedObject



75
76
77
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 75

def notify_deactivated
  Webhooks::Outgoing::EndpointMailer.deactivated(self).deliver_later
end

#notify_deactivation_limit_reachedObject



71
72
73
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 71

def notify_deactivation_limit_reached
  Webhooks::Outgoing::EndpointMailer.deactivation_limit_reached(self).deliver_later
end

#reactivate!Object



33
34
35
36
37
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 33

def reactivate!
  return unless deactivated?

  update!(deactivated_at: nil, deactivation_limit_reached_at: nil, consecutive_failed_deliveries: 0)
end

#reset_failed_deliveries_tracking!Object



16
17
18
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 16

def reset_failed_deliveries_tracking!
  update_columns(deactivation_limit_reached_at: nil, consecutive_failed_deliveries: 0)
end

#should_be_deactivated?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 56

def should_be_deactivated?
  return false unless marked_for_deactivation?
  return false if deactivated?

  deactivation_limit_reached_at <= BulletTrain::OutgoingWebhooks::Engine.config.outgoing_webhooks.dig(:automatic_endpoint_deactivation_settings, :deactivation_in).ago
end

#should_be_marked_for_deactivation?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'app/models/concerns/webhooks/outgoing/endpoint_deactivatable.rb', line 63

def should_be_marked_for_deactivation?
  return false if deactivated?
  return false if marked_for_deactivation?

  max_limit = BulletTrain::OutgoingWebhooks::Engine.config.outgoing_webhooks.dig(:automatic_endpoint_deactivation_settings, :max_limit)
  consecutive_failed_deliveries >= max_limit
end