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



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

def adapter_config
  config.adapter_config
end

#adapter_klassObject



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

def adapter_klass
  @adapter_klass ||= begin
    known_adapters = [::SidekiqAutoscale::NilAdapter,
                      ::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



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

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

#lock_managerObject



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

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



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

def lock_time
  config.lock_time || 5_000
end

#loggerObject



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

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

#max_workersObject



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

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

#min_scaling_intervalObject



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

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

#min_workersObject



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

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

#on_head_bump(event) ⇒ Object



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

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



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

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



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

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



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

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

  config.on_toe_stub.call(event)
end

#redis_clientObject



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

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

  config.redis_client
end

#scale_byObject



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

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

#scale_down_thresholdObject



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

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

#scale_up_thresholdObject



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

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

#sidekiq_interfaceObject



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

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