Sidekiq performs background jobs. While its threading model allows it to scale easier than worker-pre-process background systems, people running test or lightly loaded systems on Heroku still want to scale down to zero to avoid racking up charges.
Tested on Ruby 2.1.7 and Heroku Cedar stack.
gem install autoscaler
This gem uses the Heroku Platform-Api gem, which requires an OAuth token from Heroku. It will also need the heroku app name. By default, these are specified through environment variables. You can also pass them to HerokuPlatformScaler
explicitly.
AUTOSCALER_HEROKU_ACCESS_TOKEN=.....
AUTOSCALER_HEROKU_APP=....
Install the middleware in your Sidekiq.configure_
blocks
require 'autoscaler/sidekiq'
require 'autoscaler/heroku_platform_scaler'
Sidekiq.configure_client do |config|
config.client_middleware do |chain|
chain.add Autoscaler::Sidekiq::Client, 'default' => Autoscaler::HerokuPlatformScaler.new
end
end
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add(Autoscaler::Sidekiq::Server, Autoscaler::HerokuPlatformScaler.new, 60) # 60 second timeout
end
end
config.client_middleware
in your Sidekiq.configure_server
block in order to scale-up.HerokuPlatformScaler#exception_handler
to overrideYou can pass a scaling strategy object instead of the timeout to the server middleware. The object (or lambda) should respond to #call(system, idle_time)
and return the desired number of workers. See lib/autoscaler/binary_scaling_strategy.rb
for an example.
Client#set_initial_workers
to start workers on main process startup; typically:
Autoscaler::Sidekiq::Client.add_to_chain(chain, 'default' => heroku).set_initial_workers
scaler.counter_cache = Autoscaler::CounterCacheRedis.new(Sidekiq.method(:redis))
The project is setup to run RSpec with Guard. It expects a redis instance on a custom port, which is started by the Guardfile.
The HerokuPlatformScaler is not tested by default because it makes live API requests. Specify AUTOSCALER_HEROKU_APP
and AUTOSCALER_HEROKU_ACCESS_TOKEN
on the command line, and then watch your app's logs.
AUTOSCALER_HEROKU_APP=... AUTOSCALER_HEROKU_ACCESS_TOKEN=... guard
heroku logs --app ...
Justin Love, @wondible, https://github.com/JustinLove
Released under the MIT license.