Class: Sbmt::Outbox::BaseItem
- Inherits:
-
Object
- Object
- Sbmt::Outbox::BaseItem
- Defined in:
- app/models/sbmt/outbox/base_item.rb
Direct Known Subclasses
Class Method Summary collapse
- .box_id ⇒ Object
- .box_name ⇒ Object
- .box_type ⇒ Object
- .bucket_partitions ⇒ Object
- .calc_bucket_partitions(count) ⇒ Object
- .config ⇒ Object
- .partition_buckets ⇒ Object
Instance Method Summary collapse
- #add_error(ex_or_msg) ⇒ Object
- #for_processing? ⇒ Boolean
- #increment_errors_counter ⇒ Object
- #log_details ⇒ Object
- #max_retries_exceeded? ⇒ Boolean
- #options ⇒ Object
- #partition ⇒ Object
- #payload ⇒ Object
- #payload=(value) ⇒ Object
- #payload_builder ⇒ Object
- #proto_payload ⇒ Object
- #proto_payload=(value) ⇒ Object
- #retriable? ⇒ Boolean
- #touch_processed_at ⇒ Object
- #transports ⇒ Object
Class Method Details
.box_id ⇒ Object
24 25 26 |
# File 'app/models/sbmt/outbox/base_item.rb', line 24 def box_id @box_id ||= name.underscore.tr("/", "-").dasherize end |
.box_name ⇒ Object
20 21 22 |
# File 'app/models/sbmt/outbox/base_item.rb', line 20 def box_name @box_name ||= name.underscore end |
.box_type ⇒ Object
16 17 18 |
# File 'app/models/sbmt/outbox/base_item.rb', line 16 def box_type raise NotImplementedError end |
.bucket_partitions ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'app/models/sbmt/outbox/base_item.rb', line 44 def bucket_partitions @bucket_partitions ||= partition_buckets.each_with_object({}) do |(partition, buckets), m| buckets.each do |bucket| m[bucket] = partition end end end |
.calc_bucket_partitions(count) ⇒ Object
32 33 34 35 36 37 38 |
# File 'app/models/sbmt/outbox/base_item.rb', line 32 def calc_bucket_partitions(count) (0...count).to_a .index_with do |x| (0...config.bucket_size).to_a .select { |p| p % count == x } end end |
.config ⇒ Object
28 29 30 |
# File 'app/models/sbmt/outbox/base_item.rb', line 28 def config @config ||= lookup_config.new(box_id: box_id, box_name: box_name) end |
.partition_buckets ⇒ Object
40 41 42 |
# File 'app/models/sbmt/outbox/base_item.rb', line 40 def partition_buckets @partition_buckets ||= calc_bucket_partitions(config.partition_size) end |
Instance Method Details
#add_error(ex_or_msg) ⇒ Object
152 153 154 155 156 157 158 |
# File 'app/models/sbmt/outbox/base_item.rb', line 152 def add_error(ex_or_msg) increment_errors_counter return unless has_attribute?(:error_log) self.error_log = "-----\n#{Time.zone.now} \n #{ex_or_msg}\n #{add_backtrace(ex_or_msg)}" end |
#for_processing? ⇒ Boolean
103 104 105 |
# File 'app/models/sbmt/outbox/base_item.rb', line 103 def for_processing? pending? end |
#increment_errors_counter ⇒ Object
148 149 150 |
# File 'app/models/sbmt/outbox/base_item.rb', line 148 def increment_errors_counter increment(:errors_count) end |
#log_details ⇒ Object
125 126 127 |
# File 'app/models/sbmt/outbox/base_item.rb', line 125 def log_details default_log_details.deep_merge(extra_log_details) end |
#max_retries_exceeded? ⇒ Boolean
141 142 143 144 145 146 |
# File 'app/models/sbmt/outbox/base_item.rb', line 141 def max_retries_exceeded? return false if config.strict_order return true unless retriable? errors_count > config.max_retries end |
#options ⇒ Object
107 108 109 110 111 |
# File 'app/models/sbmt/outbox/base_item.rb', line 107 def = (self[:options] || {}).symbolize_keys = .deep_merge().deep_merge() .symbolize_keys end |
#partition ⇒ Object
160 161 162 |
# File 'app/models/sbmt/outbox/base_item.rb', line 160 def partition self.class.bucket_partitions.fetch(bucket) end |
#payload ⇒ Object
87 88 89 90 91 92 93 |
# File 'app/models/sbmt/outbox/base_item.rb', line 87 def payload if has_attribute?(:proto_payload) proto_payload else self[:payload] end end |
#payload=(value) ⇒ Object
95 96 97 98 99 100 101 |
# File 'app/models/sbmt/outbox/base_item.rb', line 95 def payload=(value) if has_attribute?(:proto_payload) self.proto_payload = value else self[:payload] = value end end |
#payload_builder ⇒ Object
129 130 131 |
# File 'app/models/sbmt/outbox/base_item.rb', line 129 def payload_builder nil end |
#proto_payload ⇒ Object
71 72 73 74 75 76 77 |
# File 'app/models/sbmt/outbox/base_item.rb', line 71 def proto_payload if has_attribute?(:payload) payload else self[:proto_payload] end end |
#proto_payload=(value) ⇒ Object
79 80 81 82 83 84 85 |
# File 'app/models/sbmt/outbox/base_item.rb', line 79 def proto_payload=(value) if has_attribute?(:payload) self.payload = value else self[:proto_payload] = value end end |
#retriable? ⇒ Boolean
137 138 139 |
# File 'app/models/sbmt/outbox/base_item.rb', line 137 def retriable? config.max_retries > 0 end |
#touch_processed_at ⇒ Object
133 134 135 |
# File 'app/models/sbmt/outbox/base_item.rb', line 133 def touch_processed_at self.processed_at = Time.current end |
#transports ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/sbmt/outbox/base_item.rb', line 113 def transports if config.transports.empty? raise Error, "Transports are not defined" end if has_attribute?(:event_name) config.transports.fetch(event_name) else config.transports.fetch(:_all_) end end |