Class: Pigeon::Models::OutboxMessage
- Inherits:
-
Object
- Object
- Pigeon::Models::OutboxMessage
- Defined in:
- lib/pigeon/models/outbox_message.rb
Overview
Base class for outbox message model This is a framework-agnostic representation of the outbox message
Direct Known Subclasses
Constant Summary collapse
- ATTRIBUTES =
Attributes that should be present in all framework implementations
%i[ id topic key headers partition payload status retry_count max_retries error_message correlation_id created_at updated_at published_at next_retry_at ].freeze
- STATUSES =
Valid status values
%w[pending processing published failed].freeze
- DEFAULTS =
Default values for attributes
{ status: "pending", retry_count: 0, headers: {}, created_at: -> { Time.now }, updated_at: -> { Time.now } }.freeze
Class Method Summary collapse
-
.create(attributes = {}) ⇒ OutboxMessage
Create a new outbox message with the given attributes.
-
.find(id) ⇒ OutboxMessage?
Find a message by ID.
-
.find_by_status(status, limit = 100) ⇒ Array<OutboxMessage>
Find messages by status.
-
.find_ready_for_retry(limit = 100) ⇒ Array<OutboxMessage>
Find messages ready for retry.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get an attribute value.
-
#[]=(name, value) ⇒ Object
Set an attribute value.
-
#attributes ⇒ Hash
Get all attributes.
-
#calculate_next_retry_time ⇒ Time
Calculate the next retry time based on exponential backoff.
-
#increment_retry_count ⇒ Boolean
Increment the retry count and set the next retry time.
-
#initialize(attributes = {}) ⇒ OutboxMessage
constructor
Initialize a new outbox message.
-
#mark_as_failed(error = nil) ⇒ Boolean
Mark the message as failed.
-
#mark_as_published ⇒ Boolean
Mark the message as published.
-
#max_retries_exceeded? ⇒ Boolean
Check if the message has exceeded the maximum retry count.
-
#save ⇒ Boolean
Save the message.
-
#update(attributes = {}) ⇒ Boolean
Update the message attributes.
Constructor Details
#initialize(attributes = {}) ⇒ OutboxMessage
Initialize a new outbox message
70 71 72 73 74 75 |
# File 'lib/pigeon/models/outbox_message.rb', line 70 def initialize(attributes = {}) @attributes = DEFAULTS.dup attributes.each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end end |
Class Method Details
.create(attributes = {}) ⇒ OutboxMessage
Create a new outbox message with the given attributes
42 43 44 |
# File 'lib/pigeon/models/outbox_message.rb', line 42 def self.create(attributes = {}) new(attributes) end |
.find(id) ⇒ OutboxMessage?
Find a message by ID
49 50 51 |
# File 'lib/pigeon/models/outbox_message.rb', line 49 def self.find(id) raise NotImplementedError, "#{self.class.name}#find must be implemented by a framework adapter" end |
.find_by_status(status, limit = 100) ⇒ Array<OutboxMessage>
Find messages by status
57 58 59 |
# File 'lib/pigeon/models/outbox_message.rb', line 57 def self.find_by_status(status, limit = 100) raise NotImplementedError, "#{self.class.name}#find_by_status must be implemented by a framework adapter" end |
.find_ready_for_retry(limit = 100) ⇒ Array<OutboxMessage>
Find messages ready for retry
64 65 66 |
# File 'lib/pigeon/models/outbox_message.rb', line 64 def self.find_ready_for_retry(limit = 100) raise NotImplementedError, "#{self.class.name}#find_ready_for_retry must be implemented by a framework adapter" end |
Instance Method Details
#[](name) ⇒ Object
Get an attribute value
80 81 82 |
# File 'lib/pigeon/models/outbox_message.rb', line 80 def [](name) @attributes[name.to_sym] end |
#[]=(name, value) ⇒ Object
Set an attribute value
87 88 89 |
# File 'lib/pigeon/models/outbox_message.rb', line 87 def []=(name, value) @attributes[name.to_sym] = value end |
#attributes ⇒ Hash
Get all attributes
93 94 95 |
# File 'lib/pigeon/models/outbox_message.rb', line 93 def attributes @attributes.dup end |
#calculate_next_retry_time ⇒ Time
Calculate the next retry time based on exponential backoff
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/pigeon/models/outbox_message.rb', line 143 def calculate_next_retry_time base_delay = Pigeon.config.retry_delay || 30 # 30 seconds default max_delay = Pigeon.config.max_retry_delay || 86_400 # 24 hours default # Exponential backoff: delay = base_delay * (2 ^ retry_count) delay = base_delay * (2**retry_count) delay = [delay, max_delay].min # Cap at max delay Time.now + delay end |
#increment_retry_count ⇒ Boolean
Increment the retry count and set the next retry time
134 135 136 137 138 139 |
# File 'lib/pigeon/models/outbox_message.rb', line 134 def increment_retry_count self.retry_count += 1 self.next_retry_at = calculate_next_retry_time self.updated_at = Time.now save end |
#mark_as_failed(error = nil) ⇒ Boolean
Mark the message as failed
125 126 127 128 129 130 |
# File 'lib/pigeon/models/outbox_message.rb', line 125 def mark_as_failed(error = nil) self.status = "failed" self. = error.is_a?(Exception) ? "#{error.class}: #{error.}" : error.to_s self.updated_at = Time.now save end |
#mark_as_published ⇒ Boolean
Mark the message as published
115 116 117 118 119 120 |
# File 'lib/pigeon/models/outbox_message.rb', line 115 def mark_as_published self.status = "published" self.published_at = Time.now self.updated_at = Time.now save end |
#max_retries_exceeded? ⇒ Boolean
Check if the message has exceeded the maximum retry count
156 157 158 159 |
# File 'lib/pigeon/models/outbox_message.rb', line 156 def max_retries_exceeded? max_retries = self[:max_retries] || Pigeon.config.max_retries || 10 retry_count >= max_retries end |
#save ⇒ Boolean
Save the message
99 100 101 |
# File 'lib/pigeon/models/outbox_message.rb', line 99 def save raise NotImplementedError, "#{self.class.name}#save must be implemented by a framework adapter" end |
#update(attributes = {}) ⇒ Boolean
Update the message attributes
106 107 108 109 110 111 |
# File 'lib/pigeon/models/outbox_message.rb', line 106 def update(attributes = {}) attributes.each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end save end |