Class: JetstreamBridge::OutboxEvent

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/jetstream_bridge/models/outbox_event.rb,
lib/jetstream_bridge/models/outbox_event.rb

Overview

Shim: loud failure if AR isn’t present but someone calls the model.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ar_connected?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 26

def ar_connected?
  # Avoid creating a connection; rescue if pool isn't set yet.
  ActiveRecord::Base.connected? && connection_pool.active_connection?
rescue StandardError
  false
end

.cleanup_sent(older_than: 7.days) ⇒ Integer

Clean up old sent events

Parameters:

  • older_than (ActiveSupport::Duration) (defaults to: 7.days)

    Age threshold

Returns:

  • (Integer)

    Number of records deleted



110
111
112
113
114
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 110

def cleanup_sent(older_than: 7.days)
  return 0 unless has_column?(:status) && has_column?(:sent_at)

  sent.where('sent_at < ?', older_than.ago).delete_all
end

.has_column?(name) ⇒ Boolean

Safe column presence check that never boots a connection during class load.

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 18

def has_column?(name)
  return false unless ar_connected?

  connection.schema_cache.columns_hash(table_name).key?(name.to_s)
rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError
  false
end

.method_missing(method_name, *_args) ⇒ Object



159
160
161
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 159

def method_missing(method_name, *_args, &)
  raise_missing_ar!('Outbox', method_name)
end

.respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 163

def respond_to_missing?(_method_name, _include_private = false)
  false
end

.retry_failed(limit: 100) ⇒ Integer

Retry failed events

Parameters:

  • limit (Integer) (defaults to: 100)

    Maximum number of events to retry

Returns:

  • (Integer)

    Number of events reset for retry



96
97
98
99
100
101
102
103
104
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 96

def retry_failed(limit: 100)
  return 0 unless has_column?(:status)

  failed.limit(limit).update_all(
    status: JetstreamBridge::Config::Status::PENDING,
    attempts: 0,
    last_error: nil
  )
end

Instance Method Details

#mark_failed!(err_msg) ⇒ Object



125
126
127
128
129
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 125

def mark_failed!(err_msg)
  self.status     = JetstreamBridge::Config::Status::FAILED if self.class.has_column?(:status)
  self.last_error = err_msg if self.class.has_column?(:last_error)
  save!
end

#mark_sent!Object

—- Instance Methods —-



118
119
120
121
122
123
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 118

def mark_sent!
  now = Time.now.utc
  self.status  = JetstreamBridge::Config::Status::SENT if self.class.has_column?(:status)
  self.sent_at = now if self.class.has_column?(:sent_at)
  save!
end

#payload_hashObject



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 141

def payload_hash
  v = self[:payload]
  case v
  when String then begin
    Oj.load(v, mode: :strict)
  rescue Oj::Error
    {}
  end
  when Hash then v
  else
    v.respond_to?(:as_json) ? v.as_json : {}
  end
end

#retry!Object



131
132
133
134
135
136
137
138
139
# File 'lib/jetstream_bridge/models/outbox_event.rb', line 131

def retry!
  return false unless self.class.has_column?(:status)

  update!(
    status: JetstreamBridge::Config::Status::PENDING,
    attempts: 0,
    last_error: nil
  )
end