Class: Sbmt::Outbox::BaseItem

Inherits:
Object
  • Object
show all
Defined in:
app/models/sbmt/outbox/base_item.rb

Direct Known Subclasses

InboxItem, OutboxItem

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.box_idObject



26
27
28
# File 'app/models/sbmt/outbox/base_item.rb', line 26

def box_id
  @box_id ||= name.underscore.tr("/", "-").dasherize
end

.box_nameObject



22
23
24
# File 'app/models/sbmt/outbox/base_item.rb', line 22

def box_name
  @box_name ||= name.underscore
end

.box_typeObject

Raises:

  • (NotImplementedError)


18
19
20
# File 'app/models/sbmt/outbox/base_item.rb', line 18

def box_type
  raise NotImplementedError
end

.bucket_partitionsObject



46
47
48
49
50
51
52
53
# File 'app/models/sbmt/outbox/base_item.rb', line 46

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



34
35
36
37
38
39
40
# File 'app/models/sbmt/outbox/base_item.rb', line 34

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

.configObject



30
31
32
# File 'app/models/sbmt/outbox/base_item.rb', line 30

def config
  @config ||= lookup_config.new(box_id: box_id, box_name: box_name)
end

.max_retries_exceeded?(count) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'app/models/sbmt/outbox/base_item.rb', line 55

def max_retries_exceeded?(count)
  return false if config.strict_order
  return true unless retriable?

  count > config.max_retries
end

.partition_bucketsObject



42
43
44
# File 'app/models/sbmt/outbox/base_item.rb', line 42

def partition_buckets
  @partition_buckets ||= calc_bucket_partitions(config.partition_size)
end

.retriable?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/sbmt/outbox/base_item.rb', line 62

def retriable?
  config.max_retries > 0
end

Instance Method Details

#add_error(ex_or_msg) ⇒ Object



162
163
164
165
166
167
168
# File 'app/models/sbmt/outbox/base_item.rb', line 162

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

Returns:

  • (Boolean)


116
117
118
# File 'app/models/sbmt/outbox/base_item.rb', line 116

def for_processing?
  pending?
end

#increment_errors_counterObject



154
155
156
# File 'app/models/sbmt/outbox/base_item.rb', line 154

def increment_errors_counter
  increment(:errors_count)
end

#log_detailsObject



138
139
140
# File 'app/models/sbmt/outbox/base_item.rb', line 138

def log_details
  default_log_details.deep_merge(extra_log_details)
end

#max_retries_exceeded?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'app/models/sbmt/outbox/base_item.rb', line 150

def max_retries_exceeded?
  self.class.max_retries_exceeded?(errors_count)
end

#optionsObject



120
121
122
123
124
# File 'app/models/sbmt/outbox/base_item.rb', line 120

def options
  options = (self[:options] || {}).symbolize_keys
  options = default_options.deep_merge(extra_options).deep_merge(options)
  options.symbolize_keys
end

#partitionObject



170
171
172
# File 'app/models/sbmt/outbox/base_item.rb', line 170

def partition
  self.class.bucket_partitions.fetch(bucket)
end

#payloadObject



100
101
102
103
104
105
106
# File 'app/models/sbmt/outbox/base_item.rb', line 100

def payload
  if has_attribute?(:proto_payload)
    proto_payload
  else
    self[:payload]
  end
end

#payload=(value) ⇒ Object



108
109
110
111
112
113
114
# File 'app/models/sbmt/outbox/base_item.rb', line 108

def payload=(value)
  if has_attribute?(:proto_payload)
    self.proto_payload = value
  else
    self[:payload] = value
  end
end

#payload_builderObject



142
143
144
# File 'app/models/sbmt/outbox/base_item.rb', line 142

def payload_builder
  nil
end

#proto_payloadObject



84
85
86
87
88
89
90
# File 'app/models/sbmt/outbox/base_item.rb', line 84

def proto_payload
  if has_attribute?(:payload)
    payload
  else
    self[:proto_payload]
  end
end

#proto_payload=(value) ⇒ Object



92
93
94
95
96
97
98
# File 'app/models/sbmt/outbox/base_item.rb', line 92

def proto_payload=(value)
  if has_attribute?(:payload)
    self.payload = value
  else
    self[:proto_payload] = value
  end
end

#set_errors_count(count) ⇒ Object



158
159
160
# File 'app/models/sbmt/outbox/base_item.rb', line 158

def set_errors_count(count)
  self.errors_count = count
end

#touch_processed_atObject



146
147
148
# File 'app/models/sbmt/outbox/base_item.rb', line 146

def touch_processed_at
  self.processed_at = Time.current
end

#transportsObject



126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/sbmt/outbox/base_item.rb', line 126

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