Class: Webhookdb::Organization::Alerting
- Inherits:
-
Object
- Object
- Webhookdb::Organization::Alerting
- 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
-
#org ⇒ Object
readonly
Returns the value of attribute org.
Instance Method Summary collapse
-
#dispatch_alert(message_template, separate_connection: true) ⇒ Object
Dispatch an alert using the given message template.
- #dispatch_alert_default(message_template, separate_connection:) ⇒ Object
-
#initialize(org) ⇒ Alerting
constructor
A new instance of Alerting.
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
#org ⇒ Object (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.
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(, separate_connection: true) self.validate_template() if self.org.error_handlers.empty? self.dispatch_alert_default(, separate_connection:) return end self.org.error_handlers.each do |eh| payload = eh.payload_for_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(, separate_connection:) signature = .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: .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 .dispatch_email(c) end end end |