diff --git a/README.md b/README.md index 793fbf7..b1e7f7e 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Tidbits * Jobs are serialized as JSON, so you should stick to strings, integers, arrays, and hashes as arguments to jobs. e.g. don't pass full Ruby objects - use something like an ActiveRecord/MongoMapper/CouchRest id instead. * Because there are no class definitions associated with jobs, you can queue jobs from anywhere without needing to include your full app's environment. -* If you need to change the location of your Beanstalk from the default (localhost:11300), set BEANSTALK_URL in your environment, e.g. export BEANSTALK_URL=beanstalk://example.com:11300/ +* If you need to change the location of your Beanstalk from the default (localhost:11300), set BEANSTALK_URL in your environment, e.g. export BEANSTALK_URL=beanstalk://example.com:11300/. You can specify multiple beanstalk servers, separated by whitespace or comma, e.g. export BEANSTALK_URL="beanstalk://b1.example.com:11300/, beanstalk://b2.example.com:11300/" * The stalk binary is just for convenience, you can also run a worker with a straight Ruby command: $ ruby -r jobs -e Stalker.work diff --git a/lib/stalker.rb b/lib/stalker.rb index 0f6101d..5542059 100644 --- a/lib/stalker.rb +++ b/lib/stalker.rb @@ -134,7 +134,7 @@ def log_error(msg) end def beanstalk - @@beanstalk ||= Beanstalk::Pool.new([ beanstalk_host_and_port ]) + @@beanstalk ||= Beanstalk::Pool.new(beanstalk_addresses) end def beanstalk_url @@ -144,10 +144,15 @@ def beanstalk_url class BadURL < RuntimeError; end - def beanstalk_host_and_port - uri = URI.parse(beanstalk_url) - raise(BadURL, beanstalk_url) if uri.scheme != 'beanstalk' - return "#{uri.host}:#{uri.port || 11300}" + def beanstalk_addresses + uris = beanstalk_url.split(/[\s,]+/) + uris.map {|uri| beanstalk_host_and_port(uri)} + end + + def beanstalk_host_and_port(uri_string) + uri = URI.parse(uri_string) + raise(BadURL, uri_string) if uri.scheme != 'beanstalk' + "#{uri.host}:#{uri.port || 11300}" end def exception_message(e) diff --git a/test/stalker_test.rb b/test/stalker_test.rb index adc66ad..2684409 100644 --- a/test/stalker_test.rb +++ b/test/stalker_test.rb @@ -12,6 +12,7 @@ class StalkerTest < Test::Unit::TestCase Stalker.clear! $result = -1 $handled = false + ENV['BEANSTALK_URL'] = "" end test "enqueue and work a job" do @@ -77,7 +78,7 @@ class StalkerTest < Test::Unit::TestCase end test "before filter invokes error handler when defined" do - Stalker.error { |e| $handled = true } + Stalker.error { |e| $handled = true } Stalker.before { |name| fail } Stalker.job('my.job') { } Stalker.enqueue('my.job') @@ -85,5 +86,18 @@ class StalkerTest < Test::Unit::TestCase Stalker.work_one_job assert_equal true, $handled end + + test "parse BEANSTALK_URL" do + ENV['BEANSTALK_URL'] = "beanstalk://localhost:12300" + assert_equal Stalker.beanstalk_addresses, ["localhost:12300"] + ENV['BEANSTALK_URL'] = "beanstalk://localhost:12300/, beanstalk://localhost:12301/" + assert_equal Stalker.beanstalk_addresses, ["localhost:12300","localhost:12301"] + ENV['BEANSTALK_URL'] = "beanstalk://localhost:12300 beanstalk://localhost:12301" + assert_equal Stalker.beanstalk_addresses, ["localhost:12300","localhost:12301"] + ENV['BEANSTALK_URL'] = "beanstalk://localhost:12300, http://localhost:12301" + assert_raise Stalker::BadURL do + Stalker.beanstalk_addresses + end + end end