Class: ApprovalEngine::OutboxEvent

Inherits:
ApplicationRecord show all
Defined in:
app/models/approval_engine/outbox_event.rb

Overview

A row in the transactional outbox. It is written in the same transaction as the state change that produced it, then relayed asynchronously — so a crashing mailer or a down payment API can never roll back an approval, and no side-effect is ever silently lost.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.drain!(older_than: 1.minute, limit: 1000) ⇒ Object

Safety net for events whose relay job was lost (e.g. the process died between commit and enqueue). Wire this to a periodic ActiveJob/cron. ‘older_than` skips freshly-created events whose relay is likely still in-flight, so draining never double-enqueues a healthy event; dead letters are skipped so exhausted poison events aren’t retried in perpetuity.



29
30
31
32
33
34
35
# File 'app/models/approval_engine/outbox_event.rb', line 29

def self.drain!(older_than: 1.minute, limit: 1000)
  unprocessed.where(failed_at: nil)
             .where(created_at: ..older_than.ago)
             .order(:created_at).limit(limit).pluck(:id).each do |id|
    ProcessOutboxJob.perform_later(id)
  end
end

Instance Method Details

#mark_processed!Object



37
38
39
# File 'app/models/approval_engine/outbox_event.rb', line 37

def mark_processed!
  update!(processed: true, processed_at: Time.current, error_payload: nil, delivery_error: nil)
end