Class: Autoscaler::Sidekiq::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/autoscaler/sidekiq/client.rb

Overview

Sidekiq client middleware Performs scale-up when items are queued and there are no workers running

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scalers) ⇒ Client

Returns a new instance of Client.

Parameters:

  • scalers (Hash)

    map of queue(String) => scaler (e.g. HerokuPlatformScaler). Which scaler to use for each sidekiq queue


11
12
13
# File 'lib/autoscaler/sidekiq/client.rb', line 11

def initialize(scalers)
  @scalers = scalers
end

Class Method Details

.add_to_chain(chain, scalers) ⇒ Client

Convenience method to avoid having to name the class and parameter twice when calling set_initial_workers

Returns:

  • (Client)

    an instance of Client for set_initial_workers


44
45
46
47
# File 'lib/autoscaler/sidekiq/client.rb', line 44

def self.add_to_chain(chain, scalers)
  chain.add self, scalers
  new(scalers)
end

Instance Method Details

#call(worker_class, item, queue, _ = nil) ⇒ Object

Sidekiq middleware api method


16
17
18
19
20
21
22
23
24
25
# File 'lib/autoscaler/sidekiq/client.rb', line 16

def call(worker_class, item, queue, _ = nil)
  result = yield

  scaler = @scalers[queue]
  if scaler && scaler.workers < 1
    scaler.workers = 1
  end

  result
end

#set_initial_workers(strategy = nil) {|queue| ... } ⇒ Object

Check for interrupted or scheduled work on startup. Typically you need to construct your own instance just to call this method, but see add_to_chain.

Parameters:

  • strategy (Strategy) (defaults to: nil)

    object that determines target workers

Yield Parameters:

  • queue (String)

    mostly for testing

Yield Returns:


33
34
35
36
37
38
39
# File 'lib/autoscaler/sidekiq/client.rb', line 33

def set_initial_workers(strategy = nil, &system_factory)
  strategy ||= BinaryScalingStrategy.new
  system_factory ||= lambda {|queue| SpecifiedQueueSystem.new([queue])} 
  @scalers.each do |queue, scaler|
    scaler.workers = strategy.call(system_factory.call(queue), 0)
  end
end