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
19 20 21 |
# File 'app/models/sbmt/outbox/base_item.rb', line 19 def box_id @box_id ||= name.underscore.tr("/", "-").dasherize end |
.box_name ⇒ Object
15 16 17 |
# File 'app/models/sbmt/outbox/base_item.rb', line 15 def box_name @box_name ||= name.underscore end |
.box_type ⇒ Object
11 12 13 |
# File 'app/models/sbmt/outbox/base_item.rb', line 11 def box_type raise NotImplementedError end |
.bucket_partitions ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'app/models/sbmt/outbox/base_item.rb', line 39 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
27 28 29 30 31 32 33 |
# File 'app/models/sbmt/outbox/base_item.rb', line 27 def calc_bucket_partitions(count) (0...count).to_a .each_with_object({}) do |x, m| m[x] = (0...config.bucket_size).to_a .select { |p| p % count == x } end end |
.config ⇒ Object
23 24 25 |
# File 'app/models/sbmt/outbox/base_item.rb', line 23 def config @config ||= lookup_config.new(box_id: box_id, box_name: box_name) end |
.partition_buckets ⇒ Object
35 36 37 |
# File 'app/models/sbmt/outbox/base_item.rb', line 35 def partition_buckets @partition_buckets ||= calc_bucket_partitions(config.partition_size) end |
Instance Method Details
#add_error(ex_or_msg) ⇒ Object
147 148 149 150 151 152 153 |
# File 'app/models/sbmt/outbox/base_item.rb', line 147 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
98 99 100 |
# File 'app/models/sbmt/outbox/base_item.rb', line 98 def for_processing? pending? end |
#increment_errors_counter ⇒ Object
143 144 145 |
# File 'app/models/sbmt/outbox/base_item.rb', line 143 def increment_errors_counter increment(:errors_count) end |
#log_details ⇒ Object
120 121 122 |
# File 'app/models/sbmt/outbox/base_item.rb', line 120 def log_details default_log_details.deep_merge(extra_log_details) end |
#max_retries_exceeded? ⇒ Boolean
136 137 138 139 140 141 |
# File 'app/models/sbmt/outbox/base_item.rb', line 136 def max_retries_exceeded? return false if config.strict_order return true unless retriable? errors_count > config.max_retries end |
#options ⇒ Object
102 103 104 105 106 |
# File 'app/models/sbmt/outbox/base_item.rb', line 102 def = (self[:options] || {}).symbolize_keys = .deep_merge().deep_merge() .symbolize_keys end |
#partition ⇒ Object
155 156 157 |
# File 'app/models/sbmt/outbox/base_item.rb', line 155 def partition self.class.bucket_partitions.fetch(bucket) end |
#payload ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/models/sbmt/outbox/base_item.rb', line 82 def payload if has_attribute?(:proto_payload) proto_payload else self[:payload] end end |
#payload=(value) ⇒ Object
90 91 92 93 94 95 96 |
# File 'app/models/sbmt/outbox/base_item.rb', line 90 def payload=(value) if has_attribute?(:proto_payload) self.proto_payload = value else self[:payload] = value end end |
#payload_builder ⇒ Object
124 125 126 |
# File 'app/models/sbmt/outbox/base_item.rb', line 124 def payload_builder nil end |
#proto_payload ⇒ Object
66 67 68 69 70 71 72 |
# File 'app/models/sbmt/outbox/base_item.rb', line 66 def proto_payload if has_attribute?(:payload) payload else self[:proto_payload] end end |
#proto_payload=(value) ⇒ Object
74 75 76 77 78 79 80 |
# File 'app/models/sbmt/outbox/base_item.rb', line 74 def proto_payload=(value) if has_attribute?(:payload) self.payload = value else self[:proto_payload] = value end end |
#retriable? ⇒ Boolean
132 133 134 |
# File 'app/models/sbmt/outbox/base_item.rb', line 132 def retriable? config.max_retries > 0 end |
#touch_processed_at ⇒ Object
128 129 130 |
# File 'app/models/sbmt/outbox/base_item.rb', line 128 def touch_processed_at self.processed_at = Time.current end |
#transports ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/sbmt/outbox/base_item.rb', line 108 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 |