Skip to content

Commit

Permalink
handle excon errors in herou scaler
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinLove committed Aug 31, 2013
1 parent bb386d6 commit 39d5bd8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
36 changes: 34 additions & 2 deletions lib/autoscaler/heroku_scaler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def workers
if known?
@workers
else
know client.get_ps(app).body.count {|ps| ps['process'].match /#{type}\.\d?/ }
know heroku_get_workers
end
end

Expand All @@ -35,11 +35,23 @@ def workers
def workers=(n)
if n != @workers || !known?
p "Scaling #{type} to #{n}"
client.post_ps_scale(app, type, n)
heroku_set_workers(n)
know n
end
end

# Callable object which responds to exceptions during api calls
#
# @example
# heroku.exception_handler = lambda {|exception| MyApp.logger.error(exception)}
# heroku.exception_handler = lambda {|exception| raise}
# # default
# lambda {|exception|
# p exception
# puts exception.backtrace
# }
attr_writer :exception_handler

private
attr_reader :client

Expand All @@ -51,5 +63,25 @@ def know(n)
def known?
Time.now < @known
end

def heroku_get_workers
client.get_ps(app).body.count {|ps| ps['process'].match /#{type}\.\d?/ }
rescue Excon::Errors::Error => e
exception_handler.call(e)
@workers
end

def heroku_set_workers(n)
client.post_ps_scale(app, type, n)
rescue Excon::Errors::Error => e
exception_handler.call(e)
end

def exception_handler
@exception_handler ||= lambda {|exception|
p exception
puts exception.backtrace
}
end
end
end
23 changes: 23 additions & 0 deletions spec/autoscaler/heroku_scaler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,27 @@

its(:workers) {should == 1}
end

describe 'exception handling', :focus => true do
before do
def client.client
raise Excon::Errors::SocketError.new(Exception.new('oops'))
end
end

describe "default handler" do
it {expect{client.workers}.to_not raise_error}
it {client.workers.should == 0}
it {expect{client.workers = 1}.to_not raise_error}
end

describe "custom handler" do
before do
@caught = false
client.exception_handler = lambda {|exception| @caught = true}
end

it {client.workers; @caught.should be_true}
end
end
end

0 comments on commit 39d5bd8

Please sign in to comment.