Class: TCB::OutboxStore::ActiveRecord
- Inherits:
-
Object
- Object
- TCB::OutboxStore::ActiveRecord
- Defined in:
- lib/tcb/outbox_store/active_record.rb
Instance Method Summary collapse
- #all ⇒ Object
-
#initialize(model) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #insert(event_id:, stream_id:, version:, handler_class:) ⇒ Object
- #lock(entry, locked_at: Time.now) ⇒ Object
- #mark_delivered(entry, delivered_at: Time.now) ⇒ Object
- #mark_failed(entry, error:) ⇒ Object
- #pending ⇒ Object
- #recover_stale_locks(older_than:) ⇒ Object
Constructor Details
#initialize(model) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
8 9 10 |
# File 'lib/tcb/outbox_store/active_record.rb', line 8 def initialize(model) @model = model end |
Instance Method Details
#all ⇒ Object
43 44 45 |
# File 'lib/tcb/outbox_store/active_record.rb', line 43 def all @model.all.map { |r| to_entry(r) } end |
#insert(event_id:, stream_id:, version:, handler_class:) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tcb/outbox_store/active_record.rb', line 12 def insert(event_id:, stream_id:, version:, handler_class:) id = SecureRandom.uuid now = Time.now @model.create!( id: id, event_id: event_id, stream_id: stream_id, version: version, handler_class: handler_class.name, status: "pending", locked_at: nil, delivered_at: nil, error: nil, created_at: now ) OutboxEntry.new( id: id, event_id: event_id, stream_id: stream_id, version: version, handler_class: handler_class.name, status: :pending, locked_at: nil, delivered_at: nil, error: nil, created_at: now ) end |
#lock(entry, locked_at: Time.now) ⇒ Object
51 52 53 54 |
# File 'lib/tcb/outbox_store/active_record.rb', line 51 def lock(entry, locked_at: Time.now) @model.where(id: entry.id).update_all(status: "locked", locked_at: locked_at) entry.with(status: :locked, locked_at: locked_at) end |
#mark_delivered(entry, delivered_at: Time.now) ⇒ Object
56 57 58 59 |
# File 'lib/tcb/outbox_store/active_record.rb', line 56 def mark_delivered(entry, delivered_at: Time.now) @model.where(id: entry.id).update_all(status: "delivered", delivered_at: delivered_at) entry.with(status: :delivered, delivered_at: delivered_at) end |
#mark_failed(entry, error:) ⇒ Object
61 62 63 64 |
# File 'lib/tcb/outbox_store/active_record.rb', line 61 def mark_failed(entry, error:) @model.where(id: entry.id).update_all(status: "failed", error: error.) entry.with(status: :failed, error: error.) end |
#pending ⇒ Object
47 48 49 |
# File 'lib/tcb/outbox_store/active_record.rb', line 47 def pending @model.where(status: "pending").order(:stream_id, :version).map { |r| to_entry(r) } end |
#recover_stale_locks(older_than:) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/tcb/outbox_store/active_record.rb', line 66 def recover_stale_locks(older_than:) stale = @model.where(status: "locked").where("locked_at < ?", older_than) stale.map do |record| @model.where(id: record.id).update_all(status: "pending", locked_at: nil) to_entry(record).with(status: :pending, locked_at: nil) end end |