Class: InstagramConnect::WebhookEvent

Inherits:
ApplicationRecord show all
Defined in:
app/models/instagram_connect/webhook_event.rb

Overview

One row per webhook event Meta delivered, stored verbatim before anything interprets it. See the migration for why this exists rather than a counter.

Constant Summary collapse

STATUSES =
%w[pending processed unhandled failed].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.claim(field:, dedupe_key:, event_type:, payload:, account: nil, object: nil, entry_id: nil, occurred_at: nil, handler: nil) ⇒ Object

Claims an event for processing, or returns nil if this exact event was already delivered. Meta retries webhooks it considers unacknowledged, so duplicates are routine rather than exceptional — the unique index on [field, dedupe_key] is what makes the claim atomic under concurrency.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/instagram_connect/webhook_event.rb', line 24

def self.claim(field:, dedupe_key:, event_type:, payload:, account: nil,
               object: nil, entry_id: nil, occurred_at: nil, handler: nil)
  create!(
    account: ,
    object: object,
    field: field,
    event_type: event_type,
    dedupe_key: dedupe_key,
    entry_id: entry_id,
    occurred_at: occurred_at,
    received_at: Time.current,
    handler: handler,
    payload: payload,
    status: "pending"
  )
rescue ActiveRecord::RecordNotUnique
  nil
end

Instance Method Details

#mark_failed!(error) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/models/instagram_connect/webhook_event.rb', line 54

def mark_failed!(error)
  update!(
    status: "failed",
    processed_at: Time.current,
    error_class: error.class.name,
    error_message: error.message.to_s.truncate(1000)
  )
end

#mark_processed!(subject = nil) ⇒ Object



43
44
45
# File 'app/models/instagram_connect/webhook_event.rb', line 43

def mark_processed!(subject = nil)
  update!(status: "processed", processed_at: Time.current, subject: subject)
end

#mark_unhandled!Object

Recorded, not dropped: a field the gem does not parse yet is still worth keeping, because replaying it later is the only way to recover events Meta will never send again.



50
51
52
# File 'app/models/instagram_connect/webhook_event.rb', line 50

def mark_unhandled!
  update!(status: "unhandled", processed_at: Time.current)
end

#processed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/instagram_connect/webhook_event.rb', line 63

def processed?
  status == "processed"
end