Class: Webhookdb::Organization::Alerting

Inherits:
Object
  • Object
show all
Includes:
Appydays::Configurable
Defined in:
lib/webhookdb/organization/alerting.rb

Overview

Alert an organization when errors happen during webhook handling. These errors are explicitly managed in the handler code, and are usually things like outdated credentials. The alerts contain information about the error, and actions to take to fix the problem. If an organization has no Webhookdb::Postgres::ErrorHandler registered, send an email to org admins instead.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(org) ⇒ Alerting

Returns a new instance of Alerting.



36
37
38
# File 'lib/webhookdb/organization/alerting.rb', line 36

def initialize(org)
  @org = org
end

Instance Attribute Details

#orgObject (readonly)

Returns the value of attribute org.



34
35
36
# File 'lib/webhookdb/organization/alerting.rb', line 34

def org
  @org
end

Instance Method Details

#dispatch_alert(message_template, separate_connection: true) ⇒ Object

Dispatch an alert using the given message template. See Webhookdb::Organization::Alerting for details about how alerts are dispatched.

Parameters:

  • message_template (Webhookdb::Message::Template)
  • separate_connection (true, false) (defaults to: true)

    Only relevant if the organization has no error handlers and email alerting (dispatch_alert_default) is used. If true, send the alert on a separate connection. See Webhookdb::Idempotency. Defaults to true since this is an alert method and we don’t want it to error accidentally, if the code is called from an unexpected situation.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/webhookdb/organization/alerting.rb', line 47

def dispatch_alert(message_template, separate_connection: true)
  self.validate_template(message_template)
  if self.org.error_handlers.empty?
    self.dispatch_alert_default(message_template, separate_connection:)
    return
  end
  self.org.error_handlers.each do |eh|
    payload = eh.payload_for_template(message_template)
    # It's possible that the template includes caller-provided values including improperly-encoded strings.
    # Sidekiq's strict job args will do a dump/parse to check for valid args,
    # which will potentially fail if valid utf-8 bytes are in a string that's encoded as ascii.
    # Really hard to explain, so see the specs, but there's nothing we can do about invalid content
    # other than not error.
    payload = JSON.parse(JSON.dump(payload))
    Webhookdb::Jobs::OrganizationErrorHandlerDispatch.perform_async(eh.id, payload.as_json)
  end
end

#dispatch_alert_default(message_template, separate_connection:) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/webhookdb/organization/alerting.rb', line 80

def dispatch_alert_default(message_template, separate_connection:)
  signature = message_template.signature
  max_alerts_per_customer_per_day = Webhookdb::Organization::Alerting.max_alerts_per_customer_per_day
  yesterday = Time.now - 24.hours
  self.org.admin_customers.each do |c|
    idem = Webhookdb::Idempotency.every(Webhookdb::Organization::Alerting.interval)
    idem = idem.using_seperate_connection if separate_connection
    idem.under_key("orgalert-#{signature}-#{c.id}") do
      sent_last_day = Webhookdb::Message::Delivery.
        where(template: message_template.full_template_name, recipient: c).
        where { created_at > yesterday }.
        limit(max_alerts_per_customer_per_day).
        count
      next unless sent_last_day < max_alerts_per_customer_per_day
      message_template.dispatch_email(c)
    end
  end
end