Class: RooOnRails::Sidekiq::ProcessScaling

Inherits:
Object
  • Object
show all
Defined in:
lib/roo_on_rails/sidekiq/process_scaling.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue_latencies) ⇒ ProcessScaling

Returns a new instance of ProcessScaling.



11
12
13
14
# File 'lib/roo_on_rails/sidekiq/process_scaling.rb', line 11

def initialize(queue_latencies)
  @queue_latencies = queue_latencies
  @queue_names = @queue_latencies.map(&:name)
end

Instance Method Details

#current_processesObject



16
17
18
19
20
21
22
23
# File 'lib/roo_on_rails/sidekiq/process_scaling.rb', line 16

def current_processes
  ::Sidekiq::ProcessSet.new.count do |process|
    process['quiet'].to_s == 'false' &&
    @queue_names.any? do |queue_name|
      process['queues'].include?(queue_name)
    end
  end
end

#max_normalised_latencyObject



25
26
27
# File 'lib/roo_on_rails/sidekiq/process_scaling.rb', line 25

def max_normalised_latency
    @queue_latencies.any? ? @queue_latencies.map(&:normalised_latency).max.to_f : 0.0
end

#requested_processesObject



29
30
31
32
33
34
35
36
37
# File 'lib/roo_on_rails/sidekiq/process_scaling.rb', line 29

def requested_processes
  if max_normalised_latency > WORKER_INCREASE_THRESHOLD
    current_processes + 1
  elsif max_normalised_latency < WORKER_DECREASE_THRESHOLD
    [current_processes - 1, 1].max
  else
    current_processes
  end
end