From 6158c514c7767f68742778cabbbb0d428a3ed0fc Mon Sep 17 00:00:00 2001 From: Robert Johansson Date: Thu, 30 May 2013 07:38:03 +0200 Subject: [PATCH 1/2] Renaming config method to avoid conflict when used with resque-mailer --- .../plugins/resque_heroku_autoscaler.rb | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/resque/plugins/resque_heroku_autoscaler.rb b/lib/resque/plugins/resque_heroku_autoscaler.rb index 5f1e0fa..dd21bcd 100644 --- a/lib/resque/plugins/resque_heroku_autoscaler.rb +++ b/lib/resque/plugins/resque_heroku_autoscaler.rb @@ -4,14 +4,11 @@ module Resque module Plugins module HerokuAutoscaler - def config - Resque::Plugins::HerokuAutoscaler::Config - end def after_enqueue_scale_workers_up(*args) - if !config.scaling_disabled? && \ + if !autoscaler_config.scaling_disabled? && \ Resque.info[:workers] == 0 && \ - config.new_worker_count(Resque.info[:pending]) >= 1 + autoscaler_config.new_worker_count(Resque.info[:pending]) >= 1 set_workers(1) Resque.redis.set('last_scaled', Time.now) @@ -28,16 +25,16 @@ def on_failure_scale_workers(*args) def set_workers(number_of_workers) if number_of_workers != current_workers - heroku_api.post_ps_scale(config.heroku_app, 'worker', number_of_workers) + heroku_api.post_ps_scale(autoscaler_config.heroku_app, 'worker', number_of_workers) end end def current_workers - heroku_api.get_ps(config.heroku_app).body.count {|p| p['process'].match(/worker\.\d+/) } + heroku_api.get_ps(autoscaler_config.heroku_app).body.count {|p| p['process'].match(/worker\.\d+/) } end def heroku_api - @heroku_api ||= ::Heroku::API.new(api_key: config.heroku_api_key) + @heroku_api ||= ::Heroku::API.new(api_key: autoscaler_config.heroku_api_key) end def self.config @@ -45,7 +42,7 @@ def self.config end def calculate_and_set_workers - unless config.scaling_disabled? + unless autoscaler_config.scaling_disabled? wait_for_task_or_scale if time_to_scale? scale @@ -55,8 +52,15 @@ def calculate_and_set_workers private + # Call this method autoscaler_config instead of just config to avoid conflict when using with resque-mailer + # The method gets added as a class method on the mailer in that case, where Rails assumes it to be of type + # ActiveSupport::InheritableOptions + def autoscaler_config + Resque::Plugins::HerokuAutoscaler::Config + end + def scale - new_count = config.new_worker_count(Resque.info[:pending]) + new_count = autoscaler_config.new_worker_count(Resque.info[:pending]) set_workers(new_count) if new_count == 0 || new_count > current_workers Resque.redis.set('last_scaled', Time.now) end @@ -68,7 +72,7 @@ def wait_for_task_or_scale end def time_to_scale? - (Time.now - Time.parse(Resque.redis.get('last_scaled'))) >= config.wait_time + (Time.now - Time.parse(Resque.redis.get('last_scaled'))) >= autoscaler_configconfig.wait_time end def log(message) From 19cbcecf94cfe5b0ee7104b681a13b917a2c79aa Mon Sep 17 00:00:00 2001 From: Robert Johansson Date: Mon, 3 Jun 2013 08:45:30 +0200 Subject: [PATCH 2/2] Fix for typo --- lib/resque/plugins/resque_heroku_autoscaler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resque/plugins/resque_heroku_autoscaler.rb b/lib/resque/plugins/resque_heroku_autoscaler.rb index dd21bcd..96e643d 100644 --- a/lib/resque/plugins/resque_heroku_autoscaler.rb +++ b/lib/resque/plugins/resque_heroku_autoscaler.rb @@ -72,7 +72,7 @@ def wait_for_task_or_scale end def time_to_scale? - (Time.now - Time.parse(Resque.redis.get('last_scaled'))) >= autoscaler_configconfig.wait_time + (Time.now - Time.parse(Resque.redis.get('last_scaled'))) >= autoscaler_config.wait_time end def log(message)