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



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

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

.box_nameObject



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

def box_name
  @box_name ||= name.underscore
end

.box_typeObject

Raises:

  • (NotImplementedError)


11
12
13
# File 'app/models/sbmt/outbox/base_item.rb', line 11

def box_type
  raise NotImplementedError
end

.bucket_partitionsObject



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

.configObject



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_bucketsObject



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

Returns:

  • (Boolean)


98
99
100
# File 'app/models/sbmt/outbox/base_item.rb', line 98

def for_processing?
  pending?
end

#increment_errors_counterObject



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

def increment_errors_counter
  increment(:errors_count)
end

#log_detailsObject



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

Returns:

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

#optionsObject



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

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

#partitionObject



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

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

#payloadObject



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_builderObject



124
125
126
# File 'app/models/sbmt/outbox/base_item.rb', line 124

def payload_builder
  nil
end

#proto_payloadObject



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

Returns:

  • (Boolean)


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

def retriable?
  config.max_retries > 0
end

#touch_processed_atObject



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

def touch_processed_at
  self.processed_at = Time.current
end

#transportsObject



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