Class: Journaled::Outbox::Event

Inherits:
Object
  • Object
show all
Defined in:
app/models/journaled/outbox/event.rb

Overview

ActiveRecord model for Outbox-style event processing

This model is only used when the Outbox::Adapter is configured. Events are stored in the database and processed by worker daemons.

Successfully delivered events are deleted immediately. Failed events are marked with failed_at and can be queried or requeued.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetch_batch_for_updateArray<Journaled::Outbox::Event>

Fetch a batch of events for processing using SELECT FOR UPDATE

In :guaranteed_order mode, uses blocking lock to ensure sequential processing. In :batch mode, uses SKIP LOCKED to allow parallel workers.

Returns:



36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/journaled/outbox/event.rb', line 36

def self.fetch_batch_for_update
  query = ready_to_process.limit(Journaled.worker_batch_size)

  lock_clause = if Journaled.outbox_processing_mode == :guaranteed_order
    'FOR UPDATE'
  else
    'FOR UPDATE SKIP LOCKED'
  end

  query.lock(lock_clause).to_a
end

.oldest_non_failed_timestampTime?

Get the oldest non-failed event’s timestamp

Returns:

  • (Time, nil)

    The timestamp of the oldest event, or nil if no events exist



63
64
65
# File 'app/models/journaled/outbox/event.rb', line 63

def self.oldest_non_failed_timestamp
  ready_to_process.order(:id).limit(1).pick(:created_at)
end

Instance Method Details

#requeue!Boolean

Requeue a failed event for processing

Clears failure information so the event can be retried.

Returns:

  • (Boolean)

    Whether the requeue was successful



53
54
55
56
57
58
# File 'app/models/journaled/outbox/event.rb', line 53

def requeue!
  update!(
    failed_at: nil,
    failure_reason: nil,
  )
end