Class: Sbmt::Outbox::BaseItemConfig

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

Direct Known Subclasses

InboxItemConfig, OutboxItemConfig

Constant Summary collapse

DEFAULT_BUCKET_SIZE =
16
DEFAULT_PARTITION_STRATEGY =
:number

Instance Method Summary collapse

Constructor Details

#initialize(box_id:, box_name:) ⇒ BaseItemConfig

Returns a new instance of BaseItemConfig.



11
12
13
14
15
16
# File 'app/models/sbmt/outbox/base_item_config.rb', line 11

def initialize(box_id:, box_name:)
  self.box_id = box_id
  self.box_name = box_name

  validate!
end

Instance Method Details

#bucket_sizeObject



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

def bucket_size
  @bucket_size ||= (options[:bucket_size] || yaml_config.fetch(:bucket_size, DEFAULT_BUCKET_SIZE)).to_i
end

#deletion_batch_sizeObject



47
48
49
# File 'app/models/sbmt/outbox/base_item_config.rb', line 47

def deletion_batch_size
  @deletion_batch_size ||= (options[:deletion_batch_size] || 1_000).to_i
end

#deletion_sleep_timeObject



51
52
53
# File 'app/models/sbmt/outbox/base_item_config.rb', line 51

def deletion_sleep_time
  @deletion_sleep_time ||= (options[:deletion_sleep_time] || 0.5).to_f
end

#delivered_min_retention_periodObject



59
60
61
# File 'app/models/sbmt/outbox/base_item_config.rb', line 59

def delivered_min_retention_period
  @delivered_min_retention_period ||= ActiveSupport::Duration.parse(options[:delivered_min_retention_period] || "PT1H")
end

#max_retriesObject



63
64
65
# File 'app/models/sbmt/outbox/base_item_config.rb', line 63

def max_retries
  @max_retries ||= (options[:max_retries] || 0).to_i
end

#maximal_retry_intervalObject



71
72
73
# File 'app/models/sbmt/outbox/base_item_config.rb', line 71

def maximal_retry_interval
  @maximal_retry_interval ||= (options[:maximal_retry_interval] || 600).to_i
end

#min_retention_periodObject



55
56
57
# File 'app/models/sbmt/outbox/base_item_config.rb', line 55

def min_retention_period
  @min_retention_period ||= ActiveSupport::Duration.parse(options[:min_retention_period] || "P1D")
end

#minimal_retry_intervalObject



67
68
69
# File 'app/models/sbmt/outbox/base_item_config.rb', line 67

def minimal_retry_interval
  @minimal_retry_interval ||= (options[:minimal_retry_interval] || 10).to_i
end

#multiplier_retry_intervalObject



75
76
77
# File 'app/models/sbmt/outbox/base_item_config.rb', line 75

def multiplier_retry_interval
  @multiplier_retry_interval ||= (options[:multiplier_retry_interval] || 2).to_i
end

#ownerObject



18
19
20
21
22
# File 'app/models/sbmt/outbox/base_item_config.rb', line 18

def owner
  return @owner if defined?(@owner)

  @owner = options[:owner].presence || yaml_config[:owner].presence
end

#partition_sizeObject



28
29
30
# File 'app/models/sbmt/outbox/base_item_config.rb', line 28

def partition_size
  @partition_size ||= (partition_size_raw || 1).to_i
end

#partition_size_rawObject



32
33
34
# File 'app/models/sbmt/outbox/base_item_config.rb', line 32

def partition_size_raw
  @partition_size_raw ||= options[:partition_size]
end

#partition_strategyObject



97
98
99
100
101
102
# File 'app/models/sbmt/outbox/base_item_config.rb', line 97

def partition_strategy
  return @partition_strategy if defined?(@partition_strategy)

  str_name = options.fetch(:partition_strategy, DEFAULT_PARTITION_STRATEGY).to_s
  @partition_strategy = "Sbmt::Outbox::PartitionStrategies::#{str_name.camelize}Partitioning".constantize
end

#retentionObject



36
37
38
# File 'app/models/sbmt/outbox/base_item_config.rb', line 36

def retention
  @retention ||= ActiveSupport::Duration.parse(options[:retention] || "P1W")
end

#retention_delivered_itemsObject



40
41
42
43
44
45
# File 'app/models/sbmt/outbox/base_item_config.rb', line 40

def retention_delivered_items
  @retention_delivered_items ||= begin
    value = options[:retention_delivered_items] || retention
    value.is_a?(String) ? ActiveSupport::Duration.parse(value) : value
  end
end

#retry_strategiesObject

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/sbmt/outbox/base_item_config.rb', line 79

def retry_strategies
  return @retry_strategies if defined?(@retry_strategies)

  configured_strategies = options[:retry_strategies]

  raise ConfigError, "You cannot use retry_strategies and the strict_order option at the same time." if strict_order.present? && configured_strategies.present?

  strategies = if strict_order.present? && configured_strategies.nil?
    []
  else
    configured_strategies.presence || %w[exponential_backoff latest_available]
  end

  @retry_strategies ||= Array.wrap(strategies).map do |str_name|
    "Sbmt::Outbox::RetryStrategies::#{str_name.camelize}".constantize
  end
end

#strict_orderObject



141
142
143
144
145
# File 'app/models/sbmt/outbox/base_item_config.rb', line 141

def strict_order
  return @strict_order if defined?(@strict_order)

  @strict_order = options[:strict_order].presence
end

#transportsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/sbmt/outbox/base_item_config.rb', line 104

def transports
  return @transports if defined?(@transports)

  values = options.fetch(:transports, [])

  if values.is_a?(Hash)
    values = values.each_with_object([]) do |(key, params), memo|
      memo << params.merge!(class: key)
    end
  end

  @transports = values.each_with_object({}) do |params, memo|
    params = params.symbolize_keys
    event_name = params.delete(:event_name) || :_all_
    memo[event_name] ||= []
    namespace = params.delete(:class)&.camelize
    raise ArgumentError, "Transport name cannot be blank" if namespace.blank?
    disposable = params.key?(:disposable) ? params.delete(:disposable) : Outbox.config.disposable_transports

    factory = "#{namespace}::OutboxTransportFactory".safe_constantize
    memo[event_name] << if factory
      if disposable
        ->(*args) { factory.build(**params).call(*args) }
      else
        factory.build(**params)
      end
    else
      klass = namespace.constantize
      if disposable
        ->(*args) { klass.new(**params).call(*args) }
      else
        klass.new(**params)
      end
    end
  end
end