Module: SidekiqAutoscale::Config::SharedConfigs

Included in:
SidekiqAutoscale
Defined in:
lib/sidekiq_autoscale/config/shared_configs.rb

Constant Summary collapse

LOG_TAG =
"[SIDEKIQ_SCALING]"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject



10
11
12
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 10

def config
  @config ||= ActiveSupport::OrderedOptions.new
end

Instance Method Details

#adapterObject



39
40
41
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 39

def adapter
  config.adapter || :nil
end

#adapter_configObject



57
58
59
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 57

def adapter_config
  config.adapter_config
end

#adapter_klassObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 43

def adapter_klass
  @adapter_klass ||= begin
    known_adapters = [::SidekiqAutoscale::NilAdapter,
                      ::SidekiqAutoscale::HerokuAdapter,
                      ::SidekiqAutoscale::KubernetesAdapter].freeze
    adapter_klass_name = known_adapters.map(&:to_s).find {|i| i.end_with?("#{adapter.to_s.camelize}Adapter") }
    if adapter_klass_name.nil?
      raise ::SidekiqAutoscale::Exception.new("#{LOG_TAG} Unknown scaling adapter: [#{adapter.to_s.camelize}Adapter]")
    end

    adapter_klass_name.constantize.new
  end
end

#cacheObject



107
108
109
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 107

def cache
  config.cache ||= ActiveSupport::Cache::NullStore.new
end

#lock_managerObject



152
153
154
155
156
157
158
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 152

def lock_manager
  config.lock_manager ||= ::Redlock::Client.new(Array.wrap(redis_client),
                                                retry_count:   3,
                                                retry_delay:   200,
                                                retry_jitter:  50,
                                                redis_timeout: 0.1)
end

#lock_timeObject



160
161
162
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 160

def lock_time
  config.lock_time || 5_000
end

#loggerObject



103
104
105
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 103

def logger
  config.logger ||= Rails.logger
end

#max_workersObject



75
76
77
78
79
80
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 75

def max_workers
  (@max_workers ||= begin
    validate_worker_set
    validated_max_workers
  end).to_i
end

#min_scaling_intervalObject



93
94
95
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 93

def min_scaling_interval
  (config.min_scaling_interval || 5.minutes).to_i
end

#min_workersObject



82
83
84
85
86
87
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 82

def min_workers
  (@min_workers ||= begin
    validate_worker_set
    validated_min_workers
  end).to_i
end

#on_head_bump(event) ⇒ Object



136
137
138
139
140
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 136

def on_head_bump(event)
  return unless config.on_head_bump.respond_to?(:call)

  config.on_head_bump.call(event)
end

#on_scaling_error(e) ⇒ Object



111
112
113
114
115
116
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 111

def on_scaling_error(e)
  logger.error(e)
  return unless config.on_scaling_error.respond_to?(:call)

  config.on_scaling_error.call(e)
end

#on_scaling_event(event) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 118

def on_scaling_event(event)
  details = config.to_h.slice(:strategy,
                              :adapter,
                              :scale_up_threshold,
                              :scale_down_threshold,
                              :max_workers,
                              :min_workers,
                              :scale_by,
                              :min_scaling_interval)

  on_head_bump(details.merge(event)) if event[:target_workers] == max_workers
  on_toe_stub(details.merge(event)) if event[:target_workers] == min_workers

  return unless config.on_scaling_event.respond_to?(:call)

  config.on_scaling_event.call(details.merge(event))
end

#on_toe_stub(event) ⇒ Object



142
143
144
145
146
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 142

def on_toe_stub(event)
  return unless config.on_toe_stub.respond_to?(:call)

  config.on_toe_stub.call(event)
end

#redis_clientObject



97
98
99
100
101
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 97

def redis_client
  raise ::SidekiqAutoscale::Exception.new("No Redis client defined") unless config.redis_client

  config.redis_client
end

#scale_byObject



89
90
91
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 89

def scale_by
  (config.scale_by || ENV.fetch("SIDEKIQ_AUTOSCALE_SCALE_BY", 1)).to_i
end

#scale_down_thresholdObject



68
69
70
71
72
73
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 68

def scale_down_threshold
  (config.scale_down_threshold ||= begin
    validate_scaling_thresholds
    validated_scale_down_threshold
  end).to_f
end

#scale_up_thresholdObject



61
62
63
64
65
66
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 61

def scale_up_threshold
  (config.scale_up_threshold ||= begin
    validate_scaling_thresholds
    validated_scale_up_threshold
  end).to_f
end

#sidekiq_interfaceObject



148
149
150
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 148

def sidekiq_interface
  @sidekiq_interface ||= ::SidekiqAutoscale::SidekiqInterface.new
end

#strategyObject



14
15
16
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 14

def strategy
  config.strategy || :base
end

#strategy_klassObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sidekiq_autoscale/config/shared_configs.rb', line 18

def strategy_klass
  @strategy_klass ||= begin
    known_strats = [
      ::SidekiqAutoscale::Strategies::BaseScaling,
      ::SidekiqAutoscale::Strategies::DelayScaling,
      ::SidekiqAutoscale::Strategies::OldestJobScaling,
      ::SidekiqAutoscale::Strategies::LinearScaling,
      ::SidekiqAutoscale::Strategies::DynamicLatencyScaling
    ]

    strat_klass_name = known_strats.map(&:to_s).find {|i| i.end_with?("#{strategy.to_s.camelize}Scaling") }
    if strat_klass_name.nil?
      raise ::SidekiqAutoscale::Exception.new <<~LOG
        #{LOG_TAG} Unknown scaling strategy: [#{strategy.to_s.camelize}Scaling]")
      LOG
    end

    strat_klass_name.constantize.new
  end
end