Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for resque mailer conflict #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/resque/plugins/resque_heroku_autoscaler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,24 +25,24 @@ 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
yield Resque::Plugins::HerokuAutoscaler::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
Expand All @@ -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
Expand All @@ -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_config.wait_time
end

def log(message)
Expand Down