Class: SidekiqAutoscale::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_autoscale/middleware.rb

Constant Summary collapse

POOL_NAME =
ENV.fetch("SIDEKIQ_POOL", "default")
LAST_SCALED_AT_EVENT_KEY =
"sidekiq_autoscaling:#{POOL_NAME}:last_scaled_at"
SCALING_LOCK_KEY =
"sidekiq_autoscaling:#{POOL_NAME}:scaling_lock"
LOG_TAG =
"[SIDEKIQ_SCALE][SCALING_EVENT]"
WORKER_COUNT_KEY =
"sidekiq_autoscaling/#{POOL_NAME}/current_worker_count"

Instance Method Summary collapse

Instance Method Details

#call(_worker_class, job, _queue) { ... } ⇒ Void

Parameters:

Yields:

  • the next middleware in the chain or worker ‘perform` method

Returns:

  • (Void)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sidekiq_autoscale/middleware.rb', line 17

def call(_worker_class, job, _queue)
  # In case the scaling strategy needs to record job-specific stuff before it runs
  SidekiqAutoscale.strategy_klass.log_job(job)
  yield # Run the job, THEN scale the cluster
  begin
    return unless SidekiqAutoscale.strategy_klass.workload_change_needed?(job)

    direction = SidekiqAutoscale.strategy_klass.scaling_direction(job)
    new_worker_count = worker_count + (SidekiqAutoscale.scale_by * direction)

    set_worker_count(new_worker_count, event_id: job["jid"], direction: direction)
  rescue StandardError => e
    SidekiqAutoscale.logger.error(e)
    SidekiqAutoscale.on_scaling_error(e)
  end
end