Skip to content

Commit

Permalink
After enque only scales workers up to 1 if there is no worker
Browse files Browse the repository at this point in the history
after_enque now only starts a single worker, if there is none. The current worker count also is being pulled from Redis instead of heroku
This improves response time of the dyno by moving expencive API calls to the worker
  • Loading branch information
ajmurmann committed May 16, 2011
1 parent 4e897ef commit 98d1c3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
6 changes: 5 additions & 1 deletion lib/resque/plugins/resque_heroku_autoscaler.rb
Expand Up @@ -6,7 +6,11 @@ module HerokuAutoscaler
@@heroku_client = nil

def after_enqueue_scale_workers_up(*args)
calculate_and_set_workers
if !Resque::Plugins::HerokuAutoscaler::Config.scaling_disabled? && \
Resque.info[:workers] == 0 && \
Resque::Plugins::HerokuAutoscaler::Config.new_worker_count(Resque.info[:pending]) >= 1
set_workers(1)
end
end

def after_perform_scale_workers(*args)
Expand Down
33 changes: 11 additions & 22 deletions spec/resque_heroku_autoscaler_spec.rb
Expand Up @@ -41,36 +41,25 @@ class AnotherJob
stub(TestJob).heroku_client { @fake_heroku_client }

lambda do
TestJob.after_enqueue_scale_workers_up("some", "random", "aguments", 42)
TestJob.after_enqueue_scale_workers_up("some", "random", "aguments", 42)
end.should_not raise_error
end

it "should create one worker" do
stub(TestJob).current_workers { 0 }
stub(Resque).info{ {:pending => 1} }
mock(TestJob).set_workers(1)
TestJob.after_enqueue_scale_workers_up
end

context "when new_worker_count was changed" do
before do
stub(TestJob).current_workers { 1 }
@original_method = Resque::Plugins::HerokuAutoscaler::Config.instance_variable_get(:@new_worker_count)
subject.config do |c|
c.new_worker_count do
2
end
@original_method = Resque::Plugins::HerokuAutoscaler::Config.instance_variable_get(:@new_worker_count)
subject.config do |c|
c.new_worker_count do
5
end
end

after do
Resque::Plugins::HerokuAutoscaler::Config.instance_variable_set(:@new_worker_count, @original_method)
end
stub(TestJob).current_workers { 0 }
stub(Resque).info { {:pending => 100, :workers => 0} }
mock(TestJob).set_workers(1)
TestJob.after_enqueue_scale_workers_up

it "should use the given block" do
mock(TestJob).set_workers(2)
TestJob.after_enqueue_scale_workers_up
end
Resque::Plugins::HerokuAutoscaler::Config.instance_variable_set(:@new_worker_count, @original_method)
end

context "when scaling workers is disabled" do
Expand Down Expand Up @@ -257,7 +246,7 @@ class AnotherJob
c.heroku_app = 'some_app_name'
end

stub(TestJob).current_workers {0}
stub(TestJob).current_workers { 0 }
mock(TestJob).heroku_client { mock(@fake_heroku_client).set_workers('some_app_name', 10) }
TestJob.set_workers(10)
end
Expand Down

0 comments on commit 98d1c3f

Please sign in to comment.