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



24
25
26
# File 'app/models/sbmt/outbox/base_item.rb', line 24

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

.box_nameObject



20
21
22
# File 'app/models/sbmt/outbox/base_item.rb', line 20

def box_name
  @box_name ||= name.underscore
end

.box_typeObject

Raises:

  • (NotImplementedError)


16
17
18
# File 'app/models/sbmt/outbox/base_item.rb', line 16

def box_type
  raise NotImplementedError
end

.bucket_partitionsObject



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

.configObject



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_bucketsObject



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

Returns:

  • (Boolean)


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

def for_processing?
  pending?
end

#increment_errors_counterObject



148
149
150
# File 'app/models/sbmt/outbox/base_item.rb', line 148

def increment_errors_counter
  increment(:errors_count)
end

#log_detailsObject



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

Returns:

  • (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

#optionsObject



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

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

#partitionObject



160
161
162
# File 'app/models/sbmt/outbox/base_item.rb', line 160

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

#payloadObject



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_builderObject



129
130
131
# File 'app/models/sbmt/outbox/base_item.rb', line 129

def payload_builder
  nil
end

#proto_payloadObject



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

Returns:

  • (Boolean)


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

def retriable?
  config.max_retries > 0
end

#touch_processed_atObject



133
134
135
# File 'app/models/sbmt/outbox/base_item.rb', line 133

def touch_processed_at
  self.processed_at = Time.current
end

#transportsObject



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