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

Support for multiple beanstalk servers #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 10 additions & 5 deletions lib/stalker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion test/stalker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,13 +78,26 @@ 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')
Stalker.prep
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