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



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

#max_retriesObject



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

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

#maximal_retry_intervalObject



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

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

#minimal_retry_intervalObject



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

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

#multiplier_retry_intervalObject



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

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



81
82
83
84
85
86
# File 'app/models/sbmt/outbox/base_item_config.rb', line 81

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:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/sbmt/outbox/base_item_config.rb', line 63

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



125
126
127
128
129
# File 'app/models/sbmt/outbox/base_item_config.rb', line 125

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

  @strict_order = options[:strict_order].presence
end

#transportsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/sbmt/outbox/base_item_config.rb', line 88

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